Package org.pipservices3.commons.convert
Class TypeConverter
java.lang.Object
org.pipservices3.commons.convert.TypeConverter
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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.static String
Converts a TypeCode into its string name.static <T> T
Converts value into an object type specified by Type Code or returns type default when conversion is not possible.static TypeCode
toTypeCode
(Class<?> type) Gets TypeCode for specific type.static TypeCode
toTypeCode
(Object value) Gets TypeCode for specific value.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.
-
Constructor Details
-
TypeConverter
public TypeConverter()
-
-
Method Details
-
toTypeCode
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
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
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
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
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
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.
-