All Downloads are FREE. Search and download functionalities are using the official Maven repository.

us.fatehi.utility.PrefixMatches Maven / Gradle / Ivy

Go to download

SchemaCrawler is an open-source Java API that makes working with database metadata as easy as working with plain old Java objects. SchemaCrawler is also a database schema discovery and comprehension, and schema documentation tool. You can search for database schema objects using regular expressions, and output the schema and data in a readable text format. The output is designed to be diff-ed against other database schemas.

There is a newer version: 16.24.2
Show newest version
/*
========================================================================
SchemaCrawler
http://www.schemacrawler.com
Copyright (c) 2000-2024, Sualeh Fatehi .
All rights reserved.
------------------------------------------------------------------------

SchemaCrawler is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

SchemaCrawler and the accompanying materials are made available under
the terms of the Eclipse Public License v1.0, GNU General Public License
v3 or GNU Lesser General Public License v3.

You may elect to redistribute this code under any of these licenses.

The Eclipse Public License is available at:
http://www.eclipse.org/legal/epl-v10.html

The GNU General Public License v3 and the GNU Lesser General Public
License v3 are available at:
http://www.gnu.org/licenses/

========================================================================
*/

package us.fatehi.utility;

import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.Utility.commonPrefix;
import static us.fatehi.utility.Utility.isBlank;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import us.fatehi.utility.string.ObjectToStringFormat;
import us.fatehi.utility.string.StringFormat;

public final class PrefixMatches {

  private static final Logger LOGGER = Logger.getLogger(PrefixMatches.class.getName());

  private final String keySeparator;
  private final Multimap keyPrefixes;

  public PrefixMatches(final List keys, final String keySeparator) {
    this.keySeparator = requireNonNull(keySeparator, "No key separator provided");
    keyPrefixes = new Multimap<>();

    analyze(keys);
  }

  public List get(final String key) {
    if (keyPrefixes.containsKey(key)) {
      return keyPrefixes.get(key);
    } else {
      return Arrays.asList(key);
    }
  }

  @Override
  public String toString() {
    return keyPrefixes.toString();
  }

  private void analyze(final List keys) {
    if (keys.isEmpty()) {
      return;
    }

    final Collection prefixes = findPrefixes(keys);
    mapPrefixes(keys, prefixes);

    LOGGER.log(Level.FINE, new StringFormat("Key prefixes=%s", prefixes));
    LOGGER.log(
        Level.FINE, new StringFormat("Key matches map: %s", new ObjectToStringFormat(keyPrefixes)));
  }

  /**
   * Finds key prefixes. Prefixes are separated by a separator character.
   *
   * @param keys Keys
   * @return Key name prefixes
   */
  private Collection findPrefixes(final List keys) {
    final SortedMap prefixesMap = new TreeMap<>();
    for (int i = 0; i < keys.size(); i++) {
      for (int j = i + 1; j < keys.size(); j++) {
        final String key1 = keys.get(i);
        final String key2 = keys.get(j);
        final String commonPrefix = commonPrefix(key1, key2);
        if (isBlank(commonPrefix)) {
          continue;
        }

        final List splitCommonPrefixes = new ArrayList<>();
        final String[] splitPrefix = commonPrefix.split(keySeparator);
        if (splitPrefix != null && splitPrefix.length > 0) {
          for (int k = 0; k < splitPrefix.length; k++) {
            final StringBuilder buffer = new StringBuilder(1024);
            for (int l = 0; l < k; l++) {
              buffer.append(splitPrefix[l]).append(keySeparator);
            }
            if (buffer.length() > 0) {
              splitCommonPrefixes.add(buffer.toString());
            }
          }
        }
        if (commonPrefix.endsWith(keySeparator)) {
          splitCommonPrefixes.add(commonPrefix);
        }

        for (final String splitCommonPrefix : splitCommonPrefixes) {
          final int prevCount;
          if (prefixesMap.containsKey(splitCommonPrefix)) {
            prevCount = prefixesMap.get(splitCommonPrefix);
          } else {
            prevCount = 0;
          }
          prefixesMap.put(splitCommonPrefix, prevCount + 1);
        }
      }
    }

    // Sort prefixes by the number of keys using them, in descending order
    final List> prefixesList = new ArrayList<>(prefixesMap.entrySet());
    Collections.sort(
        prefixesList, (entry1, entry2) -> entry1.getValue().compareTo(entry2.getValue()));

    // Reduce the number of prefixes in use
    final List prefixes = new ArrayList<>();
    for (int i = 0; i < prefixesList.size(); i++) {
      final boolean add = i < 5 || prefixesList.get(i).getValue() > prefixesMap.size() * 0.5;
      if (add) {
        prefixes.add(prefixesList.get(i).getKey());
      }
    }
    // Always return the full key as a prefix to itself
    prefixes.add("");

    return prefixes;
  }

  private void mapPrefixes(final List keys, final Collection prefixes) {
    for (final String key : keys) {
      for (final String prefix : prefixes) {
        String matchKeyName = key.toLowerCase();
        if (matchKeyName.startsWith(prefix)) {
          matchKeyName = matchKeyName.substring(prefix.length());
          matchKeyName = Inflection.singularize(matchKeyName);
          if (!isBlank(matchKeyName)) {
            keyPrefixes.add(key, matchKeyName);
          }
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy