Package com.cedarsoftware.util
Class Converter
java.lang.Object
com.cedarsoftware.util.Converter
Useful conversion utilities. Convert from primitive to other primitives, primitives to java.time and the older
java.util.Date, TimeStamp SQL Date, and Calendar classes. Support is there for the Atomics, BigInteger, BigDecimal,
String, Map, all the Java temporal (java.time) classes, and Object[] to Collection types. In addition, you can add
your own source/target pairings, and supply the lambda that performs the conversion.
Use the 'getSupportedConversions()' API to see all conversion supported - from all sources to all destinations per each source. Close to 500 "out-of-the-box" conversions ship with the library.
The Converter can be used as statically or as an instance. See the public static methods on this Converter class to use statically. Any added conversions are added to a singleton instance maintained inside this class. Alternatively, you can instantiate the Converter class to get an instance, and the conversions you add, remove, or change will be scoped to just that instance.
On this static Convert class:
`Converter.convert2*()` methods: If `null` passed in, primitive 'logical zero' is returned. Example: `Converter.convert(null, boolean.class)` returns `false`.
`Converter.convertTo*()` methods: if `null` passed in, `null` is returned. Allows "tri-state" Boolean, for example. Example: `Converter.convert(null, Boolean.class)` returns `null`.
`Converter.convert()` converts using `convertTo*()` methods for primitive wrappers, and `convert2*()` methods for primitives.
Use the 'getSupportedConversions()' API to see all conversion supported - from all sources to all destinations per each source. Close to 500 "out-of-the-box" conversions ship with the library.
The Converter can be used as statically or as an instance. See the public static methods on this Converter class to use statically. Any added conversions are added to a singleton instance maintained inside this class. Alternatively, you can instantiate the Converter class to get an instance, and the conversions you add, remove, or change will be scoped to just that instance.
On this static Convert class:
`Converter.convert2*()` methods: If `null` passed in, primitive 'logical zero' is returned. Example: `Converter.convert(null, boolean.class)` returns `false`.
`Converter.convertTo*()` methods: if `null` passed in, `null` is returned. Allows "tri-state" Boolean, for example. Example: `Converter.convert(null, Boolean.class)` returns `null`.
`Converter.convert()` converts using `convertTo*()` methods for primitive wrappers, and `convert2*()` methods for primitives.
- 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.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionConvert<?>
addConversion
(Class<?> source, Class<?> target, Convert<?> conversionFunction) Add a new conversion.static <T> T
Uses the default configuration options for your system.static AtomicBoolean
convert2AtomicBoolean
(Object fromInstance) Convert from the passed in instance to an AtomicBoolean.static AtomicInteger
convert2AtomicInteger
(Object fromInstance) Convert from the passed in instance to an AtomicInteger.static AtomicLong
convert2AtomicLong
(Object fromInstance) Convert from the passed in instance to an AtomicLong.static BigDecimal
convert2BigDecimal
(Object fromInstance) Convert from the passed in instance to a BigDecimal.static BigInteger
convert2BigInteger
(Object fromInstance) Convert from the passed in instance to a BigInteger.static boolean
convert2boolean
(Object fromInstance) Convert from the passed in instance to a boolean.static byte
convert2byte
(Object fromInstance) Convert from the passed in instance to a byte.static char
convert2char
(Object fromInstance) Convert from the passed in instance to a char.static double
convert2double
(Object fromInstance) Convert from the passed in instance to a double.static float
convert2float
(Object fromInstance) Convert from the passed in instance to a float.static int
convert2int
(Object fromInstance) Convert from the passed in instance to an int.static long
convert2long
(Object fromInstance) Convert from the passed in instance to an long.static short
convert2short
(Object fromInstance) Convert from the passed in instance to a short.static String
convert2String
(Object fromInstance) Convert from the passed in instance to a String.static AtomicBoolean
convertToAtomicBoolean
(Object fromInstance) Convert from the passed in instance to an AtomicBoolean.static AtomicInteger
convertToAtomicInteger
(Object fromInstance) Convert from the passed in instance to an AtomicInteger.static AtomicLong
convertToAtomicLong
(Object fromInstance) Convert from the passed in instance to an AtomicLong.static BigDecimal
convertToBigDecimal
(Object fromInstance) Convert from the passed in instance to a BigDecimal.static BigInteger
convertToBigInteger
(Object fromInstance) Convert from the passed in instance to a BigInteger.static Boolean
convertToBoolean
(Object fromInstance) Convert from the passed in instance to a Boolean.static Byte
convertToByte
(Object fromInstance) Convert from the passed in instance to a Byte.static Calendar
convertToCalendar
(Object fromInstance) Convert from the passed in instance to a Calendar.static Character
convertToCharacter
(Object fromInstance) Convert from the passed in instance to a Character.static Date
convertToDate
(Object fromInstance) Convert from the passed in instance to a Date.static Double
convertToDouble
(Object fromInstance) Convert from the passed in instance to a Double.static Float
convertToFloat
(Object fromInstance) Convert from the passed in instance to a Float.static Integer
convertToInteger
(Object fromInstance) Convert from the passed in instance to an Integer.static LocalDate
convertToLocalDate
(Object fromInstance) Convert from the passed in instance to a LocalDate.static LocalDateTime
convertToLocalDateTime
(Object fromInstance) Convert from the passed in instance to a LocalDateTime.static Long
convertToLong
(Object fromInstance) Convert from the passed in instance to a Long.static Short
convertToShort
(Object fromInstance) Convert from the passed in instance to a Short.static Date
convertToSqlDate
(Object fromInstance) Convert from the passed in instance to a java.sql.Date.static String
convertToString
(Object fromInstance) Convert from the passed in instance to a String.static Timestamp
convertToTimestamp
(Object fromInstance) Convert from the passed in instance to a Timestamp.static ZonedDateTime
convertToZonedDateTime
(Object fromInstance) Convert from the passed in instance to a Date.static 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).static long
localDateTimeToMillis
(LocalDateTime localDateTime) Deprecated.static long
localDateToMillis
(LocalDate localDate) Deprecated.static long
zonedDateTimeToMillis
(ZonedDateTime zonedDateTime) Deprecated.
-
Field Details
-
instance
-
-
Method Details
-
convert
Uses the default configuration options for your system. -
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.
-
convert2String
Convert from the passed in instance to a String. If null is passed in, this method will return "". Call 'getSupportedConversions()' to see all conversion options for all Classes (all sources to all destinations). -
convertToString
Convert from the passed in instance to a String. If null is passed in, this method will return null. -
convert2BigDecimal
Convert from the passed in instance to a BigDecimal. If null or "" is passed in, this method will return a BigDecimal with the value of 0. -
convertToBigDecimal
Convert from the passed in instance to a BigDecimal. If null is passed in, this method will return null. If "" is passed in, this method will return a BigDecimal with the value of 0. -
convert2BigInteger
Convert from the passed in instance to a BigInteger. If null or "" is passed in, this method will return a BigInteger with the value of 0. -
convertToBigInteger
Convert from the passed in instance to a BigInteger. If null is passed in, this method will return null. If "" is passed in, this method will return a BigInteger with the value of 0. -
convertToSqlDate
Convert from the passed in instance to a java.sql.Date. If null is passed in, this method will return null. -
convertToTimestamp
Convert from the passed in instance to a Timestamp. If null is passed in, this method will return null. -
convertToDate
Convert from the passed in instance to a Date. If null is passed in, this method will return null. -
convertToLocalDate
Convert from the passed in instance to a LocalDate. If null is passed in, this method will return null. -
convertToLocalDateTime
Convert from the passed in instance to a LocalDateTime. If null is passed in, this method will return null. -
convertToZonedDateTime
Convert from the passed in instance to a Date. If null is passed in, this method will return null. -
convertToCalendar
Convert from the passed in instance to a Calendar. If null is passed in, this method will return null. -
convert2char
Convert from the passed in instance to a char. If null is passed in, (char) 0 is returned. -
convertToCharacter
Convert from the passed in instance to a Character. If null is passed in, null is returned. -
convert2byte
Convert from the passed in instance to a byte. If null is passed in, (byte) 0 is returned. -
convertToByte
Convert from the passed in instance to a Byte. If null is passed in, null is returned. -
convert2short
Convert from the passed in instance to a short. If null is passed in, (short) 0 is returned. -
convertToShort
Convert from the passed in instance to a Short. If null is passed in, null is returned. -
convert2int
Convert from the passed in instance to an int. If null is passed in, (int) 0 is returned. -
convertToInteger
Convert from the passed in instance to an Integer. If null is passed in, null is returned. -
convert2long
Convert from the passed in instance to an long. If null is passed in, (long) 0 is returned. -
convertToLong
Convert from the passed in instance to a Long. If null is passed in, null is returned. -
convert2float
Convert from the passed in instance to a float. If null is passed in, 0.0f is returned. -
convertToFloat
Convert from the passed in instance to a Float. If null is passed in, null is returned. -
convert2double
Convert from the passed in instance to a double. If null is passed in, 0.0d is returned. -
convertToDouble
Convert from the passed in instance to a Double. If null is passed in, null is returned. -
convert2boolean
Convert from the passed in instance to a boolean. If null is passed in, false is returned. -
convertToBoolean
Convert from the passed in instance to a Boolean. If null is passed in, null is returned. -
convert2AtomicInteger
Convert from the passed in instance to an AtomicInteger. If null is passed in, a new AtomicInteger(0) is returned. -
convertToAtomicInteger
Convert from the passed in instance to an AtomicInteger. If null is passed in, null is returned. -
convert2AtomicLong
Convert from the passed in instance to an AtomicLong. If null is passed in, new AtomicLong(0L) is returned. -
convertToAtomicLong
Convert from the passed in instance to an AtomicLong. If null is passed in, null is returned. -
convert2AtomicBoolean
Convert from the passed in instance to an AtomicBoolean. If null is passed in, new AtomicBoolean(false) is returned. -
convertToAtomicBoolean
Convert from the passed in instance to an AtomicBoolean. If null is passed in, null is returned. -
localDateToMillis
Deprecated.No longer needed - use convert(localDate, long.class)- Parameters:
localDate
- A Java LocalDate- Returns:
- a long representing the localDate as epoch milliseconds (since 1970 Jan 1 at midnight)
-
localDateTimeToMillis
Deprecated.No longer needed - use convert(localDateTime, long.class)- Parameters:
localDateTime
- A Java LocalDateTime- Returns:
- a long representing the localDateTime as epoch milliseconds (since 1970 Jan 1 at midnight)
-
zonedDateTimeToMillis
Deprecated.No longer needed - use convert(ZonedDateTime, long.class)- Parameters:
zonedDateTime
- A Java ZonedDateTime- Returns:
- a long representing the ZonedDateTime as epoch milliseconds (since 1970 Jan 1 at midnight)
-