#2716 - Refactor ScalarType interface to make use of default methods

This commit is contained in:
Rob Bygrave committed 2022-06-13 22:38:15 +12:00
1 parent e29d83a379
commit 5be3b7717c
28 files changed
+65 -446

No files matched your search

@@ -18,41 +18,6 @@ abstract class ScalarTypeBase<T> implements ScalarType<T> {
this.jdbcType = jdbcType;
}
@Override
public long asVersion(T value) {
throw new RuntimeException("not supported");
}
@Override
public boolean isBinaryType() {
// override for binary/byte based types
return false;
}
/**
* Default implementation of mutable false.
*/
@Override
public boolean isMutable() {
return false;
}
/**
* Default to true.
*/
@Override
public boolean isDirty(Object value) {
return true;
}
/**
* Just return 0.
*/
@Override
public int getLength() {
return 0;
}
@Override
public boolean isJdbcNative() {
return jdbcNative;
@@ -88,16 +88,6 @@ abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
return format(value);
}
@Override
public T convertFromMillis(long systemTimeMillis) {
throw new TextException("Not Supported");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
@SuppressWarnings("unchecked")
public String format(Object value) {
@@ -234,7 +234,6 @@ public final class ScalarTypeBoolean {
@Override
public int getLength() {
// typically this will return 1
return Math.max(trueValue.length(), falseValue.length());
}
@@ -329,16 +328,6 @@ public final class ScalarTypeBoolean {
return Boolean.valueOf(value);
}
@Override
public Boolean convertFromMillis(long systemTimeMillis) {
throw new TextException("Not Supported");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public Boolean readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
@@ -85,16 +85,6 @@ final class ScalarTypeByte extends ScalarTypeBase<Byte> {
throw new TextException("Not supported");
}
@Override
public Byte convertFromMillis(long systemTimeMillis) {
throw new TextException("Not Supported");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public Byte readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
@@ -59,16 +59,6 @@ public abstract class ScalarTypeBytesBase extends ScalarTypeBase<byte[]> {
throw new TextException("Not supported");
}
@Override
public byte[] convertFromMillis(long systemTimeMillis) {
throw new TextException("Not supported");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public byte[] readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
@@ -26,26 +26,11 @@ public final class ScalarTypeBytesEncrypted implements ScalarType<byte[]> {
this.dataEncryptSupport = dataEncryptSupport;
}
@Override
public long asVersion(byte[] value) {
throw new RuntimeException("not supported");
}
@Override
public boolean isBinaryType() {
return true;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public boolean isDirty(Object value) {
return false;
}
@Override
public void bind(DataBinder binder, byte[] value) throws SQLException {
value = dataEncryptSupport.encrypt(value);
@@ -93,16 +93,6 @@ class ScalarTypeDuration extends ScalarTypeBase<Duration> {
return Duration.parse(value);
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public Duration convertFromMillis(long systemTimeMillis) {
throw new TextException("Not Supported");
}
@Override
public Duration jsonRead(JsonParser parser) throws IOException {
return Duration.parse(parser.getValueAsString());
@@ -30,11 +30,6 @@ public final class ScalarTypeEncryptedWrapper<T> implements ScalarType<T>, Local
return dataEncryptSupport.encryptObject(formatValue);
}
@Override
public long asVersion(T value) {
throw new RuntimeException("not supported");
}
@Override
public boolean isBinaryType() {
return wrapped.isBinaryType();
@@ -229,16 +229,6 @@ final class ScalarTypeEnumStandard {
return Enum.valueOf(enumType, value);
}
@Override
public Object convertFromMillis(long systemTimeMillis) {
throw new TextException("Not Supported");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public Object jsonRead(JsonParser parser) throws IOException {
if (parser.getCodec() != null) {
@@ -39,16 +39,6 @@ class ScalarTypeEnumWithMapping extends ScalarTypeEnumStandard.EnumBase implemen
return enumType == null;
}
@Override
public long asVersion(Object value) {
throw new RuntimeException("not supported");
}
@Override
public boolean isBinaryType() {
return false;
}
/**
* Return the IN values for DB constraint construction.
*/
@@ -129,16 +129,6 @@ final class ScalarTypeFile extends ScalarTypeBase<File> {
throw new TextException("Not supported");
}
@Override
public File convertFromMillis(long systemTimeMillis) {
throw new TextException("Not supported");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public File readData(DataInput dataInput) throws IOException {
// skip reading large file
@@ -80,16 +80,6 @@ abstract class ScalarTypeJsonCollection<T> extends ScalarTypeBase<T> implements
return docPropertyType;
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public T convertFromMillis(long dateTime) {
return null;
}
@Override
public T readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
@@ -215,16 +215,6 @@ abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
}
}
@Override
public final Map convertFromMillis(long dateTime) {
throw new RuntimeException("Should never be called");
}
@Override
public final boolean isDateTimeCapable() {
return false;
}
@Override
public final Map readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
@@ -167,16 +167,6 @@ abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
}
}
@Override
public JsonNode convertFromMillis(long dateTime) {
throw new RuntimeException("Should never be called");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public JsonNode readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
@@ -55,16 +55,6 @@ final class ScalarTypeJsonObjectMapper {
NoMutationDetection(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
super(Object.class, jsonManager, field, dbType, docType);
}
@Override
public boolean isMutable() {
return false;
}
@Override
public boolean isDirty(Object value) {
return false;
}
}
/**
@@ -79,6 +69,11 @@ final class ScalarTypeJsonObjectMapper {
this.jsonb = "jsonb".equals(pgType);
}
@Override
public boolean isMutable() {
return true;
}
@Override
public boolean isJsonMapper() {
return true;
@@ -144,11 +139,6 @@ final class ScalarTypeJsonObjectMapper {
this.objectWriter = helper.objectWriter();
}
@Override
public boolean isMutable() {
return true;
}
@Override
public T read(DataReader reader) throws SQLException {
String json = reader.getString();
@@ -212,16 +202,6 @@ final class ScalarTypeJsonObjectMapper {
return docType;
}
@Override
public final boolean isDateTimeCapable() {
return false;
}
@Override
public final T convertFromMillis(long dateTime) {
throw new IllegalStateException("Not supported");
}
@Override
public final T jsonRead(JsonParser parser) throws IOException {
return objectReader.readValue(parser, deserType);
@@ -78,16 +78,6 @@ final class ScalarTypeMonthDay extends ScalarTypeBase<MonthDay> {
return MonthDay.parse(value);
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public MonthDay convertFromMillis(long dateTime) {
throw new RuntimeException("Not supported on this type");
}
@Override
public MonthDay readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
@@ -22,25 +22,6 @@ class ScalarTypeNotFound implements ScalarType<Void> {
public static final ScalarTypeNotFound INSTANCE = new ScalarTypeNotFound();
private ScalarTypeNotFound() { }
@Override
public boolean isBinaryType() {
throw new UnsupportedOperationException();
}
@Override
public boolean isMutable() {
throw new UnsupportedOperationException();
}
@Override
public boolean isDirty(Object value) {
throw new UnsupportedOperationException();
}
@Override
public int getLength() {
throw new UnsupportedOperationException();
}
@Override
public boolean isJdbcNative() {
@@ -102,21 +83,6 @@ class ScalarTypeNotFound implements ScalarType<Void> {
throw new UnsupportedOperationException();
}
@Override
public boolean isDateTimeCapable() {
throw new UnsupportedOperationException();
}
@Override
public long asVersion(Void value) {
throw new UnsupportedOperationException();
}
@Override
public Void convertFromMillis(long dateTime) {
throw new UnsupportedOperationException();
}
@Override
public Void readData(DataInput dataInput) throws IOException {
throw new UnsupportedOperationException();
@@ -82,16 +82,6 @@ final class ScalarTypePostgresHstore extends ScalarTypeBase<Map> {
}
}
@Override
public Map convertFromMillis(long dateTime) {
throw new RuntimeException("Should never be called");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public Map readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
@@ -57,16 +57,6 @@ final class ScalarTypeShort extends ScalarTypeBase<Short> {
return Short.valueOf(value);
}
@Override
public Short convertFromMillis(long systemTimeMillis) {
throw new TextException("Not Supported");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public Short readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
@@ -25,16 +25,6 @@ abstract class ScalarTypeUUIDBase extends ScalarTypeBase<UUID> implements Scalar
return DbPlatformType.UUID;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public boolean isDirty(Object value) {
return true;
}
@Override
public String format(Object value) {
return String.valueOf(value);
@@ -50,16 +40,6 @@ abstract class ScalarTypeUUIDBase extends ScalarTypeBase<UUID> implements Scalar
return UUID.fromString(value);
}
@Override
public UUID convertFromMillis(long dateTime) {
throw new RuntimeException("Should never be called");
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public UUID toBeanType(Object value) {
return BasicTypeConverter.toUUID(value, false);
@@ -44,8 +44,7 @@ public class ScalarTypeWrapper<B, S> implements ScalarType<B> {
@Override
public long asVersion(B value) {
S unwrapValue = converter.unwrapValue(value);
return scalarType.asVersion(unwrapValue);
return scalarType.asVersion(converter.unwrapValue(value));
}
@Override
@@ -81,16 +81,6 @@ final class ScalarTypeYear extends ScalarTypeBase<Year> {
return Year.parse(value);
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public Year convertFromMillis(long systemTimeMillis) {
throw new TextException("Not Supported");
}
@Override
public Year jsonRead(JsonParser parser) throws IOException {
return Year.of(parser.getIntValue());
@@ -1,6 +1,5 @@
package io.ebeaninternal.server.type;
import io.ebean.text.TextException;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
@@ -11,13 +10,12 @@ import java.time.Duration;
import static org.junit.jupiter.api.Assertions.*;
public class ScalarTypeDurationTest {
class ScalarTypeDurationTest {
ScalarTypeDuration type = new ScalarTypeDuration();
@Test
public void testReadData() throws Exception {
void testReadData() throws Exception {
Duration duration = Duration.ofSeconds(1234);
ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -39,8 +37,7 @@ public class ScalarTypeDurationTest {
}
@Test
public void testToJdbcType() throws Exception {
void testToJdbcType() throws Exception {
Duration duration = Duration.ofSeconds(1234);
long seconds = duration.getSeconds();
@@ -52,8 +49,7 @@ public class ScalarTypeDurationTest {
}
@Test
public void testToBeanType() throws Exception {
void testToBeanType() throws Exception {
Duration duration = Duration.ofSeconds(1234);
long seconds = duration.getSeconds();
@@ -67,37 +63,34 @@ public class ScalarTypeDurationTest {
}
@Test
public void testFormatValue() throws Exception {
void testFormatValue() {
Duration duration = Duration.ofSeconds(1234);
String formatValue = type.formatValue(duration);
assertEquals("PT20M34S", formatValue);
}
@Test
public void testParse() {
void testParse() {
Duration duration = type.parse("PT20M34S");
assertEquals(Duration.ofSeconds(1234), duration);
}
@Test
public void testIsDateTimeCapable() {
void testIsDateTimeCapable() {
assertFalse(type.isDateTimeCapable());
}
@Test
public void testConvertFromMillis() {
assertThrows(TextException.class, () -> type.convertFromMillis(1000));
void testConvertFromMillis() {
assertThrows(UnsupportedOperationException.class, () -> type.convertFromMillis(1000));
}
@Test
public void testJsonRead() throws Exception {
void testJsonRead() throws Exception {
Duration duration = Duration.ofSeconds(1234);
JsonTester<Duration> jsonTester = new JsonTester<>(type);
jsonTester.test(duration);
}
}
@@ -1,6 +1,5 @@
package io.ebeaninternal.server.type;
import io.ebean.text.TextException;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
@@ -12,13 +11,12 @@ import java.time.Duration;
import static org.junit.jupiter.api.Assertions.*;
public class ScalarTypeDurationWithNanosTest {
class ScalarTypeDurationWithNanosTest {
ScalarTypeDurationWithNanos type = new ScalarTypeDurationWithNanos();
@Test
public void testReadData() throws Exception {
void testReadData() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -40,8 +38,7 @@ public class ScalarTypeDurationWithNanosTest {
}
@Test
public void testToJdbcType() throws Exception {
void testToJdbcType() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration);
@@ -53,8 +50,7 @@ public class ScalarTypeDurationWithNanosTest {
}
@Test
public void testToBeanType() throws Exception {
void testToBeanType() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
BigDecimal bigDecimal = DecimalUtils.toDecimal(duration);
@@ -66,32 +62,31 @@ public class ScalarTypeDurationWithNanosTest {
}
@Test
public void testFormatValue() throws Exception {
void testFormatValue() {
Duration duration = Duration.ofSeconds(323, 1500000);
String formatValue = type.formatValue(duration);
assertEquals("PT5M23.0015S", formatValue);
}
@Test
public void testParse() {
void testParse() {
Duration duration = Duration.ofSeconds(323, 1500000);
Duration val1 = type.parse("PT5M23.0015S");
assertEquals(duration, val1);
}
@Test
public void testIsDateTimeCapable() {
void testIsDateTimeCapable() {
assertFalse(type.isDateTimeCapable());
}
@Test
public void testConvertFromMillis() {
assertThrows(TextException.class, () -> type.convertFromMillis(1000));
void testConvertFromMillis() {
assertThrows(UnsupportedOperationException.class, () -> type.convertFromMillis(1000));
}
@Test
public void testJsonRead() throws Exception {
void testJsonRead() throws Exception {
Duration duration = Duration.ofSeconds(323, 1500000);
JsonTester<Duration> jsonTester = new JsonTester<>(type);
@@ -11,13 +11,12 @@ import java.time.Year;
import static org.junit.jupiter.api.Assertions.*;
public class ScalarTypeYearTest {
class ScalarTypeYearTest {
ScalarTypeYear type = new ScalarTypeYear();
@Test
public void testReadData() throws Exception {
void testReadData() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
@@ -37,8 +36,7 @@ public class ScalarTypeYearTest {
}
@Test
public void testToJdbcType() throws Exception {
void testToJdbcType() throws Exception {
Integer year = 2013;
Object val1 = type.toJdbcType(Year.of(2013));
Object val2 = type.toJdbcType(2013);
@@ -50,8 +48,7 @@ public class ScalarTypeYearTest {
}
@Test
public void testToBeanType() throws Exception {
void testToBeanType() throws Exception {
Year year = Year.of(2013);
Year val1 = type.toBeanType(year);
Year val2 = type.toBeanType(2013);
@@ -63,29 +60,29 @@ public class ScalarTypeYearTest {
}
@Test
public void testFormatValue() throws Exception {
void testFormatValue() {
String formatted = type.formatValue(Year.of(2013));
assertEquals("2013", formatted);
}
@Test
public void testParse() {
void testParse() {
Year year = type.parse("2013");
assertEquals(Year.of(2013), year);
}
@Test
public void testIsDateTimeCapable() {
void testIsDateTimeCapable() {
assertFalse(type.isDateTimeCapable());
}
@Test
public void testConvertFromMillis() {
assertThrows(TextException.class, () -> type.convertFromMillis(1000));
void testConvertFromMillis() {
assertThrows(UnsupportedOperationException.class, () -> type.convertFromMillis(1000));
}
@Test
public void testJson() throws Exception {
void testJson() throws Exception {
JsonTester<Year> jsonTester = new JsonTester<>(type);
jsonTester.test(Year.of(2013));
}