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> TUses the default configuration options for your system.static AtomicBooleanconvert2AtomicBoolean(Object fromInstance) Convert from the passed in instance to an AtomicBoolean.static AtomicIntegerconvert2AtomicInteger(Object fromInstance) Convert from the passed in instance to an AtomicInteger.static AtomicLongconvert2AtomicLong(Object fromInstance) Convert from the passed in instance to an AtomicLong.static BigDecimalconvert2BigDecimal(Object fromInstance) Convert from the passed in instance to a BigDecimal.static BigIntegerconvert2BigInteger(Object fromInstance) Convert from the passed in instance to a BigInteger.static booleanconvert2boolean(Object fromInstance) Convert from the passed in instance to a boolean.static byteconvert2byte(Object fromInstance) Convert from the passed in instance to a byte.static charconvert2char(Object fromInstance) Convert from the passed in instance to a char.static doubleconvert2double(Object fromInstance) Convert from the passed in instance to a double.static floatconvert2float(Object fromInstance) Convert from the passed in instance to a float.static intconvert2int(Object fromInstance) Convert from the passed in instance to an int.static longconvert2long(Object fromInstance) Convert from the passed in instance to an long.static shortconvert2short(Object fromInstance) Convert from the passed in instance to a short.static Stringconvert2String(Object fromInstance) Convert from the passed in instance to a String.static AtomicBooleanconvertToAtomicBoolean(Object fromInstance) Convert from the passed in instance to an AtomicBoolean.static AtomicIntegerconvertToAtomicInteger(Object fromInstance) Convert from the passed in instance to an AtomicInteger.static AtomicLongconvertToAtomicLong(Object fromInstance) Convert from the passed in instance to an AtomicLong.static BigDecimalconvertToBigDecimal(Object fromInstance) Convert from the passed in instance to a BigDecimal.static BigIntegerconvertToBigInteger(Object fromInstance) Convert from the passed in instance to a BigInteger.static BooleanconvertToBoolean(Object fromInstance) Convert from the passed in instance to a Boolean.static ByteconvertToByte(Object fromInstance) Convert from the passed in instance to a Byte.static CalendarconvertToCalendar(Object fromInstance) Convert from the passed in instance to a Calendar.static CharacterconvertToCharacter(Object fromInstance) Convert from the passed in instance to a Character.static DateconvertToDate(Object fromInstance) Convert from the passed in instance to a Date.static DoubleconvertToDouble(Object fromInstance) Convert from the passed in instance to a Double.static FloatconvertToFloat(Object fromInstance) Convert from the passed in instance to a Float.static IntegerconvertToInteger(Object fromInstance) Convert from the passed in instance to an Integer.static LocalDateconvertToLocalDate(Object fromInstance) Convert from the passed in instance to a LocalDate.static LocalDateTimeconvertToLocalDateTime(Object fromInstance) Convert from the passed in instance to a LocalDateTime.static LongconvertToLong(Object fromInstance) Convert from the passed in instance to a Long.static ShortconvertToShort(Object fromInstance) Convert from the passed in instance to a Short.static DateconvertToSqlDate(Object fromInstance) Convert from the passed in instance to a java.sql.Date.static StringconvertToString(Object fromInstance) Convert from the passed in instance to a String.static TimestampconvertToTimestamp(Object fromInstance) Convert from the passed in instance to a Timestamp.static ZonedDateTimeconvertToZonedDateTime(Object fromInstance) Convert from the passed in instance to a Date.static booleanisConversionSupportedFor(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 longlocalDateTimeToMillis(LocalDateTime localDateTime) Deprecated.static longlocalDateToMillis(LocalDate localDate) Deprecated.static longzonedDateTimeToMillis(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)
-