Package com.cedarsoftware.util
Class Converter
java.lang.Object
com.cedarsoftware.util.Converter
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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BigDecimal
static final BigInteger
static final Byte
static final Byte
static final Double
static final Double
static final Float
static final Float
static final Integer
static final Integer
static final Long
static final Long
static final Short
static final Short
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Turn the passed in value to the class indicated.static AtomicBoolean
convert2AtomicBoolean
(Object fromInstance) Convert from the passed in instance to an AtomicBoolean.static AtomicInteger
convert2AtomicInteger
(Object fromInstance) Convert from the passed in instance to an AtomicInteger.static AtomicLong
convert2AtomicLong
(Object fromInstance) Convert from the passed in instance to an AtomicLong.static BigDecimal
convert2BigDecimal
(Object fromInstance) Convert from the passed in instance to a BigDecimal.static BigInteger
convert2BigInteger
(Object fromInstance) Convert from the passed in instance to a BigInteger.static boolean
convert2boolean
(Object fromInstance) Convert from the passed in instance to a boolean.static byte
convert2byte
(Object fromInstance) Convert from the passed in instance to a byte.static char
convert2char
(Object fromInstance) Convert from the passed in instance to a char.static double
convert2double
(Object fromInstance) Convert from the passed in instance to a double.static float
convert2float
(Object fromInstance) Convert from the passed in instance to a float.static int
convert2int
(Object fromInstance) Convert from the passed in instance to an int.static long
convert2long
(Object fromInstance) Convert from the passed in instance to an long.static short
convert2short
(Object fromInstance) Convert from the passed in instance to a short.static String
convert2String
(Object fromInstance) Convert from the passed in instance to a String.static AtomicBoolean
convertToAtomicBoolean
(Object fromInstance) Convert from the passed in instance to an AtomicBoolean.static AtomicInteger
convertToAtomicInteger
(Object fromInstance) Convert from the passed in instance to an AtomicInteger.static AtomicLong
convertToAtomicLong
(Object fromInstance) Convert from the passed in instance to an AtomicLong.static BigDecimal
convertToBigDecimal
(Object fromInstance) Convert from the passed in instance to a BigDecimal.static BigInteger
convertToBigInteger
(Object fromInstance) Convert from the passed in instance to a BigInteger.static Boolean
convertToBoolean
(Object fromInstance) Convert from the passed in instance to a Boolean.static Byte
convertToByte
(Object fromInstance) Convert from the passed in instance to a Byte.static Calendar
convertToCalendar
(Object fromInstance) Convert from the passed in instance to a Calendar.static Character
convertToCharacter
(Object fromInstance) Convert from the passed in instance to a Character.static Date
convertToDate
(Object fromInstance) Convert from the passed in instance to a Date.static Double
convertToDouble
(Object fromInstance) Convert from the passed in instance to a Double.static Float
convertToFloat
(Object fromInstance) Convert from the passed in instance to a Float.static Integer
convertToInteger
(Object fromInstance) Convert from the passed in instance to an Integer.static LocalDate
convertToLocalDate
(Object fromInstance) static LocalDateTime
convertToLocalDateTime
(Object fromInstance) static Long
convertToLong
(Object fromInstance) Convert from the passed in instance to a Long.static Short
convertToShort
(Object fromInstance) Convert from the passed in instance to a Short.static Date
convertToSqlDate
(Object fromInstance) Convert from the passed in instance to a java.sql.Date.static String
convertToString
(Object fromInstance) Convert from the passed in instance to a String.static Timestamp
convertToTimestamp
(Object fromInstance) Convert from the passed in instance to a Timestamp.static ZonedDateTime
convertToZonedDateTime
(Object fromInstance) static long
localDateTimeToMillis
(LocalDateTime localDateTime) static long
localDateToMillis
(LocalDate localDate) static long
zonedDateTimeToMillis
(ZonedDateTime zonedDateTime)
-
Field Details
-
BYTE_ZERO
-
BYTE_ONE
-
SHORT_ZERO
-
SHORT_ONE
-
INTEGER_ZERO
-
INTEGER_ONE
-
LONG_ZERO
-
LONG_ONE
-
FLOAT_ZERO
-
FLOAT_ONE
-
DOUBLE_ZERO
-
DOUBLE_ONE
-
BIG_DECIMAL_ZERO
-
BIG_INTEGER_ZERO
-
-
Method Details
-
convert
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.
-
convert2String
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
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
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
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, LocalDate, LocalDateTime, ZonedDateTime (returns BigDecimal with the value of number of milliseconds since Jan 1, 1970), and Character (returns integer value of character). -
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. 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
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, LocalDate, LocalDateTime, ZonedDateTime (returns BigInteger with the value of number of milliseconds since Jan 1, 1970), and Character (returns integer value of character). -
convertToSqlDate
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), LocalDate, LocalDateTime, ZonedDateTime, 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
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, LocalDate, LocalDateTime, ZonedDateTime, 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
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]). -
convertToLocalDate
-
convertToLocalDateTime
-
convertToZonedDateTime
-
convertToCalendar
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
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
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
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
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
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
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
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
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
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, LocalDate, LocalDateTime, ZonedDateTime, 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
Convert from the passed in instance to a Long. 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, LocalDate, LocalDateTime, ZonedDateTime, 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
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
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
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
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
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
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
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
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
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, LocalDate, LocalDateTime, ZonedDateTime, 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
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, LocalDate, LocalDateTime, ZonedDateTime, 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
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
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. -
localDateToMillis
- Parameters:
localDate
- A Java LocalDate- Returns:
- a long representing the localDate as the number of milliseconds since the number of milliseconds since Jan 1, 1970
-
localDateTimeToMillis
- Parameters:
localDateTime
- A Java LocalDateTime- Returns:
- a long representing the localDateTime as the number of milliseconds since the number of milliseconds since Jan 1, 1970
-
zonedDateTimeToMillis
- Parameters:
zonedDateTime
- A Java ZonedDateTime- Returns:
- a long representing the zonedDateTime as the number of milliseconds since the number of milliseconds since Jan 1, 1970
-