Refactor ScalarTypeBaseDateTime, merge into ScalarTypeUtils both IsoJsonDateTimeParser and DecimalUtils

That is, merge the static util methods into ScalarTypeUtils
This commit is contained in:
Rob Bygrave committed 2022-08-26 13:15:55 +12:00
1 parent f319ee66ad
commit a13f581a65
16 files changed
+69 -74

No files matched your search

@@ -1,21 +0,0 @@
package io.ebean.core.type;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
public final class IsoJsonDateTimeParser {
private static final DateTimeFormatter ISO_MILLIS = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendInstant(3)
.toFormatter();
public static Instant parseIso(String jsonDateTime) {
return Instant.parse(jsonDateTime);
}
public static String formatIso(Instant value) {
return ISO_MILLIS.format(value);
}
}
@@ -69,7 +69,7 @@ public 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(IsoJsonDateTimeParser.parseIso(value)); return convertFromInstant(ScalarTypeUtils.parseInstant(value));
} }
@Override @Override
@@ -95,7 +95,7 @@ public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
* Helper method that given epoch seconds and nanos return a JSON nanos formatted string. * Helper method that given epoch seconds and nanos return a JSON nanos formatted string.
*/ */
protected String toJsonNanos(long epochSecs, int nanos) { protected String toJsonNanos(long epochSecs, int nanos) {
return DecimalUtils.toDecimal(epochSecs, nanos); return ScalarTypeUtils.toDecimal(epochSecs, nanos);
} }
@Override @Override
@@ -106,7 +106,7 @@ public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
} }
case VALUE_NUMBER_FLOAT: { case VALUE_NUMBER_FLOAT: {
BigDecimal value = parser.getDecimalValue(); BigDecimal value = parser.getDecimalValue();
Timestamp timestamp = DecimalUtils.toTimestamp(value); Timestamp timestamp = ScalarTypeUtils.toTimestamp(value);
return convertFromTimestamp(timestamp); return convertFromTimestamp(timestamp);
} }
default: { default: {
@@ -1,25 +1,11 @@
package io.ebean.core.type; package io.ebean.core.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.math.BigDecimal;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
/** /**
* Utilities to aid in the translation of decimal types to/from multiple parts. * Utilities to aid in the translation of decimal types to/from multiple parts.
@@ -27,26 +13,50 @@ import java.time.Instant;
* @author Nick Williams * @author Nick Williams
* @since 2.2.0 * @since 2.2.0
*/ */
public final class DecimalUtils { public final class ScalarTypeUtils {
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'};
private static final BigDecimal ONE_BILLION = new BigDecimal(1000000000L); private static final BigDecimal ONE_BILLION = new BigDecimal(1000000000L);
private DecimalUtils() { private static final DateTimeFormatter ISO_MILLIS = new DateTimeFormatterBuilder()
throw new RuntimeException("DecimalUtils cannot be instantiated."); .parseCaseInsensitive()
.appendInstant(3)
.toFormatter();
/**
* Parse using ISO8601.
*/
public static Instant parseInstant(String jsonDateTime) {
return Instant.parse(jsonDateTime);
} }
/**
* Format using ISO8601.
*/
public static String formatInstant(Instant value) {
return ISO_MILLIS.format(value);
}
/**
* Convert decimal to duration with nanos.
*/
public 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);
} }
/**
* Convert duration to decimal with nanos.
*/
public 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()));
} }
/**
* Convert decimal to timestamp with nanos.
*/
public 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);
@@ -55,12 +65,18 @@ public final class DecimalUtils {
return ts; return ts;
} }
/**
* Convert timestamp to decimal with nanos.
*/
public 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()));
} }
/**
* Convert to decimal string with nanos.
*/
public 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)
@@ -68,7 +84,7 @@ public final class DecimalUtils {
return seconds + "." + string; return seconds + "." + string;
} }
static int extractNanosecondDecimal(BigDecimal value, long integer) { private static int extractNanosecondDecimal(BigDecimal value, long integer) {
return value.subtract(new BigDecimal(integer)).multiply(ONE_BILLION).intValue(); return value.subtract(new BigDecimal(integer)).multiply(ONE_BILLION).intValue();
} }
} }
@@ -16,8 +16,8 @@ class DecimalUtilsTest {
Instant now = Instant.now(); Instant now = Instant.now();
Timestamp sourceTimestamp = Timestamp.from(now); Timestamp sourceTimestamp = Timestamp.from(now);
BigDecimal decimal = DecimalUtils.toDecimal(sourceTimestamp); BigDecimal decimal = ScalarTypeUtils.toDecimal(sourceTimestamp);
Timestamp timestamp = DecimalUtils.toTimestamp(decimal); Timestamp timestamp = ScalarTypeUtils.toTimestamp(decimal);
assertEquals(now, timestamp.toInstant()); assertEquals(now, timestamp.toInstant());
assertEquals(sourceTimestamp, timestamp); assertEquals(sourceTimestamp, timestamp);
@@ -27,8 +27,8 @@ class DecimalUtilsTest {
void testDuration() { void testDuration() {
Duration duration = Duration.ofSeconds(323, 1500000); Duration duration = Duration.ofSeconds(323, 1500000);
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration); BigDecimal bigDecimal = ScalarTypeUtils.toDecimal(duration);
Duration duration1 = DecimalUtils.toDuration(bigDecimal); Duration duration1 = ScalarTypeUtils.toDuration(bigDecimal);
assertEquals(duration, duration1); assertEquals(duration, duration1);
assertEquals("PT5M23.0015S", duration1.toString()); assertEquals("PT5M23.0015S", duration1.toString());
@@ -8,7 +8,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class IsoJsonDateTimeParserTest { class IsoJsonDateTimeParserTest {
private IsoJsonDateTimeParser parser = new IsoJsonDateTimeParser(); private ScalarTypeUtils parser = new ScalarTypeUtils();
@Test @Test
void parseFormat_when_hasMillis() { void parseFormat_when_hasMillis() {
@@ -41,8 +41,8 @@ class IsoJsonDateTimeParserTest {
} }
private void parseAndFormat(String input, String expected) { private void parseAndFormat(String input, String expected) {
Instant timestamp = parser.parseIso(input); Instant timestamp = parser.parseInstant(input);
String format = parser.formatIso(timestamp); String format = parser.formatInstant(timestamp);
assertThat(format).isEqualTo(expected); assertThat(format).isEqualTo(expected);
} }
} }
@@ -12,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.ebean.core.type.IsoJsonDateTimeParser.formatIso; import static io.ebean.core.type.ScalarTypeUtils.formatInstant;
/** /**
* ScalarType for java.util.Calendar. * ScalarType for java.util.Calendar.
@@ -66,7 +66,7 @@ final class ScalarTypeCalendar extends ScalarTypeBaseDateTime<Calendar> {
@Override @Override
protected String toJsonISO8601(Calendar value) { protected String toJsonISO8601(Calendar value) {
return formatIso(value.toInstant()); return formatInstant(value.toInstant());
} }
@Override @Override
@@ -27,11 +27,11 @@ class ScalarTypeDuration extends ScalarTypeBase<Duration> {
} }
BigDecimal convertToBigDecimal(Duration value) { BigDecimal convertToBigDecimal(Duration value) {
return (value == null) ? null : DecimalUtils.toDecimal(value); return (value == null) ? null : ScalarTypeUtils.toDecimal(value);
} }
Duration convertFromBigDecimal(BigDecimal value) { Duration convertFromBigDecimal(BigDecimal value) {
return (value == null) ? null : DecimalUtils.toDuration(value); return (value == null) ? null : ScalarTypeUtils.toDuration(value);
} }
@Override @Override
@@ -9,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.ebean.core.type.IsoJsonDateTimeParser.formatIso; import static io.ebean.core.type.ScalarTypeUtils.formatInstant;
/** /**
* ScalarType for java.sql.Timestamp. * ScalarType for java.sql.Timestamp.
@@ -30,7 +30,7 @@ final class ScalarTypeOffsetDateTime extends ScalarTypeBaseDateTime<OffsetDateTi
@Override @Override
protected String toJsonISO8601(OffsetDateTime value) { protected String toJsonISO8601(OffsetDateTime value) {
return formatIso(value.toInstant()); return formatInstant(value.toInstant());
} }
@Override @Override
@@ -11,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.ebean.core.type.IsoJsonDateTimeParser.formatIso; import static io.ebean.core.type.ScalarTypeUtils.formatInstant;
/** /**
* ScalarType for java.sql.Timestamp. * ScalarType for java.sql.Timestamp.
@@ -29,7 +29,7 @@ final class ScalarTypeTimestamp extends ScalarTypeBaseDateTime<Timestamp> {
@Override @Override
protected String toJsonISO8601(Timestamp value) { protected String toJsonISO8601(Timestamp value) {
return formatIso(value.toInstant()); return formatInstant(value.toInstant());
} }
@Override @Override
@@ -13,7 +13,7 @@ import java.sql.Types;
import java.time.Instant; import java.time.Instant;
import java.util.Date; import java.util.Date;
import static io.ebean.core.type.IsoJsonDateTimeParser.formatIso; import static io.ebean.core.type.ScalarTypeUtils.formatInstant;
/** /**
* ScalarType for java.util.Date. * ScalarType for java.util.Date.
@@ -33,7 +33,7 @@ final class ScalarTypeUtilDate {
@Override @Override
protected String toJsonISO8601(Date value) { protected String toJsonISO8601(Date value) {
return formatIso(value.toInstant()); return formatInstant(value.toInstant());
} }
@Override @Override
@@ -1,7 +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 io.ebean.core.type.ScalarTypeUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.Types; import java.sql.Types;
@@ -18,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.convertFromInstant(IsoJsonDateTimeParser.parseIso(asUtc)); //type.fromJsonISO8601(asUtc); Calendar calendar = type.convertFromInstant(ScalarTypeUtils.parseInstant(asUtc)); //type.fromJsonISO8601(asUtc);
assertThat(instance).isEqualTo(calendar); assertThat(instance).isEqualTo(calendar);
} }
@@ -1,6 +1,6 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import io.ebean.core.type.DecimalUtils; import io.ebean.core.type.ScalarTypeUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@@ -41,7 +41,7 @@ class ScalarTypeDurationWithNanosTest {
@Test @Test
void testToJdbcType() throws Exception { void testToJdbcType() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000); Duration duration = Duration.ofSeconds(323, 1500000);
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration); BigDecimal bigDecimal = ScalarTypeUtils.toDecimal(duration);
Object val1 = type.toJdbcType(duration); Object val1 = type.toJdbcType(duration);
Object val2 = type.toJdbcType(bigDecimal); Object val2 = type.toJdbcType(bigDecimal);
@@ -53,7 +53,7 @@ class ScalarTypeDurationWithNanosTest {
@Test @Test
void testToBeanType() throws Exception { void testToBeanType() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000); Duration duration = Duration.ofSeconds(323, 1500000);
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration); BigDecimal bigDecimal = ScalarTypeUtils.toDecimal(duration);
Duration val1 = type.toBeanType(duration); Duration val1 = type.toBeanType(duration);
Duration val2 = type.toBeanType(bigDecimal); Duration val2 = type.toBeanType(bigDecimal);
@@ -1,7 +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 io.ebean.core.type.ScalarTypeUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.Timestamp; import java.sql.Timestamp;
@@ -123,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.convertFromInstant(IsoJsonDateTimeParser.parseIso(asJson)); //typeIso.fromJsonISO8601(asJson); OffsetDateTime value = typeIso.convertFromInstant(ScalarTypeUtils.parseInstant(asJson)); //typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualToIgnoringNanos(value); assertThat(now).isEqualToIgnoringNanos(value);
} }
} }
@@ -1,7 +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 io.ebean.core.type.ScalarTypeUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.Timestamp; import java.sql.Timestamp;
@@ -18,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.convertFromInstant(IsoJsonDateTimeParser.parseIso(asJson)); //typeIso.fromJsonISO8601(asJson); Timestamp value = typeIso.convertFromInstant(ScalarTypeUtils.parseInstant(asJson)); //typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value); assertThat(now).isEqualTo(value);
} }
} }
@@ -1,7 +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 io.ebean.core.type.ScalarTypeUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
@@ -35,7 +35,7 @@ public class ScalarTypeUtilDateTest {
Date now = new Date(); Date now = new Date();
String asJson = typeIso.toJsonISO8601(now); String asJson = typeIso.toJsonISO8601(now);
Instant instant = IsoJsonDateTimeParser.parseIso(asJson); Instant instant = ScalarTypeUtils.parseInstant(asJson);
Date value = new java.util.Date(instant.toEpochMilli()); // typeIso.fromJsonISO8601(asJson); Date value = new java.util.Date(instant.toEpochMilli()); // typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value); assertThat(now).isEqualTo(value);
} }
@@ -1,7 +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 io.ebean.core.type.ScalarTypeUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.Timestamp; import java.sql.Timestamp;
@@ -125,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 = type.convertFromInstant(IsoJsonDateTimeParser.parseIso(asJson)); //typeIso.fromJsonISO8601(asJson); ZonedDateTime value = type.convertFromInstant(ScalarTypeUtils.parseInstant(asJson)); //typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value); assertThat(now).isEqualTo(value);
} }
} }