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

@@ -1,74 +0,0 @@
package io.ebeaninternal.server.type;
/*
* Copyright 2013 FasterXML.com
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
/**
* Utilities to aid in the translation of decimal types to/from multiple parts.
*
* @author Nick Williams
* @since 2.2.0
*/
final class DecimalUtils {
private static final char[] ZEROES = new char[]{'0', '0', '0', '0', '0', '0', '0', '0', '0'};
private static final BigDecimal ONE_BILLION = new BigDecimal(1000000000L);
private DecimalUtils() {
throw new RuntimeException("DecimalUtils cannot be instantiated.");
}
static Duration toDuration(BigDecimal value) {
long seconds = value.longValue();
int nanoseconds = extractNanosecondDecimal(value, seconds);
return Duration.ofSeconds(seconds, nanoseconds);
}
static BigDecimal toDecimal(Duration instant) {
return new BigDecimal(toDecimal(instant.getSeconds(), instant.getNano()));
}
static Timestamp toTimestamp(BigDecimal value) {
long seconds = value.longValue();
int nanoseconds = extractNanosecondDecimal(value, seconds);
Timestamp ts = new Timestamp(seconds * 1000);
ts.setNanos(nanoseconds);
return ts;
}
static BigDecimal toDecimal(Timestamp instant) {
long millis = instant.getTime();
long secs = millis / 1000;
return new BigDecimal(toDecimal(secs, instant.getNanos()));
}
static String toDecimal(long seconds, int nanoseconds) {
StringBuilder string = new StringBuilder(Integer.toString(nanoseconds));
if (string.length() < 9)
string.insert(0, ZEROES, 0, 9 - string.length());
return seconds + "." + string;
}
static int extractNanosecondDecimal(BigDecimal value, long integer) {
return value.subtract(new BigDecimal(integer)).multiply(ONE_BILLION).intValue();
}
}
@@ -1,21 +0,0 @@
package io.ebeaninternal.server.type;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
final class IsoJsonDateTimeParser {
private static final DateTimeFormatter ISO_MILLIS = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendInstant(3)
.toFormatter();
static Instant parseIso(String jsonDateTime) {
return Instant.parse(jsonDateTime);
}
static String formatIso(Instant value) {
return ISO_MILLIS.format(value);
}
}
@@ -1,185 +0,0 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
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.DataOutput;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import static io.ebeaninternal.server.type.IsoJsonDateTimeParser.parseIso;
/**
* Base type for DateTime types.
*/
abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
protected final JsonConfig.DateTime mode;
ScalarTypeBaseDateTime(JsonConfig.DateTime mode, Class<T> type, boolean jdbcNative, int jdbcType) {
super(type, jdbcNative, jdbcType);
this.mode = mode;
}
@Override
public long asVersion(T value) {
return convertToMillis(value);
}
/**
* Convert the value to a Timestamp.
*/
public abstract Timestamp convertToTimestamp(T t);
/**
* Convert to the value from a Timestamp.
*/
public abstract T convertFromTimestamp(Timestamp ts);
/**
* Convert to the value from a Instant.
*/
public abstract T convertFromInstant(Instant ts);
/**
* Convert from epoch millis to the value.
*/
public abstract T convertFromMillis(long systemTimeMillis);
/**
* Convert from the value to epoch millis.
*/
public abstract long convertToMillis(T value);
/**
* Convert the value to time with nanos format.
*/
protected abstract String toJsonNanos(T value);
/**
* Convert the value to ISO8601 format.
*/
protected abstract String toJsonISO8601(T value);
/**
* Convert the value to ISO8601 format.
*/
protected T fromJsonISO8601(String value) {
return convertFromInstant(parseIso(value));
}
@Override
public void bind(DataBinder binder, T value) throws SQLException {
if (value == null) {
binder.setNull(Types.TIMESTAMP);
} else {
binder.setTimestamp(convertToTimestamp(value));
}
}
@Override
public T read(DataReader reader) throws SQLException {
Timestamp ts = reader.getTimestamp();
if (ts == null) {
return null;
} else {
return convertFromTimestamp(ts);
}
}
/**
* Helper method that given epoch seconds and nanos return a JSON nanos formatted string.
*/
protected String toJsonNanos(long epochSecs, int nanos) {
return DecimalUtils.toDecimal(epochSecs, nanos);
}
@Override
public T jsonRead(JsonParser parser) throws IOException {
switch (parser.getCurrentToken()) {
case VALUE_NUMBER_INT: {
return convertFromMillis(parser.getLongValue());
}
case VALUE_NUMBER_FLOAT: {
BigDecimal value = parser.getDecimalValue();
Timestamp timestamp = DecimalUtils.toTimestamp(value);
return convertFromTimestamp(timestamp);
}
default: {
return fromJsonISO8601(parser.getText());
}
}
}
@Override
public void jsonWrite(JsonGenerator writer, T value) throws IOException {
switch (mode) {
case ISO8601: {
writer.writeString(toJsonISO8601(value));
break;
}
case NANOS: {
writer.writeNumber(toJsonNanos(value));
break;
}
default: {
writer.writeNumber(convertToMillis(value));
}
}
}
@Override
public DocPropertyType docType() {
return DocPropertyType.DATETIME;
}
@Override
public String formatValue(T value) {
// format all timestamps into epoch millis
long epochMillis = convertToMillis(value);
return Long.toString(epochMillis);
}
@Override
public T parse(String value) {
try {
long epochMillis = Long.parseLong(value);
return convertFromMillis(epochMillis);
} catch (NumberFormatException e) {
return convertFromTimestamp(Timestamp.valueOf(value));
}
}
@Override
public T readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
return null;
} else {
long val = dataInput.readLong();
Timestamp ts = new Timestamp(val);
return convertFromTimestamp(ts);
}
}
@Override
public void writeData(DataOutput dataOutput, T value) throws IOException {
if (value == null) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
Timestamp ts = convertToTimestamp(value);
dataOutput.writeLong(ts.getTime());
}
}
}
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter;
import java.sql.Date;
@@ -11,7 +12,7 @@ import java.sql.Types;
import java.time.Instant;
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.
@@ -2,10 +2,7 @@ package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
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 io.ebean.core.type.*;
import io.ebeaninternal.server.core.BasicTypeConverter;
import java.io.DataInput;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import java.sql.Timestamp;
import java.sql.Types;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter;
import org.joda.time.DateTime;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter;
import org.joda.time.LocalDateTime;
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import io.ebean.config.JsonConfig;
import io.ebean.config.dbplatform.ExtraDbTypes;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import java.io.IOException;
import java.sql.Timestamp;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import java.sql.Timestamp;
import java.sql.Types;
@@ -8,7 +9,7 @@ import java.time.Instant;
import java.time.OffsetDateTime;
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.
@@ -3,6 +3,7 @@ package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter;
import java.sql.SQLException;
@@ -10,7 +11,7 @@ import java.sql.Timestamp;
import java.sql.Types;
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.
@@ -4,6 +4,7 @@ import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.ScalarTypeBaseDate;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebeaninternal.server.core.BasicTypeConverter;
import java.sql.SQLException;
@@ -12,14 +13,14 @@ import java.sql.Types;
import java.time.Instant;
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.
*/
final class ScalarTypeUtilDate {
static final class TimestampType extends ScalarTypeBaseDateTime<java.util.Date> {
static final class TimestampType extends ScalarTypeBaseDateTime<Date> {
TimestampType(JsonConfig.DateTime mode) {
super(mode, java.util.Date.class, false, Types.TIMESTAMP);
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import java.sql.Timestamp;
import java.sql.Types;
@@ -1,37 +0,0 @@
package io.ebeaninternal.server.type;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DecimalUtilsTest {
@Test
void testToDecimal() {
Instant now = Instant.now();
Timestamp sourceTimestamp = Timestamp.from(now);
BigDecimal decimal = DecimalUtils.toDecimal(sourceTimestamp);
Timestamp timestamp = DecimalUtils.toTimestamp(decimal);
assertEquals(now, timestamp.toInstant());
assertEquals(sourceTimestamp, timestamp);
}
@Test
void testDuration() {
Duration duration = Duration.ofSeconds(323, 1500000);
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration);
Duration duration1 = DecimalUtils.toDuration(bigDecimal);
assertEquals(duration, duration1);
assertEquals("PT5M23.0015S", duration1.toString());
}
}
@@ -1,49 +0,0 @@
package io.ebeaninternal.server.type;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import static org.assertj.core.api.Assertions.assertThat;
public class IsoJsonDateTimeParserTest {
private IsoJsonDateTimeParser parser = new IsoJsonDateTimeParser();
@Test
public void parseFormat_when_hasMillis() {
parseAndFormat("2016-02-28T20:39:00.123Z", "2016-02-28T20:39:00.123Z");
}
@Test
public void parseFormat_when_noMillis() {
parseAndFormat("2016-02-28T20:39:00Z", "2016-02-28T20:39:00.000Z");
}
@Test
public void parseFormat_when_millis_1dp() {
parseAndFormat("2016-02-28T20:39:00.0Z", "2016-02-28T20:39:00.000Z");
}
@Test
public void parseFormat_when_millis_2dp() {
parseAndFormat("2016-02-28T20:39:00.00Z", "2016-02-28T20:39:00.000Z");
}
@Test
public void parseFormat_when_millis_3dp() {
parseAndFormat("2016-02-28T20:39:00.000Z", "2016-02-28T20:39:00.000Z");
}
@Test
public void parseFormat_when_millis_3dp_2() {
parseAndFormat("2016-02-28T20:39:32.999000Z", "2016-02-28T20:39:32.999Z");
}
private void parseAndFormat(String input, String expected) {
Instant timestamp = parser.parseIso(input);
String format = parser.formatIso(timestamp);
assertThat(format).isEqualTo(expected);
}
}
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test;
import java.sql.Types;
@@ -17,7 +18,7 @@ public class ScalarTypeCalendarTest {
Calendar instance = Calendar.getInstance();
String asUtc = type.toJsonISO8601(instance);
Calendar calendar = type.fromJsonISO8601(asUtc);
Calendar calendar = type.convertFromInstant(IsoJsonDateTimeParser.parseIso(asUtc)); //type.fromJsonISO8601(asUtc);
assertThat(instance).isEqualTo(calendar);
}
@@ -1,5 +1,6 @@
package io.ebeaninternal.server.type;
import io.ebean.core.type.DecimalUtils;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test;
import java.sql.Timestamp;
@@ -122,7 +123,7 @@ public class ScalarTypeOffsetDateTimeTest {
OffsetDateTime now = OffsetDateTime.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);
}
}
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test;
import java.sql.Timestamp;
@@ -17,7 +18,7 @@ public class ScalarTypeTimestampTest {
Timestamp now = new Timestamp(System.currentTimeMillis());
String asJson = typeIso.toJsonISO8601(now);
Timestamp value = typeIso.fromJsonISO8601(asJson);
Timestamp value = typeIso.convertFromInstant(IsoJsonDateTimeParser.parseIso(asJson)); //typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value);
}
}
@@ -1,9 +1,11 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.time.Instant;
import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat;
@@ -33,7 +35,8 @@ public class ScalarTypeUtilDateTest {
Date now = new Date();
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);
}
}
@@ -1,6 +1,7 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.IsoJsonDateTimeParser;
import org.junit.jupiter.api.Test;
import java.sql.Timestamp;
@@ -124,7 +125,7 @@ public class ScalarTypeZonedDateTimeTest {
ZonedDateTime now = ZonedDateTime.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);
}
}