mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
Refactor move ScalarTypeBaseDateTime to ebean-core-type module
This commit is contained in:
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;
|
||||
|
||||
Reference in new issue
Block a user