Class TypeConverter

java.lang.Object
org.pipservices3.commons.convert.TypeConverter

public class TypeConverter extends Object
Converts arbitrary values into objects specific by TypeCodes. For each TypeCode this class calls corresponding converter which applies extended conversion rules to convert the values.

### Example ###

 
 int value1 = TypeConverter.toType(TypeCode.Integer, "123.456"); // Result: 123
 ZonedDateTime value2 = TypeConverter.toType(TypeCode.DateTime, 123); // Result: ZonedDateTime(123)
 boolean value3 = TypeConverter.toType(TypeCode.Boolean, "F"); // Result: false
 
 
See Also:
  • Constructor Details

    • TypeConverter

      public TypeConverter()
  • Method Details

    • toTypeCode

      public static TypeCode toTypeCode(Class<?> type)
      Gets TypeCode for specific type.
      Parameters:
      type - the Class type for the data type.
      Returns:
      the TypeCode that corresponds to the passed object's type.
    • toTypeCode

      public static TypeCode toTypeCode(Object value)
      Gets TypeCode for specific value.
      Parameters:
      value - value whose TypeCode is to be resolved.
      Returns:
      the TypeCode that corresponds to the passed object's type.
    • toNullableType

      public static <T> T toNullableType(Class<T> type, Object value)
      Converts value into an object type specified by Type Code or returns null when conversion is not possible.
      Parameters:
      type - the Class type for the data type.
      value - the value to convert.
      Returns:
      object value of type corresponding to TypeCode, or null when conversion is not supported.
      See Also:
    • toType

      public static <T> T toType(Class<T> type, Object value)
      Converts value into an object type specified by Type Code or returns type default when conversion is not possible.
      Parameters:
      type - the Class type for the data type into which 'value' is to be converted.
      value - the value to convert.
      Returns:
      object value of type corresponding to TypeCode, or type default when conversion is not supported.
      See Also:
    • toTypeWithDefault

      public static <T> T toTypeWithDefault(Class<T> type, Object value, T defaultValue)
      Converts value into an object type specified by Type Code or returns default value when conversion is not possible.
      Parameters:
      type - the Class type for the data type into which 'value' is to be converted.
      value - the value to convert.
      defaultValue - the default value to return if conversion is not possible (returns null).
      Returns:
      object value of type corresponding to TypeCode, or default value when conversion is not supported.
      See Also:
    • toString

      public static String toString(TypeCode type)
      Converts a TypeCode into its string name.
      Parameters:
      type - the TypeCode to convert into a string.
      Returns:
      the name of the TypeCode passed as a string value.