From e305e25512825c9aff34feeb72634ad3483feede Mon Sep 17 00:00:00 2001 From: Ken Partlow Date: Tue, 30 Jan 2024 16:17:14 -0500 Subject: [PATCH] Year, OffsetDateTime conversions --- .../util/convert/CalendarConversions.java | 6 ++ .../cedarsoftware/util/convert/Converter.java | 51 +++++++++-- .../util/convert/DateConversions.java | 24 ----- .../util/convert/LocalDateConversions.java | 8 ++ .../convert/LocalDateTimeConversions.java | 6 ++ .../util/convert/LocalTimeConversions.java | 6 ++ .../convert/OffsetDateTimeConversions.java | 88 +++++++++++++++++++ .../util/convert/YearConversions.java | 66 ++++++++++++++ .../convert/ZonedDateTimeConversions.java | 7 ++ .../util/convert/MapConversionTests.java | 14 +++ 10 files changed, 246 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/cedarsoftware/util/convert/OffsetDateTimeConversions.java create mode 100644 src/main/java/com/cedarsoftware/util/convert/YearConversions.java create mode 100644 src/test/java/com/cedarsoftware/util/convert/MapConversionTests.java diff --git a/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java b/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java index 34e0e2f5..693ea1d3 100644 --- a/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java @@ -3,6 +3,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Timestamp; +import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; @@ -115,4 +116,9 @@ static Calendar create(long epochMilli, ConverterOptions options) { cal.setTimeInMillis(epochMilli); return cal; } + + static String toString(Object from, Converter converter, ConverterOptions options) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + return simpleDateFormat.format(((Calendar) from).getTime()); + } } diff --git a/src/main/java/com/cedarsoftware/util/convert/Converter.java b/src/main/java/com/cedarsoftware/util/convert/Converter.java index 37322e40..b6328b44 100644 --- a/src/main/java/com/cedarsoftware/util/convert/Converter.java +++ b/src/main/java/com/cedarsoftware/util/convert/Converter.java @@ -12,6 +12,8 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.MonthDay; +import java.time.OffsetDateTime; +import java.time.Year; import java.time.ZonedDateTime; import java.util.AbstractMap; import java.util.Calendar; @@ -156,6 +158,8 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, Integer.class), NumberConversions::toInt); DEFAULT_FACTORY.put(pair(Map.class, Integer.class), MapConversions::toInt); DEFAULT_FACTORY.put(pair(String.class, Integer.class), StringConversions::toInt); + DEFAULT_FACTORY.put(pair(Year.class, Integer.class), YearConversions::toInt); + // toLong DEFAULT_FACTORY.put(pair(Void.class, long.class), NumberConversions::toLongZero); @@ -184,6 +188,9 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, Long.class), NumberConversions::toLong); DEFAULT_FACTORY.put(pair(Map.class, Long.class), MapConversions::toLong); DEFAULT_FACTORY.put(pair(String.class, Long.class), StringConversions::toLong); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, Long.class), OffsetDateTimeConversions::toLong); + DEFAULT_FACTORY.put(pair(Year.class, Long.class), YearConversions::toLong); + // toFloat DEFAULT_FACTORY.put(pair(Void.class, float.class), NumberConversions::toFloatZero); @@ -234,6 +241,8 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, Double.class), NumberConversions::toDouble); DEFAULT_FACTORY.put(pair(Map.class, Double.class), MapConversions::toDouble); DEFAULT_FACTORY.put(pair(String.class, Double.class), StringConversions::toDouble); + DEFAULT_FACTORY.put(pair(Year.class, Double.class), YearConversions::toDouble); + // Boolean/boolean conversions supported DEFAULT_FACTORY.put(pair(Void.class, boolean.class), VoidConversions::toBoolean); @@ -254,6 +263,8 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, Boolean.class), NumberConversions::isIntTypeNotZero); DEFAULT_FACTORY.put(pair(Map.class, Boolean.class), MapConversions::toBoolean); DEFAULT_FACTORY.put(pair(String.class, Boolean.class), StringConversions::toBoolean); + DEFAULT_FACTORY.put(pair(Year.class, Boolean.class), YearConversions::toBoolean); + // Character/chat conversions supported DEFAULT_FACTORY.put(pair(Void.class, char.class), VoidConversions::toChar); @@ -302,6 +313,9 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, BigInteger.class), NumberConversions::toBigInteger); DEFAULT_FACTORY.put(pair(Map.class, BigInteger.class), MapConversions::toBigInteger); DEFAULT_FACTORY.put(pair(String.class, BigInteger.class), StringConversions::toBigInteger); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, BigInteger.class), OffsetDateTimeConversions::toBigInteger); + DEFAULT_FACTORY.put(pair(Year.class, BigInteger.class), YearConversions::toBigInteger); + // BigDecimal conversions supported DEFAULT_FACTORY.put(pair(Void.class, BigDecimal.class), VoidConversions::toNull); @@ -330,6 +344,9 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, BigDecimal.class), NumberConversions::toBigDecimal); DEFAULT_FACTORY.put(pair(Map.class, BigDecimal.class), MapConversions::toBigDecimal); DEFAULT_FACTORY.put(pair(String.class, BigDecimal.class), StringConversions::toBigDecimal); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, BigDecimal.class), OffsetDateTimeConversions::toBigDecimal); + DEFAULT_FACTORY.put(pair(Year.class, BigDecimal.class), YearConversions::toBigDecimal); + // AtomicBoolean conversions supported DEFAULT_FACTORY.put(pair(Void.class, AtomicBoolean.class), VoidConversions::toNull); @@ -349,6 +366,7 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, AtomicBoolean.class), NumberConversions::toAtomicBoolean); DEFAULT_FACTORY.put(pair(Map.class, AtomicBoolean.class), MapConversions::toAtomicBoolean); DEFAULT_FACTORY.put(pair(String.class, AtomicBoolean.class), StringConversions::toAtomicBoolean); + DEFAULT_FACTORY.put(pair(Year.class, AtomicBoolean.class), YearConversions::toAtomicBoolean); // AtomicInteger conversions supported DEFAULT_FACTORY.put(pair(Void.class, AtomicInteger.class), VoidConversions::toNull); @@ -369,6 +387,7 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, AtomicBoolean.class), NumberConversions::toAtomicInteger); DEFAULT_FACTORY.put(pair(Map.class, AtomicInteger.class), MapConversions::toAtomicInteger); DEFAULT_FACTORY.put(pair(String.class, AtomicInteger.class), StringConversions::toAtomicInteger); + DEFAULT_FACTORY.put(pair(Year.class, AtomicInteger.class), YearConversions::toAtomicInteger); // AtomicLong conversions supported DEFAULT_FACTORY.put(pair(Void.class, AtomicLong.class), VoidConversions::toNull); @@ -396,6 +415,9 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, AtomicLong.class), NumberConversions::toAtomicLong); DEFAULT_FACTORY.put(pair(Map.class, AtomicLong.class), MapConversions::toAtomicLong); DEFAULT_FACTORY.put(pair(String.class, AtomicLong.class), StringConversions::toAtomicLong); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, AtomicLong.class), OffsetDateTimeConversions::toAtomicLong); + DEFAULT_FACTORY.put(pair(Year.class, AtomicLong.class), YearConversions::toAtomicLong); + // Date conversions supported DEFAULT_FACTORY.put(pair(Void.class, Date.class), VoidConversions::toNull); @@ -415,6 +437,8 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, Date.class), NumberConversions::toDate); DEFAULT_FACTORY.put(pair(Map.class, Date.class), MapConversions::toDate); DEFAULT_FACTORY.put(pair(String.class, Date.class), StringConversions::toDate); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, Date.class), OffsetDateTimeConversions::toDate); + // java.sql.Date conversion supported DEFAULT_FACTORY.put(pair(Void.class, java.sql.Date.class), VoidConversions::toNull); @@ -434,6 +458,7 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, java.sql.Date.class), NumberConversions::toSqlDate); DEFAULT_FACTORY.put(pair(Map.class, java.sql.Date.class), MapConversions::toSqlDate); DEFAULT_FACTORY.put(pair(String.class, java.sql.Date.class), StringConversions::toSqlDate); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, java.sql.Date.class), OffsetDateTimeConversions::toSqlDate); // Timestamp conversions supported DEFAULT_FACTORY.put(pair(Void.class, Timestamp.class), VoidConversions::toNull); @@ -453,6 +478,8 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, Timestamp.class), NumberConversions::toTimestamp); DEFAULT_FACTORY.put(pair(Map.class, Timestamp.class), MapConversions::toTimestamp); DEFAULT_FACTORY.put(pair(String.class, Timestamp.class), StringConversions::toTimestamp); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, Timestamp.class), OffsetDateTimeConversions::toTimestamp); + // Calendar conversions supported DEFAULT_FACTORY.put(pair(Void.class, Calendar.class), VoidConversions::toNull); @@ -472,6 +499,8 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, Calendar.class), NumberConversions::toCalendar); DEFAULT_FACTORY.put(pair(Map.class, Calendar.class), MapConversions::toCalendar); DEFAULT_FACTORY.put(pair(String.class, Calendar.class), StringConversions::toCalendar); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, Calendar.class), OffsetDateTimeConversions::toCalendar); + // LocalDate conversions supported DEFAULT_FACTORY.put(pair(Void.class, LocalDate.class), VoidConversions::toNull); @@ -491,6 +520,8 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, LocalDate.class), NumberConversions::toLocalDate); DEFAULT_FACTORY.put(pair(Map.class, LocalDate.class), MapConversions::toLocalDate); DEFAULT_FACTORY.put(pair(String.class, LocalDate.class), StringConversions::toLocalDate); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, LocalDate.class), OffsetDateTimeConversions::toLocalDate); + // LocalDateTime conversions supported DEFAULT_FACTORY.put(pair(Void.class, LocalDateTime.class), VoidConversions::toNull); @@ -510,6 +541,8 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, LocalDateTime.class), NumberConversions::toLocalDateTime); DEFAULT_FACTORY.put(pair(Map.class, LocalDateTime.class), MapConversions::toLocalDateTime); DEFAULT_FACTORY.put(pair(String.class, LocalDateTime.class), StringConversions::toLocalDateTime); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, LocalDateTime.class), OffsetDateTimeConversions::toLocalDateTime); + // LocalTime conversions supported DEFAULT_FACTORY.put(pair(Void.class, LocalTime.class), VoidConversions::toNull); @@ -530,7 +563,9 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Number.class, LocalTime.class), NumberConversions::toLocalTime); DEFAULT_FACTORY.put(pair(Map.class, LocalTime.class), MapConversions::toLocalTime); DEFAULT_FACTORY.put(pair(String.class, LocalTime.class), StringConversions::toLocalTime); - + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, LocalTime.class), OffsetDateTimeConversions::toLocalTime); + + // ZonedDateTime conversions supported DEFAULT_FACTORY.put(pair(Void.class, ZonedDateTime.class), VoidConversions::toNull); DEFAULT_FACTORY.put(pair(Long.class, ZonedDateTime.class), NumberConversions::toZonedDateTime); @@ -588,12 +623,12 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Date.class, String.class), DateConversions::dateToString); DEFAULT_FACTORY.put(pair(java.sql.Date.class, String.class), DateConversions::sqlDateToString); DEFAULT_FACTORY.put(pair(Timestamp.class, String.class), DateConversions::timestampToString); - DEFAULT_FACTORY.put(pair(LocalDate.class, String.class), DateConversions::localDateToString); - DEFAULT_FACTORY.put(pair(LocalTime.class, String.class), DateConversions::localTimeToString); - DEFAULT_FACTORY.put(pair(LocalDateTime.class, String.class), DateConversions::localDateTimeToString); - DEFAULT_FACTORY.put(pair(ZonedDateTime.class, String.class), DateConversions::zonedDateTimeToString); + DEFAULT_FACTORY.put(pair(LocalDate.class, String.class), LocalDateConversions::toString); + DEFAULT_FACTORY.put(pair(LocalTime.class, String.class), LocalTimeConversions::toString); + DEFAULT_FACTORY.put(pair(LocalDateTime.class, String.class), LocalDateTimeConversions::toString); + DEFAULT_FACTORY.put(pair(ZonedDateTime.class, String.class), ZonedDateTimeConversions::toString); DEFAULT_FACTORY.put(pair(UUID.class, String.class), StringConversions::toString); - DEFAULT_FACTORY.put(pair(Calendar.class, String.class), DateConversions::calendarToString); + DEFAULT_FACTORY.put(pair(Calendar.class, String.class), CalendarConversions::toString); 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), StringConversions::enumToString); @@ -602,6 +637,9 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(Instant.class, String.class), StringConversions::toString); DEFAULT_FACTORY.put(pair(LocalTime.class, String.class), StringConversions::toString); DEFAULT_FACTORY.put(pair(MonthDay.class, String.class), StringConversions::toString); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, String.class), OffsetDateTimeConversions::toString); + DEFAULT_FACTORY.put(pair(Year.class, String.class), YearConversions::toString); + // Duration conversions supported DEFAULT_FACTORY.put(pair(Void.class, Duration.class), VoidConversions::toNull); @@ -627,6 +665,7 @@ private static void buildFactoryConversions() { 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); + DEFAULT_FACTORY.put(pair(OffsetDateTime.class, Instant.class), OffsetDateTimeConversions::toInstant); // java.time.OffsetDateTime = com.cedarsoftware.util.io.DEFAULT_FACTORY.OffsetDateTimeFactory // java.time.OffsetTime = com.cedarsoftware.util.io.DEFAULT_FACTORY.OffsetTimeFactory diff --git a/src/main/java/com/cedarsoftware/util/convert/DateConversions.java b/src/main/java/com/cedarsoftware/util/convert/DateConversions.java index 168f2dd1..f59440b1 100644 --- a/src/main/java/com/cedarsoftware/util/convert/DateConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/DateConversions.java @@ -114,28 +114,4 @@ static String timestampToString(Object from, Converter converter, ConverterOptio return simpleDateFormat.format(((Date) from)); } - static String calendarToString(Object from, Converter converter, ConverterOptions options) { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); - return simpleDateFormat.format(((Calendar) from).getTime()); - } - - static String localDateToString(Object from, Converter converter, ConverterOptions options) { - LocalDate localDate = (LocalDate) from; - return localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); - } - - static String localTimeToString(Object from, Converter converter, ConverterOptions options) { - LocalTime localTime = (LocalTime) from; - return localTime.format(DateTimeFormatter.ISO_LOCAL_TIME); - } - - static String localDateTimeToString(Object from, Converter converter, ConverterOptions options) { - LocalDateTime localDateTime = (LocalDateTime) from; - return localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); - } - - static String zonedDateTimeToString(Object from, Converter converter, ConverterOptions options) { - ZonedDateTime zonedDateTime = (ZonedDateTime) from; - return zonedDateTime.format(DateTimeFormatter.ISO_DATE_TIME); - } } diff --git a/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java b/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java index e20d562c..942f7fda 100644 --- a/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java @@ -8,6 +8,7 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -116,4 +117,11 @@ static BigInteger toBigInteger(Object from, Converter converter, ConverterOption static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { return BigDecimal.valueOf(toLong(from, options)); } + + static String toString(Object from, Converter converter, ConverterOptions options) { + LocalDate localDate = (LocalDate) from; + return localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); + } + + } diff --git a/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java index 44aed4f3..a714d795 100644 --- a/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java @@ -8,6 +8,7 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -100,4 +101,9 @@ static BigInteger toBigInteger(Object from, Converter converter, ConverterOption static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { return BigDecimal.valueOf(toLong(from, options)); } + + static String toString(Object from, Converter converter, ConverterOptions options) { + LocalDateTime localDateTime = (LocalDateTime) from; + return localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } } diff --git a/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java index 1d9c4236..0bac0862 100644 --- a/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java @@ -1,6 +1,7 @@ package com.cedarsoftware.util.convert; import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.Map; import com.cedarsoftware.util.CompactLinkedMap; @@ -41,4 +42,9 @@ static Map toMap(Object from, Converter converter, ConverterOpti } return target; } + + static String toString(Object from, Converter converter, ConverterOptions options) { + LocalTime localTime = (LocalTime) from; + return localTime.format(DateTimeFormatter.ISO_LOCAL_TIME); + } } diff --git a/src/main/java/com/cedarsoftware/util/convert/OffsetDateTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/OffsetDateTimeConversions.java new file mode 100644 index 00000000..fe1a2ce0 --- /dev/null +++ b/src/main/java/com/cedarsoftware/util/convert/OffsetDateTimeConversions.java @@ -0,0 +1,88 @@ +package com.cedarsoftware.util.convert; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.Timestamp; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.concurrent.atomic.AtomicLong; + +public class OffsetDateTimeConversions { + private OffsetDateTimeConversions() {} + + static OffsetDateTime toDifferentZone(Object from, ConverterOptions options) { + OffsetDateTime offsetDateTime = (OffsetDateTime) from; + return offsetDateTime.toInstant().atZone(options.getZoneId()).toOffsetDateTime(); + } + + static Instant toInstant(Object from) { + return ((OffsetDateTime)from).toInstant(); + } + + static long toLong(Object from) { + return toInstant(from).toEpochMilli(); + } + + static long toLong(Object from, Converter converter, ConverterOptions options) { + return toLong(from); + } + + static Instant toInstant(Object from, Converter converter, ConverterOptions options) { + return toInstant(from); + } + + static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) { + return toDifferentZone(from, options).toLocalDateTime(); + } + + static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) { + return toDifferentZone(from, options).toLocalDate(); + } + + static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) { + return toDifferentZone(from, options).toLocalTime(); + } + + static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) { + return new AtomicLong(toLong(from)); + } + + static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions options) { + return new Timestamp(toLong(from)); + } + + static Calendar toCalendar(Object from, Converter converter, ConverterOptions options) { + Calendar calendar = Calendar.getInstance(options.getTimeZone()); + calendar.setTimeInMillis(toLong(from)); + return calendar; + } + + static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) { + return new java.sql.Date(toLong(from)); + } + + static Date toDate(Object from, Converter converter, ConverterOptions options) { + return new Date(toLong(from)); + } + + static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) { + return BigInteger.valueOf(toLong(from)); + } + + static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { + return BigDecimal.valueOf(toLong(from)); + } + + static String toString(Object from, Converter converter, ConverterOptions options) { + OffsetDateTime offsetDateTime = (OffsetDateTime) from; + return offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); + } +} diff --git a/src/main/java/com/cedarsoftware/util/convert/YearConversions.java b/src/main/java/com/cedarsoftware/util/convert/YearConversions.java new file mode 100644 index 00000000..05052e7e --- /dev/null +++ b/src/main/java/com/cedarsoftware/util/convert/YearConversions.java @@ -0,0 +1,66 @@ +package com.cedarsoftware.util.convert; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.Timestamp; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Year; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + +public class YearConversions { + private YearConversions() {} + + static int toInt(Object from) { + return ((Year)from).getValue(); + } + + static long toLong(Object from, Converter converter, ConverterOptions options) { + return toInt(from); + } + + static int toInt(Object from, Converter converter, ConverterOptions options) { + return toInt(from); + } + + static AtomicInteger toAtomicInteger(Object from, Converter converter, ConverterOptions options) { + return new AtomicInteger(toInt(from)); + } + + static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) { + return new AtomicLong(toInt(from)); + } + + static double toDouble(Object from, Converter converter, ConverterOptions options) { + return toInt(from); + } + + static boolean toBoolean(Object from, Converter converter, ConverterOptions options) { + return toInt(from) == 0; + } + + static AtomicBoolean toAtomicBoolean(Object from, Converter converter, ConverterOptions options) { + return new AtomicBoolean(toInt(from) == 0); + } + + static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) { + return BigInteger.valueOf(toInt(from)); + } + + static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { + return BigDecimal.valueOf(toInt(from)); + } + + static String toString(Object from, Converter converter, ConverterOptions options) { + return ((Year)from).toString(); + } +} diff --git a/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java index ff77edd1..e891a56f 100644 --- a/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java @@ -8,6 +8,7 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -94,4 +95,10 @@ static BigInteger toBigInteger(Object from, Converter converter, ConverterOption static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) { return BigDecimal.valueOf(toLong(from)); } + + static String toString(Object from, Converter converter, ConverterOptions options) { + ZonedDateTime zonedDateTime = (ZonedDateTime) from; + return zonedDateTime.format(DateTimeFormatter.ISO_DATE_TIME); + } + } diff --git a/src/test/java/com/cedarsoftware/util/convert/MapConversionTests.java b/src/test/java/com/cedarsoftware/util/convert/MapConversionTests.java new file mode 100644 index 00000000..a01f1444 --- /dev/null +++ b/src/test/java/com/cedarsoftware/util/convert/MapConversionTests.java @@ -0,0 +1,14 @@ +package com.cedarsoftware.util.convert; + +import org.junit.jupiter.params.provider.Arguments; + +import javax.swing.text.Segment; +import java.util.stream.Stream; + +public class MapConversionTests { + + private static Stream toByteTests() { + return Stream.of( + ); + } +}