Package org.apache.juneau.internal
Class ConverterUtils
java.lang.Object
org.apache.juneau.internal.ConverterUtils
Utility class for efficiently converting objects between types.
If the value isn't an instance of the specified type, then converts the value if possible.
The following conversions are valid:
| Convert to type | Valid input value types | Notes |
|---|---|---|
A class that is the normal type of a registered ObjectSwap.
|
A value whose class matches the transformed type of that registered ObjectSwap.
|
|
A class that is the transformed type of a registered ObjectSwap.
|
A value whose class matches the normal type of that registered ObjectSwap.
|
|
Number (e.g. Integer, Short, Float,...)
Number. (e.g. Integer.,
Short., Float.,...)
|
Number, String, |
For primitive TYPES, |
Map (e.g. Map, HashMap, TreeMap, JsonMap)
|
Map
|
If Map is not constructible, an JsonMap is created.
|
|
|
|
If |
|
|
|
|
|
|
|
|
|
|
|
|
| Bean |
|
|
|
|
Anything | Arrays are converted to JSON arrays |
Anything with one of the following methods:
|
|
|
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BooleanConverts an object to a Boolean.static IntegerConverts an object to an Integer.static <T> Lists<T>toListBuilder(Object value, Class<T> type) Converts the specified object to aListswith elements of the specified type.static <K,V> Maps<K, V> toMapBuilder(Object value, Class<K> keyType, Class<V> valueType) Converts the specified object to aMapswith keys and values of the specified types.static NumberConverts an object to a Number.static <T> Sets<T>toSetBuilder(Object value, Class<T> type) Converts the specified object to aSetswith elements of the specified type.static <T> TConverts the specified object to the specified type.
-
Constructor Details
-
ConverterUtils
public ConverterUtils()
-
-
Method Details
-
toBoolean
Converts an object to a Boolean.- Parameters:
o- The object to convert.- Returns:
- The converted object.
-
toInteger
Converts an object to an Integer.- Parameters:
o- The object to convert.- Returns:
- The converted object.
-
toNumber
Converts an object to a Number.- Parameters:
o- The object to convert.- Returns:
- The converted object.
-
toType
Converts the specified object to the specified type.- Type Parameters:
T- The class type to convert the value to.- Parameters:
value- The value to convert.type- The class type to convert the value to.- Returns:
- The converted value.
- Throws:
InvalidDataConversionException- If the specified value cannot be converted to the specified type.
-
toListBuilder
Converts the specified object to aListswith elements of the specified type.The input value can be any of the following:
- An array of objects convertible to the element type
- A
Collectionof objects convertible to the element type - A single object convertible to the element type (creates a list with one element)
- A JSON array string that can be parsed into objects of the element type
- Type Parameters:
T- The element type.- Parameters:
value- The value to convert. Can benull .type- The element type class.- Returns:
- A new
Listscontaining the converted elements.
-
toMapBuilder
Converts the specified object to aMapswith keys and values of the specified types.The input value can be any of the following:
- A
Mapwith entries convertible to the key/value types - A JSON object string that can be parsed into a map with the specified key/value types
- An object with bean properties that can be converted to map entries
- Type Parameters:
K- The key type.V- The value type.- Parameters:
value- The value to convert. Can benull .keyType- The key type class.valueType- The value type class.- Returns:
- A new
Mapscontaining the converted entries.
- A
-
toSetBuilder
Converts the specified object to aSetswith elements of the specified type.The input value can be any of the following:
- An array of objects convertible to the element type
- A
Collectionof objects convertible to the element type - A single object convertible to the element type (creates a set with one element)
- A JSON array string that can be parsed into objects of the element type
Duplicate elements (after conversion) will be automatically removed as per
Setsemantics.- Type Parameters:
T- The element type.- Parameters:
value- The value to convert. Can benull .type- The element type class.- Returns:
- A new
Setscontaining the converted elements.
-