Refactor move ScalarTypeBaseDateTime to ebean-core-type module

This commit is contained in:
Rob Bygrave committed 2022-08-26 13:02:42 +12:00
1 parent 6f249c7b72
commit f319ee66ad
22 files changed
+59 -46

No files matched your search

+7
View File
@@ -26,6 +26,13 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type; package io.ebean.core.type;
/* /*
* Copyright 2013 FasterXML.com * Copyright 2013 FasterXML.com
@@ -27,7 +27,7 @@ import java.time.Instant;
* @author Nick Williams * @author Nick Williams
* @since 2.2.0 * @since 2.2.0
*/ */
final class DecimalUtils { public final class DecimalUtils {
private static final char[] ZEROES = new char[]{'0', '0', '0', '0', '0', '0', '0', '0', '0'}; private static final char[] ZEROES = new char[]{'0', '0', '0', '0', '0', '0', '0', '0', '0'};
@@ -37,17 +37,17 @@ final class DecimalUtils {
throw new RuntimeException("DecimalUtils cannot be instantiated."); throw new RuntimeException("DecimalUtils cannot be instantiated.");
} }
static Duration toDuration(BigDecimal value) { public static Duration toDuration(BigDecimal value) {
long seconds = value.longValue(); long seconds = value.longValue();
int nanoseconds = extractNanosecondDecimal(value, seconds); int nanoseconds = extractNanosecondDecimal(value, seconds);
return Duration.ofSeconds(seconds, nanoseconds); return Duration.ofSeconds(seconds, nanoseconds);
} }
static BigDecimal toDecimal(Duration instant) { public static BigDecimal toDecimal(Duration instant) {
return new BigDecimal(toDecimal(instant.getSeconds(), instant.getNano())); return new BigDecimal(toDecimal(instant.getSeconds(), instant.getNano()));
} }
static Timestamp toTimestamp(BigDecimal value) { public static Timestamp toTimestamp(BigDecimal value) {
long seconds = value.longValue(); long seconds = value.longValue();
int nanoseconds = extractNanosecondDecimal(value, seconds); int nanoseconds = extractNanosecondDecimal(value, seconds);
Timestamp ts = new Timestamp(seconds * 1000); Timestamp ts = new Timestamp(seconds * 1000);
@@ -55,13 +55,13 @@ final class DecimalUtils {
return ts; return ts;
} }
static BigDecimal toDecimal(Timestamp instant) { public static BigDecimal toDecimal(Timestamp instant) {
long millis = instant.getTime(); long millis = instant.getTime();
long secs = millis / 1000; long secs = millis / 1000;
return new BigDecimal(toDecimal(secs, instant.getNanos())); return new BigDecimal(toDecimal(secs, instant.getNanos()));
} }
static String toDecimal(long seconds, int nanoseconds) { public static String toDecimal(long seconds, int nanoseconds) {
StringBuilder string = new StringBuilder(Integer.toString(nanoseconds)); StringBuilder string = new StringBuilder(Integer.toString(nanoseconds));
if (string.length() < 9) if (string.length() < 9)
string.insert(0, ZEROES, 0, 9 - string.length()); string.insert(0, ZEROES, 0, 9 - string.length());
@@ -1,21 +1,21 @@
package io.ebeaninternal.server.type; package io.ebean.core.type;
import java.time.Instant; import java.time.Instant;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder; import java.time.format.DateTimeFormatterBuilder;
final class IsoJsonDateTimeParser { public final class IsoJsonDateTimeParser {
private static final DateTimeFormatter ISO_MILLIS = new DateTimeFormatterBuilder() private static final DateTimeFormatter ISO_MILLIS = new DateTimeFormatterBuilder()
.parseCaseInsensitive() .parseCaseInsensitive()
.appendInstant(3) .appendInstant(3)
.toFormatter(); .toFormatter();
static Instant parseIso(String jsonDateTime) { public static Instant parseIso(String jsonDateTime) {
return Instant.parse(jsonDateTime); return Instant.parse(jsonDateTime);
} }
static String formatIso(Instant value) { public static String formatIso(Instant value) {
return ISO_MILLIS.format(value); return ISO_MILLIS.format(value);
} }
} }
@@ -1,12 +1,8 @@
package io.ebeaninternal.server.type; package io.ebean.core.type;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.DocPropertyType;
import io.ebean.core.type.ScalarTypeBase;
import java.io.DataInput; import java.io.DataInput;
import java.io.DataOutput; import java.io.DataOutput;
@@ -17,16 +13,14 @@ import java.sql.Timestamp;
import java.sql.Types; import java.sql.Types;
import java.time.Instant; import java.time.Instant;
import static io.ebeaninternal.server.type.IsoJsonDateTimeParser.parseIso;
/** /**
* Base type for DateTime types. * Base type for DateTime types.
*/ */
abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> { public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
protected final JsonConfig.DateTime mode; protected final JsonConfig.DateTime mode;
ScalarTypeBaseDateTime(JsonConfig.DateTime mode, Class<T> type, boolean jdbcNative, int jdbcType) { public ScalarTypeBaseDateTime(JsonConfig.DateTime mode, Class<T> type, boolean jdbcNative, int jdbcType) {
super(type, jdbcNative, jdbcType); super(type, jdbcNative, jdbcType);
this.mode = mode; this.mode = mode;
} }
@@ -75,7 +69,7 @@ abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
* Convert the value to ISO8601 format. * Convert the value to ISO8601 format.
*/ */
protected T fromJsonISO8601(String value) { protected T fromJsonISO8601(String value) {
return convertFromInstant(parseIso(value)); return convertFromInstant(IsoJsonDateTimeParser.parseIso(value));
} }
@Override @Override
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type; package io.ebean.core.type;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -32,6 +32,5 @@ class DecimalUtilsTest {
assertEquals(duration, duration1); assertEquals(duration, duration1);
assertEquals("PT5M23.0015S", duration1.toString()); assertEquals("PT5M23.0015S", duration1.toString());
} }
} }
@@ -1,5 +1,4 @@
package io.ebeaninternal.server.type; package io.ebean.core.type;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -7,37 +6,37 @@ import java.time.Instant;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class IsoJsonDateTimeParserTest { class IsoJsonDateTimeParserTest {
private IsoJsonDateTimeParser parser = new IsoJsonDateTimeParser(); private IsoJsonDateTimeParser parser = new IsoJsonDateTimeParser();
@Test @Test
public void parseFormat_when_hasMillis() { void parseFormat_when_hasMillis() {
parseAndFormat("2016-02-28T20:39:00.123Z", "2016-02-28T20:39:00.123Z"); parseAndFormat("2016-02-28T20:39:00.123Z", "2016-02-28T20:39:00.123Z");
} }
@Test @Test
public void parseFormat_when_noMillis() { void parseFormat_when_noMillis() {
parseAndFormat("2016-02-28T20:39:00Z", "2016-02-28T20:39:00.000Z"); parseAndFormat("2016-02-28T20:39:00Z", "2016-02-28T20:39:00.000Z");
} }
@Test @Test
public void parseFormat_when_millis_1dp() { void parseFormat_when_millis_1dp() {
parseAndFormat("2016-02-28T20:39:00.0Z", "2016-02-28T20:39:00.000Z"); parseAndFormat("2016-02-28T20:39:00.0Z", "2016-02-28T20:39:00.000Z");
} }
@Test @Test
public void parseFormat_when_millis_2dp() { void parseFormat_when_millis_2dp() {
parseAndFormat("2016-02-28T20:39:00.00Z", "2016-02-28T20:39:00.000Z"); parseAndFormat("2016-02-28T20:39:00.00Z", "2016-02-28T20:39:00.000Z");
} }
@Test @Test
public void parseFormat_when_millis_3dp() { void parseFormat_when_millis_3dp() {
parseAndFormat("2016-02-28T20:39:00.000Z", "2016-02-28T20:39:00.000Z"); parseAndFormat("2016-02-28T20:39:00.000Z", "2016-02-28T20:39:00.000Z");
} }
@Test @Test
public void parseFormat_when_millis_3dp_2() { void parseFormat_when_millis_3dp_2() {
parseAndFormat("2016-02-28T20:39:32.999000Z", "2016-02-28T20:39:32.999Z"); parseAndFormat("2016-02-28T20:39:32.999000Z", "2016-02-28T20:39:32.999Z");
} }
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder; import io.ebean.core.type.DataBinder;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter; import io.ebeaninternal.server.core.BasicTypeConverter;
import java.sql.Date; import java.sql.Date;
@@ -11,7 +12,7 @@ import java.sql.Types;
import java.time.Instant; import java.time.Instant;
import java.util.Calendar; import java.util.Calendar;
import static io.ebeaninternal.server.type.IsoJsonDateTimeParser.formatIso; import static io.ebean.core.type.IsoJsonDateTimeParser.formatIso;
/** /**
* ScalarType for java.util.Calendar. * ScalarType for java.util.Calendar.
@@ -2,10 +2,7 @@ package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import io.ebean.core.type.DataBinder; import io.ebean.core.type.*;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.DocPropertyType;
import io.ebean.core.type.ScalarTypeBase;
import io.ebeaninternal.server.core.BasicTypeConverter; import io.ebeaninternal.server.core.BasicTypeConverter;
import java.io.DataInput; import java.io.DataInput;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.Types; import java.sql.Types;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter; import io.ebeaninternal.server.core.BasicTypeConverter;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter; import io.ebeaninternal.server.core.BasicTypeConverter;
import org.joda.time.LocalDateTime; import org.joda.time.LocalDateTime;
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.config.dbplatform.ExtraDbTypes; import io.ebean.config.dbplatform.ExtraDbTypes;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import java.io.IOException; import java.io.IOException;
import java.sql.Timestamp; import java.sql.Timestamp;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.Types; import java.sql.Types;
@@ -8,7 +9,7 @@ import java.time.Instant;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import static io.ebeaninternal.server.type.IsoJsonDateTimeParser.formatIso; import static io.ebean.core.type.IsoJsonDateTimeParser.formatIso;
/** /**
* ScalarType for java.sql.Timestamp. * ScalarType for java.sql.Timestamp.
@@ -3,6 +3,7 @@ package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder; import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader; import io.ebean.core.type.DataReader;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter; import io.ebeaninternal.server.core.BasicTypeConverter;
import java.sql.SQLException; import java.sql.SQLException;
@@ -10,7 +11,7 @@ import java.sql.Timestamp;
import java.sql.Types; import java.sql.Types;
import java.time.Instant; import java.time.Instant;
import static io.ebeaninternal.server.type.IsoJsonDateTimeParser.formatIso; import static io.ebean.core.type.IsoJsonDateTimeParser.formatIso;
/** /**
* ScalarType for java.sql.Timestamp. * ScalarType for java.sql.Timestamp.
@@ -4,6 +4,7 @@ import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder; import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader; import io.ebean.core.type.DataReader;
import io.ebean.core.type.ScalarTypeBaseDate; import io.ebean.core.type.ScalarTypeBaseDate;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter; import io.ebeaninternal.server.core.BasicTypeConverter;
import java.sql.SQLException; import java.sql.SQLException;
@@ -12,14 +13,14 @@ import java.sql.Types;
import java.time.Instant; import java.time.Instant;
import java.util.Date; import java.util.Date;
import static io.ebeaninternal.server.type.IsoJsonDateTimeParser.formatIso; import static io.ebean.core.type.IsoJsonDateTimeParser.formatIso;
/** /**
* ScalarType for java.util.Date. * ScalarType for java.util.Date.
*/ */
final class ScalarTypeUtilDate { final class ScalarTypeUtilDate {
static final class TimestampType extends ScalarTypeBaseDateTime<java.util.Date> { static final class TimestampType extends ScalarTypeBaseDateTime<Date> {
TimestampType(JsonConfig.DateTime mode) { TimestampType(JsonConfig.DateTime mode) {
super(mode, java.util.Date.class, false, Types.TIMESTAMP); super(mode, java.util.Date.class, false, Types.TIMESTAMP);
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.Types; import java.sql.Types;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.Types; import java.sql.Types;
@@ -17,7 +18,7 @@ public class ScalarTypeCalendarTest {
Calendar instance = Calendar.getInstance(); Calendar instance = Calendar.getInstance();
String asUtc = type.toJsonISO8601(instance); String asUtc = type.toJsonISO8601(instance);
Calendar calendar = type.fromJsonISO8601(asUtc); Calendar calendar = type.convertFromInstant(IsoJsonDateTimeParser.parseIso(asUtc)); //type.fromJsonISO8601(asUtc);
assertThat(instance).isEqualTo(calendar); assertThat(instance).isEqualTo(calendar);
} }
@@ -1,5 +1,6 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.core.type.DecimalUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.Timestamp; import java.sql.Timestamp;
@@ -122,7 +123,7 @@ public class ScalarTypeOffsetDateTimeTest {
OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime now = OffsetDateTime.now();
String asJson = typeIso.toJsonISO8601(now); String asJson = typeIso.toJsonISO8601(now);
OffsetDateTime value = typeIso.fromJsonISO8601(asJson); OffsetDateTime value = typeIso.convertFromInstant(IsoJsonDateTimeParser.parseIso(asJson)); //typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualToIgnoringNanos(value); assertThat(now).isEqualToIgnoringNanos(value);
} }
} }
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.Timestamp; import java.sql.Timestamp;
@@ -17,7 +18,7 @@ public class ScalarTypeTimestampTest {
Timestamp now = new Timestamp(System.currentTimeMillis()); Timestamp now = new Timestamp(System.currentTimeMillis());
String asJson = typeIso.toJsonISO8601(now); String asJson = typeIso.toJsonISO8601(now);
Timestamp value = typeIso.fromJsonISO8601(asJson); Timestamp value = typeIso.convertFromInstant(IsoJsonDateTimeParser.parseIso(asJson)); //typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value); assertThat(now).isEqualTo(value);
} }
} }
@@ -1,9 +1,11 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.util.Date; import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@@ -33,7 +35,8 @@ public class ScalarTypeUtilDateTest {
Date now = new Date(); Date now = new Date();
String asJson = typeIso.toJsonISO8601(now); String asJson = typeIso.toJsonISO8601(now);
Date value = typeIso.fromJsonISO8601(asJson); Instant instant = IsoJsonDateTimeParser.parseIso(asJson);
Date value = new java.util.Date(instant.toEpochMilli()); // typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value); assertThat(now).isEqualTo(value);
} }
} }
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig; import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.Timestamp; import java.sql.Timestamp;
@@ -124,7 +125,7 @@ public class ScalarTypeZonedDateTimeTest {
ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime now = ZonedDateTime.now();
String asJson = typeIso.toJsonISO8601(now); String asJson = typeIso.toJsonISO8601(now);
ZonedDateTime value = typeIso.fromJsonISO8601(asJson); ZonedDateTime value = type.convertFromInstant(IsoJsonDateTimeParser.parseIso(asJson)); //typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value); assertThat(now).isEqualTo(value);
} }
} }