Package com.cedarsoftware.util.convert
Class Converter
java.lang.Object
com.cedarsoftware.util.convert.Converter
Instance conversion utility. Convert from primitive to other primitives, plus support for Number, Date,
TimeStamp, SQL Date, LocalDate, LocalDateTime, ZonedDateTime, Calendar, Big*, Atomic*, Class, UUID,
String, ... Additional conversions can be added by specifying source class, destination class, and
a lambda function that performs the conversion.
Currently, there are nearly 500 built-in conversions. Use the getSupportedConversions() API to see all source to target conversions.
The main API is convert(value, class). if null passed in, null is returned for most types, which allows "tri-state" Boolean, for example, however, for primitive types, it chooses zero for the numeric ones, `false` for boolean, and 0 for char.
A Map can be converted to almost all JDK "data" classes. For example, UUID can be converted to/from a Map. It is expected for the Map to have certain keys ("mostSigBits", "leastSigBits"). For the older Java Date/Time related classes, it expects "time" or "nanos", and for all others, a Map as the source, the "value" key will be used to source the value for the conversion.
Currently, there are nearly 500 built-in conversions. Use the getSupportedConversions() API to see all source to target conversions.
The main API is convert(value, class). if null passed in, null is returned for most types, which allows "tri-state" Boolean, for example, however, for primitive types, it chooses zero for the numeric ones, `false` for boolean, and 0 for char.
A Map can be converted to almost all JDK "data" classes. For example, UUID can be converted to/from a Map. It is expected for the Map to have certain keys ("mostSigBits", "leastSigBits"). For the older Java Date/Time related classes, it expects "time" or "nanos", and for all others, a Map as the source, the "value" key will be used to source the value for the conversion.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConvert<?>
addConversion
(Class<?> source, Class<?> target, Convert<?> conversionFunction) Add a new conversion.<T> T
Turn the passed in value to the class indicated.boolean
isConversionSupportedFor
(Class<?> source, Class<?> target) Check to see if a conversion from type to another type is supported (may use inheritance via super classes/interfaces).
-
Constructor Details
-
Converter
-
-
Method Details
-
getOptions
-
convert
Turn the passed in value to the class indicated. This will allow, for example, a String to be passed in and be converted to a Long.Examples: Long x = convert("35", Long.class); Date d = convert("2015/01/01", Date.class) int y = convert(45.0, int.class) String date = convert(date, String.class) String date = convert(calendar, String.class) Short t = convert(true, short.class); // returns (short) 1 or (short) 0 Long date = convert(calendar, long.class); // get calendar's time into long Map containing ["_v": "75.0"] convert(map, double.class) // Converter will extract the value associated to the "_v" (or "value") key and convert it.
- Parameters:
from
- A value used to create the targetType, even though it may not (most likely will not) be the same data type as the targetTypetoType
- Class which indicates the targeted (final) data type. Please note that in addition to the 8 Java primitives, the targeted class can also be Date.class, String.class, BigInteger.class, BigDecimal.class, and many other JDK classes, including Map. For Map, often it will seek a 'value' field, however, for some complex objects, like UUID, it will look for specific fields within the Map to perform the conversion.- Returns:
- An instanceof targetType class, based upon the value passed in.
- See Also:
-
isConversionSupportedFor
Check to see if a conversion from type to another type is supported (may use inheritance via super classes/interfaces).- Parameters:
source
- Class of source type.target
- Class of target type.- Returns:
- boolean true if the Converter converts from the source type to the destination type, false otherwise.
-
allSupportedConversions
- Returns:
Map<Class, Set<Class>>
which contains all supported conversions. The key of the Map is a source class, and the Set contains all the target types (classes) that the source can be converted to.
-
getSupportedConversions
- Returns:
Map<String, Set<String>>
which contains all supported conversions. The key of the Map is a source class name, and the Set contains all the target class names that the source can be converted to.
-
addConversion
Add a new conversion.- Parameters:
source
- Class to convert from.target
- Class to convert to.conversionFunction
- Convert function that converts from the source type to the destination type.- Returns:
- prior conversion function if one existed.
-