Class Utils
java.lang.Object
io.github.valentinherrmann.levenshtein.Utils
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanContain(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.static booleanisNameWithinDeviation(String expectedName, String actualName, int threshold) Checks if names are within a specified deviation threshold.static booleanisNumericDeviation(Class<?> expected, Class<?> actual) Checks whether two types are different numeric types in either direction (e.g.static intlevenshteinDistance(String s1, String s2) Calculates the Levenshtein distance between two strings.static doublelevenshteinDistancePercent(String expected, String actual) Calculates the Levenshtein distance as percentage of the length of the longer string.static ObjectConverts a value to the given type: returns the value itself if it already is an instance, or converts between numeric types (e.g. anintresult where adoubleis expected;charcounts as numeric type).static ObjectLikesafeCast(Object, Class), but optionally permitsnull.static WrapperProperty.ExistenceselectWorstExistence(WrapperProperty.Existence worst, WrapperProperty.Existence updated) Selects the worse existence state between two states, ignoringUNCHECKED.static Class<?> toWrapperType(Class<?> type) Converts a primitive type to its corresponding wrapper type.static Class<?> unwrapPrimitive(Class<?> type) Unwraps a primitive wrapper class to its corresponding primitive type.static voidverifyType(WrapperProperty<Class<?>> typeWrapperProperty, Class<?> actualType) Verifies that the actual type matches or is compatible with the expected type.
-
Method Details
-
levenshteinDistance
-
levenshteinDistancePercent
Calculates the Levenshtein distance as percentage of the length of the longer string.- Parameters:
expected- the expected stringactual- the actual string- Returns:
- the deviation in percent (0 for two empty strings), or -1 if one of them is null
-
isNameWithinDeviation
Checks if names are within a specified deviation threshold.- Parameters:
expectedName- The expected nameactualName- The actual namethreshold- The deviation threshold in percent (0-100)- Returns:
- true if the deviation is within the threshold, false otherwise
-
safeCast
Converts a value to the given type: returns the value itself if it already is an instance, or converts between numeric types (e.g. anintresult where adoubleis expected;charcounts 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 abyte, 65 becomes'A'); afloattakes everydoublethat 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
LikesafeCast(Object, Class), but optionally permitsnull.- Parameters:
val- the value to convertcastTo- the expected typeallowNull- whethernullis 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, ignoringUNCHECKED. Used to aggregate multiple property existence states into a single worst-case state.- Parameters:
worst- the current worst existence stateupdated- the new existence state to compare- Returns:
- the worse of the two existence states
- See Also:
-
verifyType
Verifies that the actual type matches or is compatible with the expected type. Checks for exact match, primitive/wrapper equivalence (e.g.intvs.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 typeactualType- the actual type found via reflection
-
isNumericDeviation
Checks whether two types are different numeric types in either direction (e.g.longvs.int,Integervs.double).booleanand non-primitive types never qualify.- Parameters:
expected- the expected typeactual- the actual type- Returns:
- true if both unwrap to numeric primitives (byte, short, char, int, long, float, double)
-
canContain
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 checkexpectedType- the expected type to verify containment for- Returns:
- true if actualType can contain values of expectedType, false otherwise
-
unwrapPrimitive
-
toWrapperType
-