Skip to content

Commit

Permalink
Year, OffsetDateTime conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 30, 2024
1 parent 46b62a7 commit e305e25
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
51 changes: 45 additions & 6 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/com/cedarsoftware/util/convert/DateConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,4 +42,9 @@ static Map<String, Object> 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);
}
}
Loading

0 comments on commit e305e25

Please sign in to comment.