Class StringCompareUtil

java.lang.Object
com.hypixel.hytale.common.util.StringCompareUtil

public class StringCompareUtil extends Object
Since:
2020/07/17
Version:
1.0
  • Constructor Details

    • StringCompareUtil

      public StringCompareUtil()
  • Method Details

    • indexOfDifference

      public static int indexOfDifference(@Nullable CharSequence cs1, @Nullable CharSequence cs2)
      Source: https://github.com/apache/commons-lang/blob/954338a1522aa32b869966be63ef833d10cdd79e/src/main/java/org/apache/commons/lang3/StringUtils.java#L3021-L3063

      Compares two CharSequences, and returns the index at which the CharSequences begin to differ.

      For example, indexOfDifference("i am a machine", "i am a robot") -> 7

      StringUtils.indexOfDifference(null, null) = -1
      StringUtils.indexOfDifference("", "") = -1
      StringUtils.indexOfDifference("", "abc") = 0
      StringUtils.indexOfDifference("abc", "") = 0
      StringUtils.indexOfDifference("abc", "abc") = -1
      StringUtils.indexOfDifference("ab", "abxyz") = 2
      StringUtils.indexOfDifference("abcde", "abxyz") = 2
      StringUtils.indexOfDifference("abcde", "xyz") = 0
      
      Parameters:
      cs1 - the first CharSequence, may be null
      cs2 - the second CharSequence, may be null
      Returns:
      the index where cs1 and cs2 begin to differ; -1 if they are equal
    • getFuzzyDistance

      public static int getFuzzyDistance(@Nonnull CharSequence term, @Nonnull CharSequence query, @Nonnull Locale locale)

      Find the Fuzzy Distance which indicates the similarity score between two Strings.

      This string matching algorithm is similar to the algorithms of editors such as Sublime Text, TextMate, Atom and others. One point is given for every matched character. Subsequent matches yield two bonus points. A higher score indicates a higher similarity.

      StringUtils.getFuzzyDistance(null, null, null)                                    = IllegalArgumentException
      StringUtils.getFuzzyDistance("", "", Locale.ENGLISH)                              = 0
      StringUtils.getFuzzyDistance("Workshop", "b", Locale.ENGLISH)                     = 0
      StringUtils.getFuzzyDistance("Room", "o", Locale.ENGLISH)                         = 1
      StringUtils.getFuzzyDistance("Workshop", "w", Locale.ENGLISH)                     = 1
      StringUtils.getFuzzyDistance("Workshop", "ws", Locale.ENGLISH)                    = 2
      StringUtils.getFuzzyDistance("Workshop", "wo", Locale.ENGLISH)                    = 4
      StringUtils.getFuzzyDistance("Apache Software Foundation", "asf", Locale.ENGLISH) = 3
      
      Parameters:
      term - a full term that should be matched against, must not be null
      query - the query that will be matched against a term, must not be null
      locale - This string matching logic is case insensitive. A locale is necessary to normalize both Strings to lower case.
      Returns:
      result score
      Throws:
      IllegalArgumentException - if either String input null or Locale input null
      Since:
      3.4
    • getLevenshteinDistance

      public static int getLevenshteinDistance(@Nonnull CharSequence s, @Nonnull CharSequence t)
      Source: https://github.com/apache/commons-lang/blob/954338a1522aa32b869966be63ef833d10cdd79e/src/main/java/org/apache/commons/lang3/StringUtils.java#L2262-L2350

      Find the Levenshtein distance between two Strings.

      This is the number of changes needed to change one String into another, where each change is a single character modification (deletion, insertion or substitution).

      The implementation uses a single-dimensional array of length s.length() + 1. See http://blog.softwx.net/2014/12/optimizing-levenshtein-algorithm-in-c.html for details.

      StringUtils.getLevenshteinDistance(null, *)             = IllegalArgumentException
      StringUtils.getLevenshteinDistance(*, null)             = IllegalArgumentException
      StringUtils.getLevenshteinDistance("", "")              = 0
      StringUtils.getLevenshteinDistance("", "a")             = 1
      StringUtils.getLevenshteinDistance("aaapppp", "")       = 7
      StringUtils.getLevenshteinDistance("frog", "fog")       = 1
      StringUtils.getLevenshteinDistance("fly", "ant")        = 3
      StringUtils.getLevenshteinDistance("elephant", "hippo") = 7
      StringUtils.getLevenshteinDistance("hippo", "elephant") = 7
      StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz") = 8
      StringUtils.getLevenshteinDistance("hello", "hallo")    = 1
      
      Parameters:
      s - the first String, must not be null
      t - the second String, must not be null
      Returns:
      result distance
      Throws:
      IllegalArgumentException - if either String input null