Skip to content

Commit

Permalink
MapConversons and OffsetDateTime Covnersions
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Feb 12, 2024
1 parent fb55240 commit b8aec84
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(OffsetDateTime.class, OffsetDateTime.class), Converter::identity);
CONVERSION_DB.put(pair(Map.class, OffsetDateTime.class), MapConversions::toOffsetDateTime);
CONVERSION_DB.put(pair(String.class, OffsetDateTime.class), StringConversions::toOffsetDateTime);
CONVERSION_DB.put(pair(Long.class, OffsetDateTime.class), NumberConversions::toOffsetDateTime);

// toOffsetTime
CONVERSION_DB.put(pair(Void.class, OffsetTime.class), VoidConversions::toNull);
Expand Down Expand Up @@ -854,6 +855,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Number.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(Map.class, Map.class), MapConversions::toMap);
CONVERSION_DB.put(pair(Enum.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(OffsetDateTime.class, Map.class), OffsetDateTimeConversions::toMap);
}

public Converter(ConverterOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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.Map;
Expand Down Expand Up @@ -86,10 +85,6 @@ static BigInteger toBigInteger(Object from, Converter converter) {
return BigInteger.valueOf(toLong(from, converter));
}

static String toString(Object from, Converter converter) {
return DateTimeFormatter.ISO_INSTANT.format((Instant)from);
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
return BigDecimal.valueOf(toLong(from, converter));
}
Expand Down
39 changes: 35 additions & 4 deletions src/main/java/com/cedarsoftware/util/convert/MapConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
final class MapConversions {
private static final String V = "_v";
private static final String VALUE = "value";
private static final String DATE = "date";
private static final String TIME = "time";
private static final String ZONE = "zone";
private static final String YEAR = "year";
Expand All @@ -73,6 +74,14 @@ final class MapConversions {
private static final String MOST_SIG_BITS = "mostSigBits";
private static final String LEAST_SIG_BITS = "leastSigBits";

static final String OFFSET = "offset";

private static final String TOTAL_SECONDS = "totalSeconds";

static final String DATE_TIME = "dateTime";

private static final String ID = "id";

private 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.";
Expand Down Expand Up @@ -245,8 +254,12 @@ static OffsetTime toOffsetTime(Object from, Converter converter) {
private static final String[] OFFSET_DATE_TIME_PARAMS = new String[] { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, NANO, OFFSET_HOUR, OFFSET_MINUTE };
static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
Map<?, ?> map = (Map<?, ?>) from;
if (map.containsKey(YEAR) && map.containsKey(OFFSET_HOUR)) {
ConverterOptions options = converter.getOptions();
ConverterOptions options = converter.getOptions();
if (map.containsKey(DATE_TIME) && map.containsKey(OFFSET)) {
LocalDateTime dateTime = converter.convert(map.get(DATE_TIME), LocalDateTime.class);
ZoneOffset zoneOffset = converter.convert(map.get(OFFSET), ZoneOffset.class);
return OffsetDateTime.of(dateTime, zoneOffset);
} else if (map.containsKey(YEAR) && map.containsKey(OFFSET_HOUR)) {
int year = converter.convert(map.get(YEAR), int.class);
int month = converter.convert(map.get(MONTH), int.class);
int day = converter.convert(map.get(DAY), int.class);
Expand All @@ -263,12 +276,30 @@ static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
}
}

private static final String[] LOCAL_DATE_TIME_PARAMS = new String[] { DATE, TIME };

static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return fromValue(from, converter, LocalDateTime.class);
Map<?, ?> map = (Map<?, ?>) from;
if (map.containsKey(DATE)) {
LocalDate localDate = converter.convert(map.get(DATE), LocalDate.class);
LocalTime localTime = map.containsKey(TIME) ? converter.convert(map.get(TIME), LocalTime.class) : LocalTime.MIDNIGHT;
// validate date isn't null?
return LocalDateTime.of(localDate, localTime);
} else {
return fromValueForMultiKey(from, converter, LocalDateTime.class, LOCAL_DATE_TIME_PARAMS);
}
}

private static final String[] ZONED_DATE_TIME_PARAMS = new String[] { ZONE, DATE_TIME };
static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return fromValue(from, converter, ZonedDateTime.class);
Map<?, ?> map = (Map<?, ?>) from;
if (map.containsKey(ZONE) && map.containsKey(DATE_TIME)) {
ZoneId zoneId = converter.convert(map.get(ZONE), ZoneId.class);
LocalDateTime localDateTime = converter.convert(map.get(DATE_TIME), LocalDateTime.class);
return ZonedDateTime.of(localDateTime, zoneId);
} else {
return fromValueForMultiKey(from, converter, ZonedDateTime.class, ZONED_DATE_TIME_PARAMS);
}
}

static Class<?> toClass(Object from, Converter converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.Year;
import java.time.ZonedDateTime;
import java.util.Calendar;
Expand Down Expand Up @@ -229,11 +230,15 @@ static LocalDateTime toLocalDateTime(Object from, Converter converter) {
static LocalTime toLocalTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalTime();
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return toInstant(from, converter).atZone(converter.getOptions().getZoneId());
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toOffsetDateTime();
}

static Year toYear(Object from, Converter converter) {
if (from instanceof Byte) {
throw new IllegalArgumentException("Cannot convert Byte to Year, not enough precision.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.CompactLinkedMap;

/**
* @author Kenny Partlow ([email protected])
* <br>
Expand All @@ -34,11 +38,6 @@
final class OffsetDateTimeConversions {
private OffsetDateTimeConversions() {}

static OffsetDateTime toDifferentZone(Object from, Converter converter) {
OffsetDateTime offsetDateTime = (OffsetDateTime) from;
return offsetDateTime.toInstant().atZone(converter.getOptions().getZoneId()).toOffsetDateTime();
}

static Instant toInstant(Object from, Converter converter) {
return ((OffsetDateTime)from).toInstant();
}
Expand All @@ -48,15 +47,15 @@ static long toLong(Object from, Converter converter) {
}

static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return toDifferentZone(from, converter).toLocalDateTime();
return ((OffsetDateTime)from).toLocalDateTime();
}

static LocalDate toLocalDate(Object from, Converter converter) {
return toDifferentZone(from, converter).toLocalDate();
return ((OffsetDateTime)from).toLocalDate();
}

static LocalTime toLocalTime(Object from, Converter converter) {
return toDifferentZone(from, converter).toLocalTime();
return ((OffsetDateTime)from).toLocalTime();
}

static AtomicLong toAtomicLong(Object from, Converter converter) {
Expand Down Expand Up @@ -98,4 +97,16 @@ static String toString(Object from, Converter converter) {
OffsetDateTime offsetDateTime = (OffsetDateTime) from;
return offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}

static Map<String, Object> toMap(Object from, Converter converter) {
OffsetDateTime offsetDateTime = (OffsetDateTime) from;

LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
ZoneOffset zoneOffset = offsetDateTime.getOffset();

Map<String, Object> target = new CompactLinkedMap<>();
target.put(MapConversions.DATE_TIME, converter.convert(localDateTime, String.class));
target.put(MapConversions.OFFSET, converter.convert(zoneOffset, String.class));
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;

import com.cedarsoftware.util.DeepEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -38,6 +37,8 @@
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.NullSource;

import com.cedarsoftware.util.DeepEquals;

import static com.cedarsoftware.util.ArrayUtilities.EMPTY_BYTE_ARRAY;
import static com.cedarsoftware.util.ArrayUtilities.EMPTY_CHAR_ARRAY;
import static com.cedarsoftware.util.Converter.zonedDateTimeToMillis;
Expand Down Expand Up @@ -3076,7 +3077,7 @@ void testMapToLocalDateTime()
map.clear();
assertThatThrownBy(() -> this.converter.convert(map, LocalDateTime.class))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("To convert from Map to LocalDateTime the map must include one of the following: [_v], or [value] with associated values");
.hasMessageContaining("To convert from Map to LocalDateTime the map must include one of the following: [date, time], [_v], or [value] with associated values");
}

@Test
Expand All @@ -3095,7 +3096,7 @@ void testMapToZonedDateTime()
map.clear();
assertThatThrownBy(() -> this.converter.convert(map, ZonedDateTime.class))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("To convert from Map to ZonedDateTime the map must include one of the following: [_v], or [value] with associated values");
.hasMessageContaining("To convert from Map to ZonedDateTime the map must include one of the following: [zone, dateTime], [_v], or [value] with associated values");

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.cedarsoftware.util.convert;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.assertj.core.api.Assertions.assertThat;

class ZonedDateTimeConversionsTests {

private Converter converter;


private static final ZoneId TOKYO = ZoneId.of("Asia/Tokyo");
private static final ZoneId CHICAGO = ZoneId.of("America/Chicago");
private static final ZoneId ALASKA = ZoneId.of("America/Anchorage");

private static final ZonedDateTime ZDT_1 = ZonedDateTime.of(LocalDateTime.of(2019, 12, 15, 9, 7, 16, 2000), CHICAGO);
private static final ZonedDateTime ZDT_2 = ZonedDateTime.of(LocalDateTime.of(2027, 12, 23, 9, 7, 16, 2000), TOKYO);
private static final ZonedDateTime ZDT_3 = ZonedDateTime.of(LocalDateTime.of(2027, 12, 23, 9, 7, 16, 2000), ALASKA);

@BeforeEach
public void before() {
// create converter with default options
this.converter = new Converter(new DefaultConverterOptions());
}

private static Stream<Arguments> roundTripZDT() {
return Stream.of(
Arguments.of(ZDT_1),
Arguments.of(ZDT_2),
Arguments.of(ZDT_3)
);
}

@ParameterizedTest
@MethodSource("roundTripZDT")
void testZonedDateTime(ZonedDateTime zdt) {

String value = this.converter.convert(zdt, String.class);
System.out.println(value);
ZonedDateTime actual = this.converter.convert(value, ZonedDateTime.class);
System.out.println(actual);

assertThat(actual).isEqualTo(zdt);
}

}

0 comments on commit b8aec84

Please sign in to comment.