public class TypeConverter extends Object
| Modifier and Type | Method and Description |
|---|---|
static <E> E[] |
arrayOf(Object[] array,
E[] typeIndicator,
Class<E> componentType)
Converts an array of objects to an array of a specified type.
|
static <E> E[] |
arrayOf(Object[] array,
IntFunction<E[]> generator,
Class<E> componentType)
Converts an array of objects to an array of a specified type using a generator function.
|
static <E> E[] |
arrayOf(Object object,
E[] typeIndicator,
Class<E> componentType)
Converts an object to an array of a specified type.
|
static <E> E[] |
arrayOf(Object object,
IntFunction<E[]> generator,
Class<E> componentType)
Converts an object to an array of a specified type using a generator function.
|
static <T extends Collection<E>,E> |
collectionOf(Object input,
Supplier<? extends T> output,
Class<E> itemType)
Creates a collection of a specific typeId containing elements of a specific typeId based on the given input.
|
static <T> T |
convertObj(Object value,
Class<T> targetType)
Safely converts an object to the specified target typeId.
|
static <T extends Enum<T>> |
enumOf(String value,
Class<T> enumType)
Converts a string value to an enum of a specified type.
|
static <K,V,M extends Map<K,V>> |
mapOf(Map<?,?> input,
Supplier<M> output,
Class<K> keyType,
Class<V> valueType)
Creates a new map of typeId
M from the given input map. |
public static <T> T convertObj(Object value, Class<T> targetType)
This method provides a way to convert between common types such as String, Boolean, LocalDateTime, Numbers, Collections, and Maps. If the value is already of the target typeId, it will simply be cast and returned. Otherwise, the method attempts to safely convert the value to the desired typeId.
String, the method converts the value using toString().Boolean and the value is a string representation of a boolean, the method converts it using Boolean.valueOf().LocalDateTime and the value is a Date, it converts it to LocalDateTime.Byte and the value is a number, it casts it to byte.Number and the value is a number, it invokes numberOf to convert it.Collection or Map, and the value is already of that typeId, it will simply be cast and returned.T - The target typeId to convert the value to.value - The object value to convert.targetType - The class of the target typeId.T, or null if the conversion is not supported.public static <K,V,M extends Map<K,V>> M mapOf(Map<?,?> input, Supplier<M> output, Class<K> keyType, Class<V> valueType)
M from the given input map. The keys and values are converted
to types K and V respectively.K - The typeId of the keys in the output map.V - The typeId of the values in the output map.M - The typeId of the output map, which must be a subclass of Map<K, V>.input - The input map containing keys and values to be converted.output - A Supplier for the output map of typeId M.keyType - The Class object representing the typeId of key to convert to.valueType - The Class object representing the typeId of value to convert to.M with keys and values converted to types K and V. Returns null if the output map is null.public static <T extends Collection<E>,E> T collectionOf(Object input, Supplier<? extends T> output, Class<E> itemType)
T - Type of the target collection.E - Type of the elements in the target collection.input - The input object, which can be an array or a collection.output - A supplier for creating an empty collection of typeId T.itemType - The class typeId of the elements to be contained in the output collection.The method handles three scenarios: 1. If the input is already a collection, it converts it to a collection of typeId T containing elements of typeId E. 2. If the input is an array, it converts it to a collection of typeId T containing elements of typeId E. 3. If the input is a single object, it converts it to typeId E and returns a collection of typeId T containing that single element.
public static <E> E[] arrayOf(Object object, E[] typeIndicator, Class<E> componentType)
E - The component type of the array.object - The object to be converted.typeIndicator - An array instance indicating the type of array to return.componentType - The class of the array's component type.public static <E> E[] arrayOf(Object object, IntFunction<E[]> generator, Class<E> componentType)
E - The component type of the array.object - The object to be converted.generator - A function to generate the array of the required size.componentType - The class of the array's component type.public static <E> E[] arrayOf(Object[] array, E[] typeIndicator, Class<E> componentType)
E - The component type of the target array.array - The array to be converted.typeIndicator - An array instance indicating the type of array to return.componentType - The class of the array's component type.public static <E> E[] arrayOf(Object[] array, IntFunction<E[]> generator, Class<E> componentType)
E - The component type of the target array.array - The array to be converted.generator - A function to generate the array of the required size.componentType - The class of the array's component type.public static <T extends Enum<T>> T enumOf(String value, Class<T> enumType)
T - The enum type to which the string is to be converted.value - The string value to be converted to an enum constant.enumType - The class of the enum type.Copyright © 2023. All rights reserved.