WIP Java8 types - ScalarTypeDuration etc

This commit is contained in:
rbygrave
2014-11-17 01:09:43 +13:00
parent 4303bcfd13
commit b71b0fc8e2
9 changed files with 232 additions and 82 deletions
@@ -152,7 +152,7 @@ public class DeployCreateProperties {
Class<?> superClass = beanType.getSuperclass();
if (!superClass.equals(Object.class)) {
// recursively add any properties in the inheritance heirarchy
// recursively add any properties in the inheritance hierarchy
// up to the Object.class level...
createProperties(desc, superClass, level + 1);
}
@@ -39,19 +39,14 @@ public abstract class ScalarTypeBaseDate<T> extends ScalarTypeBase<T> {
if (value == null) {
b.setNull(Types.DATE);
} else {
Date date = convertToDate(value);
b.setDate(date);
b.setDate(convertToDate(value));
}
}
public T read(DataReader dataReader) throws SQLException {
Date ts = dataReader.getDate();
if (ts == null) {
return null;
} else {
return convertFromDate(ts);
}
return ts == null ? null : convertFromDate(ts);
}
public String formatValue(T t) {
@@ -9,10 +9,10 @@ import com.fasterxml.jackson.core.JsonToken;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Types;
import java.time.Duration;
import java.time.LocalTime;
/**
* ScalarType for java.time.Duration
@@ -27,6 +27,14 @@ public class ScalarTypeDuration extends ScalarTypeBase<Duration> {
super(Duration.class, false, jdbcType);
}
public BigDecimal convertToBigDecimal(Duration value) {
return (value == null) ? null : DecimalUtils.toDecimal(value);
}
public Duration convertFromBigDecimal(BigDecimal value) {
return (value == null) ? null : DecimalUtils.toDuration(value);
}
@Override
public void bind(DataBind bind, Duration value) throws SQLException {
if (value == null) {
@@ -38,26 +46,8 @@ public class ScalarTypeDuration extends ScalarTypeBase<Duration> {
@Override
public Duration read(DataReader dataReader) throws SQLException {
return Duration.ofSeconds(dataReader.getLong());
}
@Override
public Duration readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
return null;
} else {
return Duration.ofSeconds(dataInput.readLong());
}
}
@Override
public void writeData(DataOutput dataOutput, Duration value) throws IOException {
if (value == null) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
dataOutput.writeLong(value.getSeconds());
}
Long value = dataReader.getLong();
return (value == null) ? null : Duration.ofSeconds(value);
}
@Override
@@ -72,6 +62,26 @@ public class ScalarTypeDuration extends ScalarTypeBase<Duration> {
return Duration.ofSeconds(BasicTypeConverter.toLong(value));
}
@Override
public Duration readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
return null;
} else {
return convertFromBigDecimal(new BigDecimal(dataInput.readUTF()));
}
}
@Override
public void writeData(DataOutput dataOutput, Duration value) throws IOException {
if (value == null) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
dataOutput.writeUTF(convertToBigDecimal(value).toString());
}
}
@Override
public String formatValue(Duration v) {
return v.toString();
@@ -26,14 +26,6 @@ public class ScalarTypeDurationWithNanos extends ScalarTypeDuration {
super(Types.DECIMAL);
}
public BigDecimal convertToBigDecimal(Duration value) {
return DecimalUtils.toDecimal(value);
}
public Duration convertFromBigDecimal(BigDecimal value) {
return DecimalUtils.toDuration(value);
}
@Override
public void bind(DataBind bind, Duration value) throws SQLException {
if (value == null) {
@@ -48,25 +40,6 @@ public class ScalarTypeDurationWithNanos extends ScalarTypeDuration {
return convertFromBigDecimal(dataReader.getBigDecimal());
}
@Override
public Duration readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
return null;
} else {
return convertFromBigDecimal(new BigDecimal(dataInput.readUTF()));
}
}
@Override
public void writeData(DataOutput dataOutput, Duration value) throws IOException {
if (value == null) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
dataOutput.writeUTF(convertToBigDecimal(value).toString());
}
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof BigDecimal) return value;
@@ -10,31 +10,48 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Types;
import java.time.LocalTime;
import java.time.Year;
/**
* ScalarType for java.time.Year
* ScalarType for java.time.LocalTime stored as JDBC Time.
*/
public class ScalarTypeLocalTime extends ScalarTypeBase<LocalTime> {
public ScalarTypeLocalTime() {
super(LocalTime.class, true, Types.BIGINT);
super(LocalTime.class, false, Types.TIME);
}
protected ScalarTypeLocalTime(int jdbcTtype) {
super(LocalTime.class, false, jdbcTtype);
}
@Override
public void bind(DataBind bind, LocalTime value) throws SQLException {
if (value == null) {
bind.setNull(Types.BIGINT);
bind.setNull(Types.TIME);
} else {
bind.setLong(value.toNanoOfDay());
bind.setTime(Time.valueOf(value));
}
}
@Override
public LocalTime read(DataReader dataReader) throws SQLException {
return LocalTime.ofNanoOfDay(dataReader.getLong());
Time time = dataReader.getTime();
return (time == null) ? null : time.toLocalTime();
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof Time) return value;
return Time.valueOf((LocalTime)value);
}
@Override
public LocalTime toBeanType(Object value) {
if (value instanceof LocalTime) return (LocalTime) value;
return BasicTypeConverter.toTime(value).toLocalTime();
}
@Override
@@ -56,18 +73,6 @@ public class ScalarTypeLocalTime extends ScalarTypeBase<LocalTime> {
}
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof Long) return value;
return ((LocalTime)value).toNanoOfDay();
}
@Override
public LocalTime toBeanType(Object value) {
if (value instanceof LocalTime) return (LocalTime) value;
return LocalTime.ofNanoOfDay(BasicTypeConverter.toLong(value));
}
@Override
public String formatValue(LocalTime v) {
return v.toString();
@@ -0,0 +1,45 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
import java.sql.SQLException;
import java.sql.Types;
import java.time.LocalTime;
/**
* ScalarType for java.time.LocalTime stored with Nanos as DB BIGINT type.
*/
public class ScalarTypeLocalTimeWithNanos extends ScalarTypeLocalTime {
public ScalarTypeLocalTimeWithNanos() {
super(Types.BIGINT);
}
@Override
public void bind(DataBind bind, LocalTime value) throws SQLException {
if (value == null) {
bind.setNull(Types.BIGINT);
} else {
bind.setLong(value.toNanoOfDay());
}
}
@Override
public LocalTime read(DataReader dataReader) throws SQLException {
Long value = dataReader.getLong();
return (value == null) ? null : LocalTime.ofNanoOfDay(value);
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof Long) return value;
return ((LocalTime)value).toNanoOfDay();
}
@Override
public LocalTime toBeanType(Object value) {
if (value instanceof LocalTime) return (LocalTime) value;
return LocalTime.ofNanoOfDay(BasicTypeConverter.toLong(value));
}
}
@@ -33,7 +33,8 @@ public class ScalarTypeYear extends ScalarTypeBase<Year> {
@Override
public Year read(DataReader dataReader) throws SQLException {
return Year.of(dataReader.getInt());
Integer value = dataReader.getInt();
return (value == null) ? null : Year.of(value);
}
@Override