Class Converter


  • public final class Converter
    extends Object
    Handy conversion utilities. Convert from primitive to other primitives, plus support for Date, TimeStamp SQL Date, and the Atomic's. `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. 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

    http://www.apache.org/licenses/LICENSE-2.0

    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 Detail

      • BYTE_ZERO

        public static final Byte BYTE_ZERO
      • BYTE_ONE

        public static final Byte BYTE_ONE
      • SHORT_ZERO

        public static final Short SHORT_ZERO
      • SHORT_ONE

        public static final Short SHORT_ONE
      • INTEGER_ZERO

        public static final Integer INTEGER_ZERO
      • INTEGER_ONE

        public static final Integer INTEGER_ONE
      • LONG_ZERO

        public static final Long LONG_ZERO
      • LONG_ONE

        public static final Long LONG_ONE
      • FLOAT_ZERO

        public static final Float FLOAT_ZERO
      • FLOAT_ONE

        public static final Float FLOAT_ONE
      • DOUBLE_ZERO

        public static final Double DOUBLE_ZERO
      • DOUBLE_ONE

        public static final Double DOUBLE_ONE
      • BIG_DECIMAL_ZERO

        public static final BigDecimal BIG_DECIMAL_ZERO
      • BIG_INTEGER_ZERO

        public static final BigInteger BIG_INTEGER_ZERO
    • Method Detail

      • convert

        public static <T> T convert​(Object fromInstance,
                                    Class<T> toType)
        Turn the passed in value to the class indicated. This will allow, for example, a String value to be passed in and have it coerced 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
         
        Parameters:
        fromInstance - A value used to create the targetType, even though it may not (most likely will not) be the same data type as the targetType
        toType - 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 the Atomic classes. The primitive class can be either primitive class or primitive wrapper class, however, the returned value will always [obviously] be a primitive wrapper.
        Returns:
        An instanceof targetType class, based upon the value passed in.
      • 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 "". Possible inputs are any primitive or primitive wrapper, Date (returns ISO-DATE format: 2020-04-10T12:15:47), Calendar (returns ISO-DATE format: 2020-04-10T12:15:47), any Enum (returns Enum's name()), BigDecimal, BigInteger, AtomicBoolean, AtomicInteger, AtomicLong, and Character.
      • 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. Possible inputs are any primitive/primitive wrapper, Date (returns ISO-DATE format: 2020-04-10T12:15:47), Calendar (returns ISO-DATE format: 2020-04-10T12:15:47), any Enum (returns Enum's name()), BigDecimal, BigInteger, AtomicBoolean, AtomicInteger, AtomicLong, and Character.
      • 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. Possible inputs are String (base10 numeric values in string), BigInteger, any primitive/primitive wrapper, Boolean/AtomicBoolean (returns BigDecimal of 0 or 1), Date/Calendar (returns BigDecimal with the value of number of milliseconds since Jan 1, 1970), and Character (returns integer value of character).
      • 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. Possible inputs are String (base10 numeric values in string), BigInteger, any primitive/primitive wrapper, Boolean/AtomicBoolean (returns BigDecimal of 0 or 1), Date/Calendar (returns BigDecimal with the value of number of milliseconds since Jan 1, 1970), and Character (returns integer value of character).
      • 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. Possible inputs are String (base10 numeric values in string), BigDecimal, any primitive/primitive wrapper, Boolean/AtomicBoolean (returns BigDecimal of 0 or 1), Date/Calendar (returns BigDecimal with the value of number of milliseconds since Jan 1, 1970), and Character (returns integer value of character).
      • 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. Possible inputs are String (base10 numeric values in string), BigDecimal, any primitive/primitive wrapper, Boolean/AtomicBoolean (returns BigInteger of 0 or 1), Date/Calendar (returns BigInteger with the value of number of milliseconds since Jan 1, 1970), and Character (returns integer value of character).
      • 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. Possible inputs are TimeStamp, Date, Calendar, java.sql.Date (will return a copy), String (which will be parsed by DateUtilities into a Date and a java.sql.Date will created from that), Long, BigInteger, BigDecimal, and AtomicLong (all of which the java.sql.Date will be created directly from [number of milliseconds since Jan 1, 1970]).
      • 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. Possible inputs are java.sql.Date, Date, Calendar, TimeStamp (will return a copy), String (which will be parsed by DateUtilities into a Date and a Timestamp will created from that), Long, BigInteger, BigDecimal, and AtomicLong (all of which the Timestamp will be created directly from [number of milliseconds since Jan 1, 1970]).
      • 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. Possible inputs are java.sql.Date, Timestamp, Calendar, Date (will return a copy), String (which will be parsed by DateUtilities and returned as a new Date instance), Long, BigInteger, BigDecimal, and AtomicLong (all of which the Date will be created directly from [number of milliseconds since Jan 1, 1970]).
      • 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. Possible inputs are java.sql.Date, Timestamp, Date, Calendar (will return a copy), String (which will be parsed by DateUtilities and returned as a new Date instance), Long, BigInteger, BigDecimal, and AtomicLong (all of which the Date will be created directly from [number of milliseconds since Jan 1, 1970]).
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convertToCharacter

        public static Character convertToCharacter​(Object fromInstance)
        Convert from the passed in instance to a Character. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convertToByte

        public static Byte convertToByte​(Object fromInstance)
        Convert from the passed in instance to a Byte. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convertToShort

        public static Short convertToShort​(Object fromInstance)
        Convert from the passed in instance to a Short. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convertToInteger

        public static Integer convertToInteger​(Object fromInstance)
        Convert from the passed in instance to an Integer. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s. In addition, Date, java.sql.Date, Timestamp, and Calendar can be passed in, in which case the long returned is the number of milliseconds since Jan 1, 1970.
      • convertToLong

        public static Long convertToLong​(Object fromInstance)
        Convert from the passed in instance to a Long. If null is passed in, (Long) 0 is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s. In addition, Date, java.sql.Date, Timestamp, and Calendar can be passed in, in which case the long returned is the number of milliseconds since Jan 1, 1970.
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convertToFloat

        public static Float convertToFloat​(Object fromInstance)
        Convert from the passed in instance to a Float. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convertToDouble

        public static Double convertToDouble​(Object fromInstance)
        Convert from the passed in instance to a Double. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convert2boolean

        public static boolean convert2boolean​(Object fromInstance)
        Convert from the passed in instance to a boolean. If null is passed in, false is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convertToBoolean

        public static Boolean convertToBoolean​(Object fromInstance)
        Convert from the passed in instance to a Boolean. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convertToAtomicInteger

        public static AtomicInteger convertToAtomicInteger​(Object fromInstance)
        Convert from the passed in instance to an AtomicInteger. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s. In addition, Date, java.sql.Date, Timestamp, and Calendar can be passed in, in which case the AtomicLong returned is the number of milliseconds since Jan 1, 1970.
      • convertToAtomicLong

        public static AtomicLong convertToAtomicLong​(Object fromInstance)
        Convert from the passed in instance to an AtomicLong. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s. In addition, Date, java.sql.Date, Timestamp, and Calendar can be passed in, in which case the AtomicLong returned is the number of milliseconds since Jan 1, 1970.
      • 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. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.
      • convertToAtomicBoolean

        public static AtomicBoolean convertToAtomicBoolean​(Object fromInstance)
        Convert from the passed in instance to an AtomicBoolean. If null is passed in, null is returned. Possible inputs are String, all primitive/primitive wrappers, boolean, AtomicBoolean, (false=0, true=1), and all Atomic*s.