Class ConverterUtils

java.lang.Object
org.apache.juneau.internal.ConverterUtils

public class ConverterUtils extends Object
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 typeValid input value typesNotes
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.TYPE (e.g. Integer.TYPE, Short.TYPE, Float.TYPE,...)
Number, String, null For primitive TYPES, null returns the JVM default value for that type.
Map (e.g. Map, HashMap, TreeMap, JsonMap) Map If Map is not constructible, an JsonMap is created.
Collection (e.g. List, LinkedList, HashSet, JsonList) Collection<Object>
Object[]
If Collection is not constructible, a JsonList is created.
X[] (array of any type X) List<X>  
X[][] (multi-dimensional arrays) List<List<X>>
List<X[]>
List[]<X>
 
Enum String  
Bean Map  
String Anything Arrays are converted to JSON arrays
Anything with one of the following methods:
public static T fromString(String)
public static T valueOf(String)
public T(String)
String
  • Constructor Details

  • Method Details

    • toBoolean

      public static Boolean toBoolean(Object o)
      Converts an object to a Boolean.
      Parameters:
      o - The object to convert.
      Returns:
      The converted object.
    • toInteger

      public static Integer toInteger(Object o)
      Converts an object to an Integer.
      Parameters:
      o - The object to convert.
      Returns:
      The converted object.
    • toNumber

      public static Number toNumber(Object o)
      Converts an object to a Number.
      Parameters:
      o - The object to convert.
      Returns:
      The converted object.
    • toType

      public static <T> T toType(Object value, Class<T> type)
      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

      public static <T> Lists<T> toListBuilder(Object value, Class<T> type)
      Converts the specified object to a Lists with elements of the specified type.

      The input value can be any of the following:

      • An array of objects convertible to the element type
      • A Collection of 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 be null.
      type - The element type class.
      Returns:
      A new Lists containing the converted elements.
    • toMapBuilder

      public static <K, V> Maps<K,V> toMapBuilder(Object value, Class<K> keyType, Class<V> valueType)
      Converts the specified object to a Maps with keys and values of the specified types.

      The input value can be any of the following:

      • A Map with 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 be null.
      keyType - The key type class.
      valueType - The value type class.
      Returns:
      A new Maps containing the converted entries.
    • toSetBuilder

      public static <T> Sets<T> toSetBuilder(Object value, Class<T> type)
      Converts the specified object to a Sets with elements of the specified type.

      The input value can be any of the following:

      • An array of objects convertible to the element type
      • A Collection of 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 Set semantics.

      Type Parameters:
      T - The element type.
      Parameters:
      value - The value to convert. Can be null.
      type - The element type class.
      Returns:
      A new Sets containing the converted elements.