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.
    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)
      • convertToString

        public static String convertToString​(Object fromInstance)
      • convert2BigDecimal

        public static BigDecimal convert2BigDecimal​(Object fromInstance)
      • convertToBigDecimal

        public static BigDecimal convertToBigDecimal​(Object fromInstance)
      • convert2BigInteger

        public static BigInteger convert2BigInteger​(Object fromInstance)
      • convertToBigInteger

        public static BigInteger convertToBigInteger​(Object fromInstance)
      • convertToSqlDate

        public static Date convertToSqlDate​(Object fromInstance)
      • convertToTimestamp

        public static Timestamp convertToTimestamp​(Object fromInstance)
      • convertToDate

        public static Date convertToDate​(Object fromInstance)
      • convertToCalendar

        public static Calendar convertToCalendar​(Object fromInstance)
      • convert2Byte

        public static byte convert2Byte​(Object fromInstance)
      • convertToByte

        public static Byte convertToByte​(Object fromInstance)
      • convert2Short

        public static short convert2Short​(Object fromInstance)
      • convertToShort

        public static Short convertToShort​(Object fromInstance)
      • convert2Integer

        public static int convert2Integer​(Object fromInstance)
      • convertToInteger

        public static Integer convertToInteger​(Object fromInstance)
      • convert2Long

        public static long convert2Long​(Object fromInstance)
      • convertToLong

        public static Long convertToLong​(Object fromInstance)
      • convert2Float

        public static float convert2Float​(Object fromInstance)
      • convertToFloat

        public static Float convertToFloat​(Object fromInstance)
      • convert2Double

        public static double convert2Double​(Object fromInstance)
      • convertToDouble

        public static Double convertToDouble​(Object fromInstance)
      • convert2Boolean

        public static boolean convert2Boolean​(Object fromInstance)
      • convertToBoolean

        public static Boolean convertToBoolean​(Object fromInstance)
      • convert2AtomicInteger

        public static AtomicInteger convert2AtomicInteger​(Object fromInstance)
      • convertToAtomicInteger

        public static AtomicInteger convertToAtomicInteger​(Object fromInstance)
      • convert2AtomicLong

        public static AtomicLong convert2AtomicLong​(Object fromInstance)
      • convertToAtomicLong

        public static AtomicLong convertToAtomicLong​(Object fromInstance)
      • convert2AtomicBoolean

        public static AtomicBoolean convert2AtomicBoolean​(Object fromInstance)
      • convertToAtomicBoolean

        public static AtomicBoolean convertToAtomicBoolean​(Object fromInstance)