Class ConverterUtils

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

public final 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
See Also:
  • Constructor Details

  • Method Details

    • 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.
    • toType

      public static <T> T toType(Object value, Class<T> type, Type... args)
      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.
      args - The type arguments.
      Returns:
      The converted value.
      Throws:
      InvalidDataConversionException - If the specified value cannot be converted to the specified type.
    • 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.