Package com.cedarsoftware.util
Class Converter
- java.lang.Object
-
- com.cedarsoftware.util.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 Summary
Fields Modifier and Type Field Description static BigDecimal
BIG_DECIMAL_ZERO
static BigInteger
BIG_INTEGER_ZERO
static Byte
BYTE_ONE
static Byte
BYTE_ZERO
static Double
DOUBLE_ONE
static Double
DOUBLE_ZERO
static Float
FLOAT_ONE
static Float
FLOAT_ZERO
static Integer
INTEGER_ONE
static Integer
INTEGER_ZERO
static Long
LONG_ONE
static Long
LONG_ZERO
static Short
SHORT_ONE
static Short
SHORT_ZERO
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
convert(Object fromInstance, Class<T> toType)
Turn the passed in value to the class indicated.static AtomicBoolean
convert2AtomicBoolean(Object fromInstance)
static AtomicInteger
convert2AtomicInteger(Object fromInstance)
static AtomicLong
convert2AtomicLong(Object fromInstance)
static BigDecimal
convert2BigDecimal(Object fromInstance)
static BigInteger
convert2BigInteger(Object fromInstance)
static boolean
convert2Boolean(Object fromInstance)
static byte
convert2Byte(Object fromInstance)
static double
convert2Double(Object fromInstance)
static float
convert2Float(Object fromInstance)
static int
convert2Integer(Object fromInstance)
static long
convert2Long(Object fromInstance)
static short
convert2Short(Object fromInstance)
static String
convert2String(Object fromInstance)
static AtomicBoolean
convertToAtomicBoolean(Object fromInstance)
static AtomicInteger
convertToAtomicInteger(Object fromInstance)
static AtomicLong
convertToAtomicLong(Object fromInstance)
static BigDecimal
convertToBigDecimal(Object fromInstance)
static BigInteger
convertToBigInteger(Object fromInstance)
static Boolean
convertToBoolean(Object fromInstance)
static Byte
convertToByte(Object fromInstance)
static Calendar
convertToCalendar(Object fromInstance)
static Date
convertToDate(Object fromInstance)
static Double
convertToDouble(Object fromInstance)
static Float
convertToFloat(Object fromInstance)
static Integer
convertToInteger(Object fromInstance)
static Long
convertToLong(Object fromInstance)
static Short
convertToShort(Object fromInstance)
static Date
convertToSqlDate(Object fromInstance)
static String
convertToString(Object fromInstance)
static Timestamp
convertToTimestamp(Object fromInstance)
-
-
-
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 targetTypetoType
- 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.
-
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)
-
convert2Byte
public static byte convert2Byte(Object fromInstance)
-
convert2Short
public static short convert2Short(Object fromInstance)
-
convert2Integer
public static int convert2Integer(Object fromInstance)
-
convert2Long
public static long convert2Long(Object fromInstance)
-
convert2Float
public static float convert2Float(Object fromInstance)
-
convert2Double
public static double convert2Double(Object fromInstance)
-
convert2Boolean
public static boolean convert2Boolean(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)
-
-