diff --git a/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java b/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java index 29e7c803e..06a48ec9b 100644 --- a/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java @@ -10,84 +10,83 @@ import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Date; -import java.util.GregorianCalendar; import java.util.concurrent.atomic.AtomicLong; public class CalendarConversions { - static Date toDate(Object fromInstance) { - return ((Calendar)fromInstance).getTime(); + static Date toDate(Object from) { + return ((Calendar)from).getTime(); } - static long toLong(Object fromInstance) { - return toDate(fromInstance).getTime(); + static long toLong(Object from) { + return toDate(from).getTime(); } - static Instant toInstant(Object fromInstance) { - return ((Calendar)fromInstance).toInstant(); + static Instant toInstant(Object from) { + return ((Calendar)from).toInstant(); } - static ZonedDateTime toZonedDateTime(Object fromInstance, ConverterOptions options) { - return toInstant(fromInstance).atZone(options.getZoneId()); + static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) { + return toInstant(from).atZone(options.getZoneId()); } - static ZonedDateTime toZonedDateTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options); + static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options); } - static Long toLong(Object fromInstance, Converter converter, ConverterOptions options) { - return toLong(fromInstance); + static Long toLong(Object from, Converter converter, ConverterOptions options) { + return toLong(from); } - static double toDouble(Object fromInstance, Converter converter, ConverterOptions options) { - return (double)toLong(fromInstance); + static double toDouble(Object from, Converter converter, ConverterOptions options) { + return (double)toLong(from); } - static Date toDate(Object fromInstance, Converter converter, ConverterOptions options) { - return toDate(fromInstance); + static Date toDate(Object from, Converter converter, ConverterOptions options) { + return toDate(from); } - static java.sql.Date toSqlDate(Object fromInstance, Converter converter, ConverterOptions options) { - return new java.sql.Date(toLong(fromInstance)); + static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) { + return new java.sql.Date(toLong(from)); } - static Timestamp toTimestamp(Object fromInstance, Converter converter, ConverterOptions options) { - return new Timestamp(toLong(fromInstance)); + static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions options) { + return new Timestamp(toLong(from)); } - static AtomicLong toAtomicLong(Object fromInstance, Converter converter, ConverterOptions options) { - return new AtomicLong(toLong(fromInstance)); + static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) { + return new AtomicLong(toLong(from)); } - static Instant toInstant(Object fromInstance, Converter converter, ConverterOptions options) { - return toInstant(fromInstance); + static Instant toInstant(Object from, Converter converter, ConverterOptions options) { + return toInstant(from); } - static LocalDateTime toLocalDateTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toLocalDateTime(); + static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toLocalDateTime(); } - static LocalDate toLocalDate(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toLocalDate(); + static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toLocalDate(); } - static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toLocalTime(); + static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toLocalTime(); } - static BigDecimal toBigDecimal(Object fromInstance, Converter converter, ConverterOptions options) { - return BigDecimal.valueOf(toLong(fromInstance)); + static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { + return BigDecimal.valueOf(toLong(from)); } - static BigInteger toBigInteger(Object fromInstance, Converter converter, ConverterOptions options) { - return BigInteger.valueOf(toLong(fromInstance)); + static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) { + return BigInteger.valueOf(toLong(from)); } - static Calendar clone(Object fromInstance, Converter converter, ConverterOptions options) { - Calendar from = (Calendar)fromInstance; + static Calendar clone(Object from, Converter converter, ConverterOptions options) { + Calendar calendar = (Calendar)from; // mutable class, so clone it. - return (Calendar)from.clone(); + return (Calendar)calendar.clone(); } static Calendar create(long epochMilli, ConverterOptions options) { diff --git a/src/main/java/com/cedarsoftware/util/convert/Converter.java b/src/main/java/com/cedarsoftware/util/convert/Converter.java index 45e9321cc..366260a4b 100644 --- a/src/main/java/com/cedarsoftware/util/convert/Converter.java +++ b/src/main/java/com/cedarsoftware/util/convert/Converter.java @@ -590,7 +590,7 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Calendar.class, String.class), DateConversions::calendarToString); DEFAULT_FACTORY.put(pair(Number.class, String.class), StringConversions::toString); DEFAULT_FACTORY.put(pair(Map.class, String.class), MapConversions::toString); - DEFAULT_FACTORY.put(pair(Enum.class, String.class), (fromInstance, converter, options) -> ((Enum) fromInstance).name()); + DEFAULT_FACTORY.put(pair(Enum.class, String.class), StringConversions::enumToString); DEFAULT_FACTORY.put(pair(String.class, String.class), Converter::identity); DEFAULT_FACTORY.put(pair(Duration.class, String.class), StringConversions::toString); DEFAULT_FACTORY.put(pair(Instant.class, String.class), StringConversions::toString); @@ -600,7 +600,7 @@ private static void buildFactoryConversions() { // Duration conversions supported DEFAULT_FACTORY.put(pair(Void.class, Duration.class), VoidConversions::toNull); DEFAULT_FACTORY.put(pair(Duration.class, Duration.class), Converter::identity); - DEFAULT_FACTORY.put(pair(String.class, Duration.class), (fromInstance, converter, options) -> Duration.parse((String) fromInstance)); + DEFAULT_FACTORY.put(pair(String.class, Duration.class), StringConversions::toString); DEFAULT_FACTORY.put(pair(Map.class, Duration.class), MapConversions::toDuration); // Instant conversions supported @@ -619,7 +619,6 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(ZonedDateTime.class, Instant.class), ZonedDateTimeConversions::toInstant); DEFAULT_FACTORY.put(pair(Calendar.class, Instant.class), CalendarConversions::toInstant); DEFAULT_FACTORY.put(pair(Number.class, Instant.class), NumberConversions::toInstant); - DEFAULT_FACTORY.put(pair(String.class, Instant.class), StringConversions::toInstant); DEFAULT_FACTORY.put(pair(Map.class, Instant.class), MapConversions::toInstant); @@ -635,10 +634,7 @@ private static void buildFactoryConversions() { // MonthDay conversions supported DEFAULT_FACTORY.put(pair(Void.class, MonthDay.class), VoidConversions::toNull); DEFAULT_FACTORY.put(pair(MonthDay.class, MonthDay.class), Converter::identity); - DEFAULT_FACTORY.put(pair(String.class, MonthDay.class), (fromInstance, converter, options) -> { - String monthDay = (String) fromInstance; - return MonthDay.parse(monthDay); - }); + DEFAULT_FACTORY.put(pair(String.class, MonthDay.class), StringConversions::toMonthDay); DEFAULT_FACTORY.put(pair(Map.class, MonthDay.class), MapConversions::toMonthDay); // Map conversions supported @@ -662,24 +658,24 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(LocalDate.class, Map.class), MapConversions::initMap); DEFAULT_FACTORY.put(pair(LocalDateTime.class, Map.class), MapConversions::initMap); DEFAULT_FACTORY.put(pair(ZonedDateTime.class, Map.class), MapConversions::initMap); - DEFAULT_FACTORY.put(pair(Duration.class, Map.class), (fromInstance, converter, options) -> { - long sec = ((Duration) fromInstance).getSeconds(); - long nanos = ((Duration) fromInstance).getNano(); + DEFAULT_FACTORY.put(pair(Duration.class, Map.class), (from, converter, options) -> { + long sec = ((Duration) from).getSeconds(); + long nanos = ((Duration) from).getNano(); Map target = new LinkedHashMap<>(); target.put("seconds", sec); target.put("nanos", nanos); return target; }); - DEFAULT_FACTORY.put(pair(Instant.class, Map.class), (fromInstance, converter, options) -> { - long sec = ((Instant) fromInstance).getEpochSecond(); - long nanos = ((Instant) fromInstance).getNano(); + DEFAULT_FACTORY.put(pair(Instant.class, Map.class), (from, converter, options) -> { + long sec = ((Instant) from).getEpochSecond(); + long nanos = ((Instant) from).getNano(); Map target = new LinkedHashMap<>(); target.put("seconds", sec); target.put("nanos", nanos); return target; }); - DEFAULT_FACTORY.put(pair(LocalTime.class, Map.class), (fromInstance, converter, options) -> { - LocalTime localTime = (LocalTime) fromInstance; + DEFAULT_FACTORY.put(pair(LocalTime.class, Map.class), (from, converter, options) -> { + LocalTime localTime = (LocalTime) from; Map target = new LinkedHashMap<>(); target.put("hour", localTime.getHour()); target.put("minute", localTime.getMinute()); @@ -693,8 +689,8 @@ private static void buildFactoryConversions() { } return target; }); - DEFAULT_FACTORY.put(pair(MonthDay.class, Map.class), (fromInstance, converter, options) -> { - MonthDay monthDay = (MonthDay) fromInstance; + DEFAULT_FACTORY.put(pair(MonthDay.class, Map.class), (from, converter, options) -> { + MonthDay monthDay = (MonthDay) from; Map target = new LinkedHashMap<>(); target.put("day", monthDay.getDayOfMonth()); target.put("month", monthDay.getMonthValue()); @@ -704,8 +700,8 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(UUID.class, Map.class), MapConversions::initMap); DEFAULT_FACTORY.put(pair(Calendar.class, Map.class), MapConversions::initMap); DEFAULT_FACTORY.put(pair(Number.class, Map.class), MapConversions::initMap); - DEFAULT_FACTORY.put(pair(Map.class, Map.class), (fromInstance, converter, options) -> { - Map source = (Map) fromInstance; + DEFAULT_FACTORY.put(pair(Map.class, Map.class), (from, converter, options) -> { + Map source = (Map) from; Map copy = new LinkedHashMap<>(source); return copy; }); @@ -733,7 +729,7 @@ public Converter(ConverterOptions options) { * convert(map, double.class) // Converter will extract the value associated to the "_v" (or "value") key and convert it. * * - * @param fromInstance A value used to create the targetType, even though it may + * @param from A value used to create the targetType, even though it may * not (most likely will not) be the same data type as the targetType * @param toType Class which indicates the targeted (final) data type. * Please note that in addition to the 8 Java primitives, the targeted class @@ -743,8 +739,8 @@ public Converter(ConverterOptions options) { * fields within the Map to perform the conversion. * @return An instanceof targetType class, based upon the value passed in. */ - public T convert(Object fromInstance, Class toType) { - return this.convert(fromInstance, toType, options); + public T convert(Object from, Class toType) { + return this.convert(from, toType, options); } /** @@ -763,7 +759,7 @@ public T convert(Object fromInstance, Class toType) { * convert(map, double.class) // Converter will extract the value associated to the "_v" (or "value") key and convert it. * * - * @param fromInstance A value used to create the targetType, even though it may + * @param from A value used to create the targetType, even though it may * not (most likely will not) be the same data type as the targetType * @param toType Class which indicates the targeted (final) data type. * Please note that in addition to the 8 Java primitives, the targeted class @@ -776,17 +772,17 @@ public T convert(Object fromInstance, Class toType) { * @return An instanceof targetType class, based upon the value passed in. */ @SuppressWarnings("unchecked") - public T convert(Object fromInstance, Class toType, ConverterOptions options) { + public T convert(Object from, Class toType, ConverterOptions options) { if (toType == null) { throw new IllegalArgumentException("toType cannot be null"); } Class sourceType; - if (fromInstance == null) { + if (from == null) { // Do not promote primitive to primitive wrapper - allows for different 'from NULL' type for each. sourceType = Void.class; } else { // Promote primitive to primitive wrapper so we don't have to define so many duplicates in the factory map. - sourceType = fromInstance.getClass(); + sourceType = from.getClass(); if (toType.isPrimitive()) { toType = (Class) toPrimitiveWrapperClass(toType); } @@ -795,7 +791,7 @@ public T convert(Object fromInstance, Class toType, ConverterOptions opti // Direct Mapping Convert converter = factory.get(pair(sourceType, toType)); if (converter != null) { - return (T) converter.convert(fromInstance, this, options); + return (T) converter.convert(from, this, options); } // Try inheritance @@ -805,10 +801,10 @@ public T convert(Object fromInstance, Class toType, ConverterOptions opti if (!isDirectConversionSupportedFor(sourceType, toType)) { addConversion(sourceType, toType, converter); } - return (T) converter.convert(fromInstance, this, options); + return (T) converter.convert(from, this, options); } - throw new IllegalArgumentException("Unsupported conversion, source type [" + name(fromInstance) + "] target type '" + getShortName(toType) + "'"); + throw new IllegalArgumentException("Unsupported conversion, source type [" + name(from) + "] target type '" + getShortName(toType) + "'"); } /** @@ -910,11 +906,11 @@ static String getShortName(Class type) { return java.sql.Date.class.equals(type) ? type.getName() : type.getSimpleName(); } - static private String name(Object fromInstance) { - if (fromInstance == null) { + static private String name(Object from) { + if (from == null) { return "null"; } - return getShortName(fromInstance.getClass()) + " (" + fromInstance + ")"; + return getShortName(from.getClass()) + " (" + from + ")"; } /** diff --git a/src/main/java/com/cedarsoftware/util/convert/DurationConversions.java b/src/main/java/com/cedarsoftware/util/convert/DurationConversions.java new file mode 100644 index 000000000..e332e2ac6 --- /dev/null +++ b/src/main/java/com/cedarsoftware/util/convert/DurationConversions.java @@ -0,0 +1,22 @@ +package com.cedarsoftware.util.convert; + +/** + * @author John DeRegnaucourt (jdereg@gmail.com) + *
+ * 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. + */ +public class DurationConversions { + +} diff --git a/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java b/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java index 50e19dce7..4e03fe197 100644 --- a/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java @@ -15,87 +15,86 @@ public class LocalDateConversions { - private static ZonedDateTime toZonedDateTime(Object fromInstance, ConverterOptions options) { - return ((LocalDate)fromInstance).atStartOfDay(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId()); + private static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) { + return ((LocalDate)from).atStartOfDay(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId()); } - static Instant toInstant(Object fromInstance, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toInstant(); + static Instant toInstant(Object from, ConverterOptions options) { + return toZonedDateTime(from, options).toInstant(); } - static long toLong(Object fromInstance, ConverterOptions options) { - return toInstant(fromInstance, options).toEpochMilli(); + static long toLong(Object from, ConverterOptions options) { + return toInstant(from, options).toEpochMilli(); } - static LocalDateTime toLocalDateTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toLocalDateTime(); + static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toLocalDateTime(); } - static LocalDate toLocalDate(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toLocalDate(); + static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toLocalDate(); } - static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toLocalTime(); + static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toLocalTime(); } - static ZonedDateTime toZonedDateTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).withZoneSameInstant(options.getZoneId()); + static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).withZoneSameInstant(options.getZoneId()); } - static Instant toInstant(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toInstant(); + static Instant toInstant(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toInstant(); } - static long toLong(Object fromInstance, Converter converter, ConverterOptions options) { - return toInstant(fromInstance, options).toEpochMilli(); + static long toLong(Object from, Converter converter, ConverterOptions options) { + return toInstant(from, options).toEpochMilli(); } /** * Warning: Can lose precision going from a full long down to a floating point number - * @param fromInstance instance to convert + * @param from instance to convert * @param converter converter instance * @param options converter options * @return the floating point number cast from a lont. */ - static float toFloat(Object fromInstance, Converter converter, ConverterOptions options) { - return toLong(fromInstance, converter, options); + static float toFloat(Object from, Converter converter, ConverterOptions options) { + return toLong(from, converter, options); } - static double toDouble(Object fromInstance, Converter converter, ConverterOptions options) { - return toLong(fromInstance, converter, options); + static double toDouble(Object from, Converter converter, ConverterOptions options) { + return toLong(from, converter, options); } - static AtomicLong toAtomicLong(Object fromInstance, Converter converter, ConverterOptions options) { - LocalDate from = (LocalDate)fromInstance; + static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) { return new AtomicLong(toLong(from, options)); } - static Timestamp toTimestamp(Object fromInstance, Converter converter, ConverterOptions options) { - return new Timestamp(toLong(fromInstance, options)); + static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions options) { + return new Timestamp(toLong(from, options)); } - static Calendar toCalendar(Object fromInstance, Converter converter, ConverterOptions options) { - ZonedDateTime time = toZonedDateTime(fromInstance, options); + static Calendar toCalendar(Object from, Converter converter, ConverterOptions options) { + ZonedDateTime time = toZonedDateTime(from, options); GregorianCalendar calendar = new GregorianCalendar(options.getTimeZone()); calendar.setTimeInMillis(time.toInstant().toEpochMilli()); return calendar; } - static java.sql.Date toSqlDate(Object fromInstance, Converter converter, ConverterOptions options) { - return new java.sql.Date(toLong(fromInstance, options)); + static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) { + return new java.sql.Date(toLong(from, options)); } - static Date toDate(Object fromInstance, Converter converter, ConverterOptions options) { - return new Date(toLong(fromInstance, options)); + static Date toDate(Object from, Converter converter, ConverterOptions options) { + return new Date(toLong(from, options)); } - static BigInteger toBigInteger(Object fromInstance, Converter converter, ConverterOptions options) { - return BigInteger.valueOf(toLong(fromInstance, options)); + static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) { + return BigInteger.valueOf(toLong(from, options)); } - static BigDecimal toBigDecimal(Object fromInstance, Converter converter, ConverterOptions options) { - return BigDecimal.valueOf(toLong(fromInstance, options)); + static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { + return BigDecimal.valueOf(toLong(from, options)); } } diff --git a/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java index d98a2e0c1..a55b9a10b 100644 --- a/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java @@ -14,70 +14,70 @@ import java.util.concurrent.atomic.AtomicLong; public class LocalDateTimeConversions { - private static ZonedDateTime toZonedDateTime(Object fromInstance, ConverterOptions options) { - return ((LocalDateTime)fromInstance).atZone(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId()); + private static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) { + return ((LocalDateTime)from).atZone(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId()); } - private static Instant toInstant(Object fromInstance, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toInstant(); + private static Instant toInstant(Object from, ConverterOptions options) { + return toZonedDateTime(from, options).toInstant(); } - private static long toLong(Object fromInstance, ConverterOptions options) { - return toInstant(fromInstance, options).toEpochMilli(); + private static long toLong(Object from, ConverterOptions options) { + return toInstant(from, options).toEpochMilli(); } - static ZonedDateTime toZonedDateTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options); + static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options); } - static LocalDateTime toLocalDateTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toLocalDateTime(); + static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toLocalDateTime(); } - static LocalDate toLocalDate(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toLocalDate(); + static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toLocalDate(); } - static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toZonedDateTime(fromInstance, options).toLocalTime(); + static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) { + return toZonedDateTime(from, options).toLocalTime(); } - static Instant toInstant(Object fromInstance, Converter converter, ConverterOptions options) { - return toInstant(fromInstance, options); + static Instant toInstant(Object from, Converter converter, ConverterOptions options) { + return toInstant(from, options); } - static long toLong(Object fromInstance, Converter converter, ConverterOptions options) { - return toLong(fromInstance, options); + static long toLong(Object from, Converter converter, ConverterOptions options) { + return toLong(from, options); } - static AtomicLong toAtomicLong(Object fromInstance, Converter converter, ConverterOptions options) { - return new AtomicLong(toLong(fromInstance, options)); + static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) { + return new AtomicLong(toLong(from, options)); } - static Timestamp toTimestamp(Object fromInstance, Converter converter, ConverterOptions options) { - return new Timestamp(toLong(fromInstance, options)); + static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions options) { + return new Timestamp(toLong(from, options)); } - static Calendar toCalendar(Object fromInstance, Converter converter, ConverterOptions options) { - ZonedDateTime time = toZonedDateTime(fromInstance, options); + static Calendar toCalendar(Object from, Converter converter, ConverterOptions options) { + ZonedDateTime time = toZonedDateTime(from, options); GregorianCalendar calendar = new GregorianCalendar(options.getTimeZone()); calendar.setTimeInMillis(time.toInstant().toEpochMilli()); return calendar; } - static java.sql.Date toSqlDate(Object fromInstance, Converter converter, ConverterOptions options) { - return new java.sql.Date(toLong(fromInstance, options)); + static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) { + return new java.sql.Date(toLong(from, options)); } - static Date toDate(Object fromInstance, Converter converter, ConverterOptions options) { - return new Date(toLong(fromInstance, options)); + static Date toDate(Object from, Converter converter, ConverterOptions options) { + return new Date(toLong(from, options)); } - static BigInteger toBigInteger(Object fromInstance, Converter converter, ConverterOptions options) { - return BigInteger.valueOf(toLong(fromInstance, options)); + static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) { + return BigInteger.valueOf(toLong(from, options)); } - static BigDecimal toBigDecimal(Object fromInstance, Converter converter, ConverterOptions options) { - return BigDecimal.valueOf(toLong(fromInstance, options)); + static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { + return BigDecimal.valueOf(toLong(from, options)); } } diff --git a/src/main/java/com/cedarsoftware/util/convert/MapConversions.java b/src/main/java/com/cedarsoftware/util/convert/MapConversions.java index d59ee1256..63f66d7cd 100644 --- a/src/main/java/com/cedarsoftware/util/convert/MapConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/MapConversions.java @@ -44,8 +44,8 @@ public class MapConversions { public static final String KEY_VALUE_ERROR_MESSAGE = "To convert from Map to %s the map must include one of the following: %s[_v], or [value] with associated values."; private static String[] UUID_PARAMS = new String[] { MOST_SIG_BITS, LEAST_SIG_BITS }; - static Object toUUID(Object fromInstance, Converter converter, ConverterOptions options) { - Map map = (Map) fromInstance; + static Object toUUID(Object from, Converter converter, ConverterOptions options) { + Map map = (Map) from; if (map.containsKey(MOST_SIG_BITS) && map.containsKey(LEAST_SIG_BITS)) { long most = converter.convert(map.get(MOST_SIG_BITS), long.class, options); @@ -53,76 +53,76 @@ static Object toUUID(Object fromInstance, Converter converter, ConverterOptions return new UUID(most, least); } - return fromValueForMultiKey(fromInstance, converter, options, UUID.class, UUID_PARAMS); + return fromValueForMultiKey(from, converter, options, UUID.class, UUID_PARAMS); } - static Byte toByte(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, Byte.class); + static Byte toByte(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, Byte.class); } - static Short toShort(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, Short.class); + static Short toShort(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, Short.class); } - static Integer toInt(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, Integer.class); + static Integer toInt(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, Integer.class); } - static Long toLong(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, Long.class); + static Long toLong(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, Long.class); } - static Float toFloat(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, Float.class); + static Float toFloat(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, Float.class); } - static Double toDouble(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, Double.class); + static Double toDouble(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, Double.class); } - static Boolean toBoolean(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, Boolean.class); + static Boolean toBoolean(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, Boolean.class); } - static BigDecimal toBigDecimal(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, BigDecimal.class); + static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, BigDecimal.class); } - static BigInteger toBigInteger(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, BigInteger.class); + static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, BigInteger.class); } - static String toString(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, String.class); + static String toString(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, String.class); } - static Character toCharacter(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, char.class); + static Character toCharacter(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, char.class); } - static AtomicInteger toAtomicInteger(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, AtomicInteger.class); + static AtomicInteger toAtomicInteger(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, AtomicInteger.class); } - static AtomicLong toAtomicLong(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, AtomicLong.class); + static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, AtomicLong.class); } - static AtomicBoolean toAtomicBoolean(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, AtomicBoolean.class); + static AtomicBoolean toAtomicBoolean(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, AtomicBoolean.class); } - static java.sql.Date toSqlDate(Object fromInstance, Converter converter, ConverterOptions options) { - return fromSingleKey(fromInstance, converter, options, TIME, java.sql.Date.class); + static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) { + return fromSingleKey(from, converter, options, TIME, java.sql.Date.class); } - static Date toDate(Object fromInstance, Converter converter, ConverterOptions options) { - return fromSingleKey(fromInstance, converter, options, TIME, Date.class); + static Date toDate(Object from, Converter converter, ConverterOptions options) { + return fromSingleKey(from, converter, options, TIME, Date.class); } private static final String[] TIMESTAMP_PARAMS = new String[] { TIME, NANOS }; - static Timestamp toTimestamp(Object fromInstance, Converter converter, ConverterOptions options) { - Map map = (Map) fromInstance; + static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions options) { + Map map = (Map) from; if (map.containsKey(TIME)) { long time = converter.convert(map.get(TIME), long.class, options); int ns = converter.convert(map.get(NANOS), int.class, options); @@ -135,8 +135,8 @@ static Timestamp toTimestamp(Object fromInstance, Converter converter, Converter } private static final String[] CALENDAR_PARAMS = new String[] { TIME, ZONE }; - static Calendar toCalendar(Object fromInstance, Converter converter, ConverterOptions options) { - Map map = (Map) fromInstance; + static Calendar toCalendar(Object from, Converter converter, ConverterOptions options) { + Map map = (Map) from; if (map.containsKey(TIME)) { Object zoneRaw = map.get(ZONE); TimeZone tz; @@ -157,8 +157,8 @@ static Calendar toCalendar(Object fromInstance, Converter converter, ConverterOp } private static final String[] LOCAL_DATE_PARAMS = new String[] { YEAR, MONTH, DAY }; - static LocalDate toLocalDate(Object fromInstance, Converter converter, ConverterOptions options) { - Map map = (Map) fromInstance; + static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) { + Map map = (Map) from; if (map.containsKey(MONTH) && map.containsKey(DAY) && map.containsKey(YEAR)) { int month = converter.convert(map.get(MONTH), int.class, options); int day = converter.convert(map.get(DAY), int.class, options); @@ -170,8 +170,8 @@ static LocalDate toLocalDate(Object fromInstance, Converter converter, Converter } private static final String[] LOCAL_TIME_PARAMS = new String[] { HOUR, MINUTE, SECOND, NANO }; - static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) { - Map map = (Map) fromInstance; + static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) { + Map map = (Map) from; if (map.containsKey(HOUR) && map.containsKey(MINUTE)) { int hour = converter.convert(map.get(HOUR), int.class, options); int minute = converter.convert(map.get(MINUTE), int.class, options); @@ -183,51 +183,51 @@ static LocalTime toLocalTime(Object fromInstance, Converter converter, Converter } } - static LocalDateTime toLocalDateTime(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, LocalDateTime.class); + static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, LocalDateTime.class); } - static ZonedDateTime toZonedDateTime(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, ZonedDateTime.class); + static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, ZonedDateTime.class); } - static Class toClass(Object fromInstance, Converter converter, ConverterOptions options) { - return fromValue(fromInstance, converter, options, Class.class); + static Class toClass(Object from, Converter converter, ConverterOptions options) { + return fromValue(from, converter, options, Class.class); } private static final String[] DURATION_PARAMS = new String[] { SECONDS, NANOS }; - static Duration toDuration(Object fromInstance, Converter converter, ConverterOptions options) { - Map map = (Map) fromInstance; + static Duration toDuration(Object from, Converter converter, ConverterOptions options) { + Map map = (Map) from; if (map.containsKey(SECONDS)) { long sec = converter.convert(map.get(SECONDS), long.class, options); long nanos = converter.convert(map.get(NANOS), long.class, options); return Duration.ofSeconds(sec, nanos); } else { - return fromValueForMultiKey(fromInstance, converter, options, Duration.class, DURATION_PARAMS); + return fromValueForMultiKey(from, converter, options, Duration.class, DURATION_PARAMS); } } private static final String[] INSTANT_PARAMS = new String[] { SECONDS, NANOS }; - static Instant toInstant(Object fromInstance, Converter converter, ConverterOptions options) { - Map map = (Map) fromInstance; + static Instant toInstant(Object from, Converter converter, ConverterOptions options) { + Map map = (Map) from; if (map.containsKey(SECONDS)) { long sec = converter.convert(map.get(SECONDS), long.class, options); long nanos = converter.convert(map.get(NANOS), long.class, options); return Instant.ofEpochSecond(sec, nanos); } else { - return fromValueForMultiKey(fromInstance, converter, options, Instant.class, INSTANT_PARAMS); + return fromValueForMultiKey(from, converter, options, Instant.class, INSTANT_PARAMS); } } private static final String[] MONTH_DAY_PARAMS = new String[] { MONTH, DAY }; - static MonthDay toMonthDay(Object fromInstance, Converter converter, ConverterOptions options) { - Map map = (Map) fromInstance; + static MonthDay toMonthDay(Object from, Converter converter, ConverterOptions options) { + Map map = (Map) from; if (map.containsKey(MONTH)) { int month = converter.convert(map.get(MONTH), int.class, options); int day = converter.convert(map.get(DAY), int.class, options); return MonthDay.of(month, day); } else { - return fromValueForMultiKey(fromInstance, converter, options, MonthDay.class, MONTH_DAY_PARAMS); + return fromValueForMultiKey(from, converter, options, MonthDay.class, MONTH_DAY_PARAMS); } } @@ -244,10 +244,10 @@ static MonthDay toMonthDay(Object fromInstance, Converter converter, ConverterOp * @param type of object to convert the value. * @return type if it exists, else returns what is in V or VALUE */ - private static T fromSingleKey(final Object fromInstance, final Converter converter, final ConverterOptions options, final String key, final Class type) { + private static T fromSingleKey(final Object from, final Converter converter, final ConverterOptions options, final String key, final Class type) { validateParams(converter, options, type); - Map map = asMap(fromInstance); + Map map = asMap(from); if (map.containsKey(key)) { return converter.convert(key, type, options); @@ -288,7 +288,7 @@ private static void validateParams(Converter converter, ConverterOptions opt } private static Map asMap(Object o) { - Convention.throwIfFalse(o instanceof Map, "fromInstance must be an instance of map"); + Convention.throwIfFalse(o instanceof Map, "from must be an instance of map"); return (Map)o; } } diff --git a/src/main/java/com/cedarsoftware/util/convert/NumberConversions.java b/src/main/java/com/cedarsoftware/util/convert/NumberConversions.java index e33eedf40..dffb033f2 100644 --- a/src/main/java/com/cedarsoftware/util/convert/NumberConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/NumberConversions.java @@ -128,7 +128,7 @@ static BigInteger bigDecimalToBigInteger(Object from, Converter converter, Conve return ((BigDecimal)from).toBigInteger(); } - static String bigDecimalToString(Object from, Converter converter, ConverterOptions converterOptions) { + static String bigDecimalToString(Object from, Converter converter, ConverterOptions options) { return ((BigDecimal) from).stripTrailingZeros().toPlainString(); } diff --git a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java index 329c5c9c3..bcf3c6c46 100644 --- a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java @@ -4,10 +4,12 @@ import java.math.BigInteger; import java.math.RoundingMode; import java.sql.Timestamp; +import java.time.Duration; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.MonthDay; import java.time.ZonedDateTime; import java.time.temporal.TemporalAccessor; import java.util.Calendar; @@ -232,10 +234,18 @@ static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOption } } + static String enumToString(Object from, Converter converter, ConverterOptions options) { + return ((Enum) from).name(); + } + static UUID toUUID(Object from, Converter converter, ConverterOptions options) { return UUID.fromString(((String) from).trim()); } + static Duration toDuration(Object from, Converter converter, ConverterOptions options) { + return Duration.parse((String) from); + } + static Class toClass(Object from, Converter converter, ConverterOptions options) { String str = ((String) from).trim(); Class clazz = ClassUtilities.forName(str, options.getClassLoader()); @@ -245,10 +255,15 @@ static Class toClass(Object from, Converter converter, ConverterOptions optio throw new IllegalArgumentException("Cannot convert String '" + str + "' to class. Class not found."); } - static String classToString(Object from, Converter converter, ConverterOptions converterOptions) { + static String classToString(Object from, Converter converter, ConverterOptions options) { return ((Class) from).getName(); } + static MonthDay toMonthDay(Object from, Converter converter, ConverterOptions options) { + String monthDay = (String) from; + return MonthDay.parse(monthDay); + } + static Date toDate(Object from, Converter converter, ConverterOptions options) { Instant instant = getInstant((String) from, options); if (instant == null) { diff --git a/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java index 4395d4bb5..3772f10a4 100644 --- a/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java @@ -14,63 +14,63 @@ public class ZonedDateTimeConversions { - static ZonedDateTime toDifferentZone(Object fromInstance, ConverterOptions options) { - return ((ZonedDateTime)fromInstance).withZoneSameInstant(options.getZoneId()); + static ZonedDateTime toDifferentZone(Object from, ConverterOptions options) { + return ((ZonedDateTime)from).withZoneSameInstant(options.getZoneId()); } - static Instant toInstant(Object fromInstance) { - return ((ZonedDateTime)fromInstance).toInstant(); + static Instant toInstant(Object from) { + return ((ZonedDateTime)from).toInstant(); } - static long toLong(Object fromInstance) { - return toInstant(fromInstance).toEpochMilli(); + static long toLong(Object from) { + return toInstant(from).toEpochMilli(); } - static long toLong(Object fromInstance, Converter converter, ConverterOptions options) { - return toLong(fromInstance); + static long toLong(Object from, Converter converter, ConverterOptions options) { + return toLong(from); } - static Instant toInstant(Object fromInstance, Converter converter, ConverterOptions options) { - return toInstant(fromInstance); + static Instant toInstant(Object from, Converter converter, ConverterOptions options) { + return toInstant(from); } - static LocalDateTime toLocalDateTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toDifferentZone(fromInstance, options).toLocalDateTime(); + static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) { + return toDifferentZone(from, options).toLocalDateTime(); } - static LocalDate toLocalDate(Object fromInstance, Converter converter, ConverterOptions options) { - return toDifferentZone(fromInstance, options).toLocalDate(); + static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) { + return toDifferentZone(from, options).toLocalDate(); } - static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) { - return toDifferentZone(fromInstance, options).toLocalTime(); + static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) { + return toDifferentZone(from, options).toLocalTime(); } - static AtomicLong toAtomicLong(Object fromInstance, Converter converter, ConverterOptions options) { - return new AtomicLong(toLong(fromInstance)); + static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) { + return new AtomicLong(toLong(from)); } - static Timestamp toTimestamp(Object fromInstance, Converter converter, ConverterOptions options) { - return new Timestamp(toLong(fromInstance)); + static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions options) { + return new Timestamp(toLong(from)); } - static Calendar toCalendar(Object fromInstance, Converter converter, ConverterOptions options) { - return CalendarConversions.create(toLong(fromInstance), options); + static Calendar toCalendar(Object from, Converter converter, ConverterOptions options) { + return CalendarConversions.create(toLong(from), options); } - static java.sql.Date toSqlDate(Object fromInstance, Converter converter, ConverterOptions options) { - return new java.sql.Date(toLong(fromInstance)); + static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) { + return new java.sql.Date(toLong(from)); } - static Date toDate(Object fromInstance, Converter converter, ConverterOptions options) { - return new Date(toLong(fromInstance)); + static Date toDate(Object from, Converter converter, ConverterOptions options) { + return new Date(toLong(from)); } - static BigInteger toBigInteger(Object fromInstance, Converter converter, ConverterOptions options) { - return BigInteger.valueOf(toLong(fromInstance)); + static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) { + return BigInteger.valueOf(toLong(from)); } - static BigDecimal toBigDecimal(Object fromInstance, Converter converter, ConverterOptions options) { - return BigDecimal.valueOf(toLong(fromInstance)); + static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { + return BigDecimal.valueOf(toLong(from)); } }