Class Converter

java.lang.Object
com.cedarsoftware.util.Converter

public final class Converter extends Object
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.

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 Details

    • instance

      public static Converter instance
  • Method Details

    • convert

      public static <T> T convert(Object fromInstance, Class<T> toType)
      Uses the default configuration options for your system.
    • isConversionSupportedFor

      public 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).
      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

      public static Map<Class<?>,Set<Class<?>>> 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

      public static Map<String,Set<String>> 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

      public Convert<?> addConversion(Class<?> source, Class<?> target, Convert<?> conversionFunction)
      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

      public static String convert2String(Object fromInstance)
      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

      public static String convertToString(Object fromInstance)
      Convert from the passed in instance to a String. If null is passed in, this method will return null.
    • convert2BigDecimal

      public static BigDecimal convert2BigDecimal(Object fromInstance)
      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

      public static BigDecimal convertToBigDecimal(Object fromInstance)
      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

      public static BigInteger convert2BigInteger(Object fromInstance)
      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

      public static BigInteger convertToBigInteger(Object fromInstance)
      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

      public static Date convertToSqlDate(Object fromInstance)
      Convert from the passed in instance to a java.sql.Date. If null is passed in, this method will return null.
    • convertToTimestamp

      public static Timestamp convertToTimestamp(Object fromInstance)
      Convert from the passed in instance to a Timestamp. If null is passed in, this method will return null.
    • convertToDate

      public static Date convertToDate(Object fromInstance)
      Convert from the passed in instance to a Date. If null is passed in, this method will return null.
    • convertToLocalDate

      public static LocalDate convertToLocalDate(Object fromInstance)
      Convert from the passed in instance to a LocalDate. If null is passed in, this method will return null.
    • convertToLocalDateTime

      public static LocalDateTime convertToLocalDateTime(Object fromInstance)
      Convert from the passed in instance to a LocalDateTime. If null is passed in, this method will return null.
    • convertToZonedDateTime

      public static ZonedDateTime convertToZonedDateTime(Object fromInstance)
      Convert from the passed in instance to a Date. If null is passed in, this method will return null.
    • convertToCalendar

      public static Calendar convertToCalendar(Object fromInstance)
      Convert from the passed in instance to a Calendar. If null is passed in, this method will return null.
    • convert2char

      public static char convert2char(Object fromInstance)
      Convert from the passed in instance to a char. If null is passed in, (char) 0 is returned.
    • convertToCharacter

      public static Character convertToCharacter(Object fromInstance)
      Convert from the passed in instance to a Character. If null is passed in, null is returned.
    • convert2byte

      public static byte convert2byte(Object fromInstance)
      Convert from the passed in instance to a byte. If null is passed in, (byte) 0 is returned.
    • convertToByte

      public static Byte convertToByte(Object fromInstance)
      Convert from the passed in instance to a Byte. If null is passed in, null is returned.
    • convert2short

      public static short convert2short(Object fromInstance)
      Convert from the passed in instance to a short. If null is passed in, (short) 0 is returned.
    • convertToShort

      public static Short convertToShort(Object fromInstance)
      Convert from the passed in instance to a Short. If null is passed in, null is returned.
    • convert2int

      public static int convert2int(Object fromInstance)
      Convert from the passed in instance to an int. If null is passed in, (int) 0 is returned.
    • convertToInteger

      public static Integer convertToInteger(Object fromInstance)
      Convert from the passed in instance to an Integer. If null is passed in, null is returned.
    • convert2long

      public static long convert2long(Object fromInstance)
      Convert from the passed in instance to an long. If null is passed in, (long) 0 is returned.
    • convertToLong

      public static Long convertToLong(Object fromInstance)
      Convert from the passed in instance to a Long. If null is passed in, null is returned.
    • convert2float

      public static float convert2float(Object fromInstance)
      Convert from the passed in instance to a float. If null is passed in, 0.0f is returned.
    • convertToFloat

      public static Float convertToFloat(Object fromInstance)
      Convert from the passed in instance to a Float. If null is passed in, null is returned.
    • convert2double

      public static double convert2double(Object fromInstance)
      Convert from the passed in instance to a double. If null is passed in, 0.0d is returned.
    • convertToDouble

      public static Double convertToDouble(Object fromInstance)
      Convert from the passed in instance to a Double. If null is passed in, null is returned.
    • convert2boolean

      public static boolean convert2boolean(Object fromInstance)
      Convert from the passed in instance to a boolean. If null is passed in, false is returned.
    • convertToBoolean

      public static Boolean convertToBoolean(Object fromInstance)
      Convert from the passed in instance to a Boolean. If null is passed in, null is returned.
    • convert2AtomicInteger

      public static AtomicInteger convert2AtomicInteger(Object fromInstance)
      Convert from the passed in instance to an AtomicInteger. If null is passed in, a new AtomicInteger(0) is returned.
    • convertToAtomicInteger

      public static AtomicInteger convertToAtomicInteger(Object fromInstance)
      Convert from the passed in instance to an AtomicInteger. If null is passed in, null is returned.
    • convert2AtomicLong

      public static AtomicLong convert2AtomicLong(Object fromInstance)
      Convert from the passed in instance to an AtomicLong. If null is passed in, new AtomicLong(0L) is returned.
    • convertToAtomicLong

      public static AtomicLong convertToAtomicLong(Object fromInstance)
      Convert from the passed in instance to an AtomicLong. If null is passed in, null is returned.
    • convert2AtomicBoolean

      public static AtomicBoolean convert2AtomicBoolean(Object fromInstance)
      Convert from the passed in instance to an AtomicBoolean. If null is passed in, new AtomicBoolean(false) is returned.
    • convertToAtomicBoolean

      public static AtomicBoolean convertToAtomicBoolean(Object fromInstance)
      Convert from the passed in instance to an AtomicBoolean. If null is passed in, null is returned.
    • localDateToMillis

      @Deprecated public static long localDateToMillis(LocalDate localDate)
      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 public static long localDateTimeToMillis(LocalDateTime localDateTime)
      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 public static long zonedDateTimeToMillis(ZonedDateTime zonedDateTime)
      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)