Class Utils

java.lang.Object
io.github.valentinherrmann.levenshtein.Utils

public final class Utils extends Object
  • Method Details

    • levenshteinDistance

      public static int levenshteinDistance(String s1, String s2)
      Calculates the Levenshtein distance between two strings.
      Parameters:
      s1 - First string
      s2 - Second string
      Returns:
      The edit distance between the two strings, or -1 if one of them is null
    • levenshteinDistancePercent

      public static double levenshteinDistancePercent(String expected, String actual)
      Calculates the Levenshtein distance as percentage of the length of the longer string.
      Parameters:
      expected - the expected string
      actual - the actual string
      Returns:
      the deviation in percent (0 for two empty strings), or -1 if one of them is null
    • isNameWithinDeviation

      public static boolean isNameWithinDeviation(String expectedName, String actualName, int threshold)
      Checks if names are within a specified deviation threshold.
      Parameters:
      expectedName - The expected name
      actualName - The actual name
      threshold - The deviation threshold in percent (0-100)
      Returns:
      true if the deviation is within the threshold, false otherwise
    • safeCast

      public static Object safeCast(Object val, Class<?> castTo)
      Converts a value to the given type: returns the value itself if it already is an instance, or converts between numeric types (e.g. an int result where a double is expected; char counts as numeric type). A numeric conversion must not change the value beyond rounding: an integral type (int, long, short, byte, char) only takes a whole number within its range (2.5 or 300 do not fit into a byte, 65 becomes 'A'); a float takes every double that does not overflow it. Fails with a readable assertion message if neither is possible.
      Parameters:
      val - the value to convert (must not be null)
      castTo - the expected type
      Returns:
      the converted value
    • safeCast

      public static Object safeCast(Object val, Class<?> castTo, boolean allowNull)
      Like safeCast(Object, Class), but optionally permits null.
      Parameters:
      val - the value to convert
      castTo - the expected type
      allowNull - whether null is accepted (and returned)
      Returns:
      the converted value
    • selectWorstExistence

      public static WrapperProperty.Existence selectWorstExistence(WrapperProperty.Existence worst, WrapperProperty.Existence updated)
      Selects the worse existence state between two states, ignoring UNCHECKED. Used to aggregate multiple property existence states into a single worst-case state.
      Parameters:
      worst - the current worst existence state
      updated - the new existence state to compare
      Returns:
      the worse of the two existence states
      See Also:
    • verifyType

      public static void verifyType(WrapperProperty<Class<?>> typeWrapperProperty, Class<?> actualType)
      Verifies that the actual type matches or is compatible with the expected type. Checks for exact match, primitive/wrapper equivalence (e.g. int vs. Integer), assignability, or numeric type containment (e.g. long can contain int). Updates the type property existence state accordingly.
      Parameters:
      typeWrapperProperty - the wrapper property containing the expected type
      actualType - the actual type found via reflection
    • isNumericDeviation

      public static boolean isNumericDeviation(Class<?> expected, Class<?> actual)
      Checks whether two types are different numeric types in either direction (e.g. long vs. int, Integer vs. double). boolean and non-primitive types never qualify.
      Parameters:
      expected - the expected type
      actual - the actual type
      Returns:
      true if both unwrap to numeric primitives (byte, short, char, int, long, float, double)
    • canContain

      public static boolean canContain(Class<?> actualType, Class<?> expectedType)
      Checks if the actual primitive type can contain the expected primitive type, i.e. whether Java's widening primitive conversion goes from the expected to the actual type. For example, a long can contain an int, a double can contain a float, and every numeric type except byte and short can contain a char.
      Parameters:
      actualType - the actual type to check
      expectedType - the expected type to verify containment for
      Returns:
      true if actualType can contain values of expectedType, false otherwise
    • unwrapPrimitive

      public static Class<?> unwrapPrimitive(Class<?> type)
      Unwraps a primitive wrapper class to its corresponding primitive type. For example, Integer.class is unwrapped to int.class.
      Parameters:
      type - the type to unwrap
      Returns:
      the primitive type if type is a wrapper, otherwise the original type
    • toWrapperType

      public static Class<?> toWrapperType(Class<?> type)
      Converts a primitive type to its corresponding wrapper type. For example, int.class is converted to Integer.class.
      Parameters:
      type - the type to convert
      Returns:
      the wrapper type if type is primitive, otherwise the original type