Package onl.ycode.stormify
Class TypeUtils
java.lang.Object
onl.ycode.stormify.TypeUtils
Utility class for converting between different types and handling exceptions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <F,
T> T Convert a value to the target classstatic Class<?>
getWrapper
(Class<?> primitive) Get the boxed type class for a primitive classstatic <F,
T> Function<F, T> registerConversion
(Class<F> sourceClass, Class<T> targetClass, Function<F, T> converter) Register a conversion function from sourceClass to targetClass.
-
Method Details
-
castTo
Convert a value to the target class- Type Parameters:
F
- the source class typeT
- the target class type- Parameters:
targetClass
- the target classvalue
- the value to convert- Returns:
- the converted value
-
registerConversion
public static <F,T> Function<F,T> registerConversion(Class<F> sourceClass, Class<T> targetClass, Function<F, T> converter) Register a conversion function from sourceClass to targetClass. This function will provide custom conversion between classes, when casting objects of different types.- Type Parameters:
F
- the source class typeT
- the target class type- Parameters:
sourceClass
- the source class that needs to be convertedtargetClass
- the target class that the data should be converted toconverter
- the function that will convert the data- Returns:
- the previous conversion function if it was already registered
-
getWrapper
Get the boxed type class for a primitive class- Parameters:
primitive
- the primitive class- Returns:
- the boxed class
-