diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonList.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonList.java index 7ca361405..699c7ad0f 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonList.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonList.java @@ -19,12 +19,12 @@ import java.util.List; /** * Types for mapping List in JSON format to DB types VARCHAR, JSON and JSONB. */ -public class ScalarTypeJsonList { +class ScalarTypeJsonList { /** * Return the appropriate ScalarType based requested dbType and if Postgres. */ - public static ScalarType typeFor(boolean postgres, int dbType, DocPropertyType docType, boolean nullable, boolean keepSource) { + static ScalarType typeFor(boolean postgres, int dbType, DocPropertyType docType, boolean nullable, boolean keepSource) { if (postgres) { switch (dbType) { case DbPlatformType.JSONB: @@ -39,8 +39,8 @@ public class ScalarTypeJsonList { /** * List mapped to DB VARCHAR. */ - public static class Varchar extends ScalarTypeJsonList.Base { - public Varchar(DocPropertyType docType, boolean nullable, boolean keepSource) { + static final class Varchar extends ScalarTypeJsonList.Base { + Varchar(DocPropertyType docType, boolean nullable, boolean keepSource) { super(Types.VARCHAR, docType, nullable, keepSource); } } @@ -48,8 +48,8 @@ public class ScalarTypeJsonList { /** * List mapped to Postgres JSON. */ - private static class Json extends ScalarTypeJsonList.PgBase { - public Json(DocPropertyType docType, boolean nullable, boolean keepSource) { + private final static class Json extends ScalarTypeJsonList.PgBase { + Json(DocPropertyType docType, boolean nullable, boolean keepSource) { super(DbPlatformType.JSON, PostgresHelper.JSON_TYPE, docType, nullable, keepSource); } } @@ -57,8 +57,8 @@ public class ScalarTypeJsonList { /** * List mapped to Postgres JSONB. */ - private static class JsonB extends ScalarTypeJsonList.PgBase { - public JsonB(DocPropertyType docType, boolean nullable, boolean keepSource) { + private static final class JsonB extends ScalarTypeJsonList.PgBase { + JsonB(DocPropertyType docType, boolean nullable, boolean keepSource) { super(DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE, docType, nullable, keepSource); } } @@ -68,22 +68,22 @@ public class ScalarTypeJsonList { */ @SuppressWarnings("rawtypes") private abstract static class Base extends ScalarTypeJsonCollection { - private final boolean keepSource; + final boolean keepSource; - public Base(int dbType, DocPropertyType docType, boolean nullable, boolean keepSource) { + private Base(int dbType, DocPropertyType docType, boolean nullable, boolean keepSource) { super(List.class, dbType, docType, nullable); this.keepSource = keepSource; } - + @Override - public boolean isJsonMapper() { + public final boolean isJsonMapper() { return keepSource; } @Override - public List read(DataReader reader) throws SQLException { + public final List read(DataReader reader) throws SQLException { String json = reader.getString(); - if (isJsonMapper()) { + if (keepSource) { reader.pushJson(json); } try { @@ -96,7 +96,7 @@ public class ScalarTypeJsonList { @Override public final void bind(DataBinder binder, List value) throws SQLException { - String rawJson = isJsonMapper() ? binder.popJson() : null; + String rawJson = keepSource ? binder.popJson() : null; if (rawJson == null && value != null) { rawJson = formatValue(value); } @@ -115,15 +115,15 @@ public class ScalarTypeJsonList { binder.setString("[]"); } } - + protected void bindRawJson(DataBinder binder, String rawJson) throws SQLException { binder.setString(rawJson); } @Override - public String formatValue(List value) { + public final String formatValue(List value) { if (value.isEmpty()) { - return "[]"; + return "[]"; } try { return EJson.write(value); @@ -133,7 +133,7 @@ public class ScalarTypeJsonList { } @Override - public List parse(String value) { + public final List parse(String value) { try { return EJson.parseList(value, false); } catch (IOException e) { @@ -142,12 +142,12 @@ public class ScalarTypeJsonList { } @Override - public List jsonRead(JsonParser parser) throws IOException { + public final List jsonRead(JsonParser parser) throws IOException { return EJson.parseList(parser, parser.getCurrentToken()); } @Override - public void jsonWrite(JsonGenerator writer, List value) throws IOException { + public final void jsonWrite(JsonGenerator writer, List value) throws IOException { EJson.write(value, writer); } } @@ -165,12 +165,12 @@ public class ScalarTypeJsonList { } @Override - protected void bindRawJson(DataBinder binder, String rawJson) throws SQLException { + protected final void bindRawJson(DataBinder binder, String rawJson) throws SQLException { binder.setObject(PostgresHelper.asObject(pgType, rawJson)); } @Override - protected void bindNull(DataBinder binder) throws SQLException { + protected final void bindNull(DataBinder binder) throws SQLException { binder.setObject(PostgresHelper.asObject(pgType, nullable ? null : "[]")); } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonMap.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonMap.java index 507e7012b..93ecdab79 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonMap.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonMap.java @@ -9,13 +9,7 @@ import io.ebean.core.type.DocPropertyType; import io.ebean.text.TextException; import io.ebean.text.json.EJson; -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.StringWriter; +import java.io.*; import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.sql.Types; @@ -25,12 +19,12 @@ import java.util.Map; * Type which maps Map to various DB types (Clob, Varchar, Blob) in JSON format. */ @SuppressWarnings("rawtypes") -public abstract class ScalarTypeJsonMap extends ScalarTypeBase { +abstract class ScalarTypeJsonMap extends ScalarTypeBase { /** * Return the ScalarType for the requested dbType and postgres. */ - public static ScalarTypeJsonMap typeFor(boolean postgres, int dbType, boolean keepSource) { + static ScalarTypeJsonMap typeFor(boolean postgres, int dbType, boolean keepSource) { switch (dbType) { case Types.VARCHAR: return new ScalarTypeJsonMap.Varchar(keepSource); @@ -47,9 +41,8 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { } } - public static class Clob extends ScalarTypeJsonMap { - - public Clob(boolean keepSource) { + private static final class Clob extends ScalarTypeJsonMap { + Clob(boolean keepSource) { super(Types.CLOB, keepSource); } @@ -59,15 +52,14 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { } } - public static class Varchar extends ScalarTypeJsonMap { - - public Varchar(boolean keepSource) { + private static final class Varchar extends ScalarTypeJsonMap { + Varchar(boolean keepSource) { super(Types.VARCHAR, keepSource); } } - public static class Blob extends ScalarTypeJsonMap { - public Blob(boolean keepSource) { + private static final class Blob extends ScalarTypeJsonMap { + Blob(boolean keepSource) { super(Types.BLOB, keepSource); } @@ -75,23 +67,23 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { public Map read(DataReader reader) throws SQLException { InputStream is = reader.getBinaryStream(); if (is == null) { - if (isJsonMapper()) { + if (keepSource) { reader.pushJson(null); } return null; } try { - if (isJsonMapper()) { - StringWriter rawJson = new StringWriter(); - try (InputStreamReader inputStreamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) { - inputStreamReader.transferTo(rawJson); + if (keepSource) { + StringWriter jsonBuffer = new StringWriter(); + try (InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) { + transferTo(streamReader, jsonBuffer); } - reader.pushJson(rawJson.toString()); - return parse(rawJson.toString()); - + String rawJson = jsonBuffer.toString(); + reader.pushJson(rawJson); + return parse(rawJson); } else { - try (InputStreamReader inputStreamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) { - return parse(inputStreamReader); + try (InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8)) { + return parse(streamReader); } } } catch (IOException e) { @@ -99,21 +91,29 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { } } + private static void transferTo(Reader reader, Writer out) throws IOException { + char[] buffer = new char[2048]; + int nRead; + while ((nRead = reader.read(buffer, 0, 2048)) >= 0) { + out.write(buffer, 0, nRead); + } + } + @Override protected void bindNull(DataBinder binder) throws SQLException { binder.setNull(Types.BLOB); } - + @Override protected void bindJson(DataBinder binder, String rawJson) throws SQLException { binder.setBytes(rawJson.getBytes(StandardCharsets.UTF_8)); } - + } - private final boolean keepSource; - - public ScalarTypeJsonMap(int jdbcType, boolean keepSource) { + final boolean keepSource; + + ScalarTypeJsonMap(int jdbcType, boolean keepSource) { super(Map.class, false, jdbcType); this.keepSource = keepSource; } @@ -122,7 +122,7 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { * Map is a mutable type. Use the isDirty() method to check for dirty state. */ @Override - public boolean isMutable() { + public final boolean isMutable() { return true; } @@ -133,48 +133,41 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { public boolean isDirty(Object value) { return TypeJsonManager.checkIsDirty(value); } - - - + @Override - public boolean isJsonMapper() { + public final boolean isJsonMapper() { return keepSource; } - @Override public Map read(DataReader reader) throws SQLException { String rawJson = readJson(reader); - if (isJsonMapper()) { + if (keepSource) { reader.pushJson(rawJson); } - if (rawJson == null) { return null; } - return parse(rawJson); } - protected String readJson(DataReader reader) throws SQLException { return reader.getString(); } - + @Override public final void bind(DataBinder binder, Map value) throws SQLException { - String rawJson = isJsonMapper() ? binder.popJson() : null; + String rawJson = keepSource ? binder.popJson() : null; if (rawJson == null && value != null) { rawJson = formatValue(value); } - if (value == null) { bindNull(binder); } else { bindJson(binder, rawJson); } } - + protected void bindNull(DataBinder binder) throws SQLException { binder.setNull(Types.VARCHAR); } @@ -182,19 +175,19 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { protected void bindJson(DataBinder binder, String rawJson) throws SQLException { binder.setString(rawJson); } - + @Override - public Object toJdbcType(Object value) { + public final Object toJdbcType(Object value) { return value; } @Override - public Map toBeanType(Object value) { + public final Map toBeanType(Object value) { return (Map) value; } @Override - public String formatValue(Map v) { + public final String formatValue(Map v) { try { return EJson.write(v); } catch (IOException e) { @@ -203,7 +196,7 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { } @Override - public Map parse(String value) { + public final Map parse(String value) { try { // return a modify aware map return EJson.parseObject(value, true); @@ -212,7 +205,7 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { } } - public Map parse(Reader reader) { + public final Map parse(Reader reader) { try { // return a modify aware map return EJson.parseObject(reader, true); @@ -222,17 +215,17 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { } @Override - public Map convertFromMillis(long dateTime) { + public final Map convertFromMillis(long dateTime) { throw new RuntimeException("Should never be called"); } @Override - public boolean isDateTimeCapable() { + public final boolean isDateTimeCapable() { return false; } @Override - public Map readData(DataInput dataInput) throws IOException { + public final Map readData(DataInput dataInput) throws IOException { if (!dataInput.readBoolean()) { return null; } else { @@ -241,7 +234,7 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { } @Override - public void writeData(DataOutput dataOutput, Map map) throws IOException { + public final void writeData(DataOutput dataOutput, Map map) throws IOException { if (map == null) { dataOutput.writeBoolean(false); } else { @@ -250,17 +243,17 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase { } @Override - public void jsonWrite(JsonGenerator writer, Map value) throws IOException { + public final void jsonWrite(JsonGenerator writer, Map value) throws IOException { EJson.write(value, writer); } @Override - public Map jsonRead(JsonParser parser) throws IOException { + public final Map jsonRead(JsonParser parser) throws IOException { return EJson.parseObject(parser, parser.getCurrentToken()); } @Override - public DocPropertyType getDocType() { + public final DocPropertyType getDocType() { return DocPropertyType.OBJECT; } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonMapPostgres.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonMapPostgres.java index 46a585ebc..ae33fa0e4 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonMapPostgres.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonMapPostgres.java @@ -8,9 +8,9 @@ import java.sql.SQLException; /** * Support for the Postgres DB types JSON and JSONB. */ -public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap { +abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap { - final String postgresType; + private final String postgresType; ScalarTypeJsonMapPostgres(int jdbcType, String postgresType, boolean keepSource) { super(jdbcType, keepSource); @@ -18,21 +18,21 @@ public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap { } @Override - protected void bindNull(DataBinder binder) throws SQLException { + protected final void bindNull(DataBinder binder) throws SQLException { binder.setObject(PostgresHelper.asObject(postgresType, null)); } - + @Override - protected void bindJson(DataBinder binder, String rawJson) throws SQLException { + protected final void bindJson(DataBinder binder, String rawJson) throws SQLException { binder.setObject(PostgresHelper.asObject(postgresType, rawJson)); } /** * ScalarType mapping java Map type to Postgres JSON database type. */ - public static class JSON extends ScalarTypeJsonMapPostgres { + static final class JSON extends ScalarTypeJsonMapPostgres { - public JSON(boolean keepSource) { + JSON(boolean keepSource) { super(DbPlatformType.JSON, PostgresHelper.JSON_TYPE, keepSource); } } @@ -40,9 +40,9 @@ public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap { /** * ScalarType mapping java Map type to Postgres JSONB database type. */ - public static class JSONB extends ScalarTypeJsonMapPostgres { + static final class JSONB extends ScalarTypeJsonMapPostgres { - public JSONB(boolean keepSource) { + JSONB(boolean keepSource) { super(DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE, keepSource); } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonObjectMapper.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonObjectMapper.java index 41cb28d49..65cb88cdc 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonObjectMapper.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonObjectMapper.java @@ -46,7 +46,7 @@ class ScalarTypeJsonObjectMapper { /** * No mutation detection on this json property. */ - private static class NoMutationDetection extends Base { + private static final class NoMutationDetection extends Base { NoMutationDetection(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { super(Object.class, jsonManager, field, dbType, docType); @@ -66,7 +66,7 @@ class ScalarTypeJsonObjectMapper { /** * Supports HASH and SOURCE dirty detection modes. */ - private static class GenericObject extends Base { + private static final class GenericObject extends Base { GenericObject(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { super(Object.class, jsonManager, field, dbType, docType); @@ -167,20 +167,20 @@ class ScalarTypeJsonObjectMapper { } @Override - public Object toJdbcType(Object value) { + public final Object toJdbcType(Object value) { // no type conversion supported return value; } @Override @SuppressWarnings("unchecked") - public T toBeanType(Object value) { + public final T toBeanType(Object value) { // no type conversion supported return (T) value; } @Override - public String formatValue(T value) { + public final String formatValue(T value) { try { return objectWriter.writeValueAsString(value); } catch (JsonProcessingException e) { @@ -189,7 +189,7 @@ class ScalarTypeJsonObjectMapper { } @Override - public T parse(String value) { + public final T parse(String value) { try { return objectReader.readValue(value, deserType); } catch (IOException e) { @@ -198,32 +198,32 @@ class ScalarTypeJsonObjectMapper { } @Override - public DocPropertyType getDocType() { + public final DocPropertyType getDocType() { return docType; } @Override - public boolean isDateTimeCapable() { + public final boolean isDateTimeCapable() { return false; } @Override - public T convertFromMillis(long dateTime) { + public final T convertFromMillis(long dateTime) { throw new IllegalStateException("Not supported"); } @Override - public T jsonRead(JsonParser parser) throws IOException { + public final T jsonRead(JsonParser parser) throws IOException { return objectReader.readValue(parser, deserType); } @Override - public void jsonWrite(JsonGenerator writer, T value) throws IOException { + public final void jsonWrite(JsonGenerator writer, T value) throws IOException { objectWriter.writeValue(writer, value); } @Override - public T readData(DataInput dataInput) throws IOException { + public final T readData(DataInput dataInput) throws IOException { if (!dataInput.readBoolean()) { return null; } else { @@ -232,7 +232,7 @@ class ScalarTypeJsonObjectMapper { } @Override - public void writeData(DataOutput dataOutput, T value) throws IOException { + public final void writeData(DataOutput dataOutput, T value) throws IOException { if (value == null) { dataOutput.writeBoolean(false); } else { diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonSet.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonSet.java index 7e728b0d3..86a39443e 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonSet.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonSet.java @@ -21,12 +21,12 @@ import java.util.Set; /** * Types for mapping List in JSON format to DB types VARCHAR, JSON and JSONB. */ -public class ScalarTypeJsonSet { +final class ScalarTypeJsonSet { /** * Return the appropriate ScalarType for the requested dbType and Postgres. */ - public static ScalarType typeFor(boolean postgres, int dbType, DocPropertyType docPropertyType, boolean nullable, boolean keepSource) { + static ScalarType typeFor(boolean postgres, int dbType, DocPropertyType docPropertyType, boolean nullable, boolean keepSource) { if (postgres) { switch (dbType) { case DbPlatformType.JSONB: @@ -41,7 +41,7 @@ public class ScalarTypeJsonSet { /** * List mapped to DB VARCHAR. */ - public static class Varchar extends ScalarTypeJsonSet.Base { + static final class Varchar extends ScalarTypeJsonSet.Base { public Varchar(DocPropertyType docPropertyType, boolean nullable, boolean keepSource) { super(Types.VARCHAR, docPropertyType, nullable, keepSource); } @@ -50,8 +50,8 @@ public class ScalarTypeJsonSet { /** * List mapped to Postgres JSON. */ - private static class Json extends ScalarTypeJsonSet.PgBase { - public Json(DocPropertyType docPropertyType, boolean nullable, boolean keepSource) { + private static final class Json extends ScalarTypeJsonSet.PgBase { + private Json(DocPropertyType docPropertyType, boolean nullable, boolean keepSource) { super(DbPlatformType.JSON, PostgresHelper.JSON_TYPE, docPropertyType, nullable, keepSource); } } @@ -59,35 +59,31 @@ public class ScalarTypeJsonSet { /** * List mapped to Postgres JSONB. */ - private static class JsonB extends ScalarTypeJsonSet.PgBase { - public JsonB(DocPropertyType docPropertyType, boolean nullable, boolean keepSource) { + private static final class JsonB extends ScalarTypeJsonSet.PgBase { + private JsonB(DocPropertyType docPropertyType, boolean nullable, boolean keepSource) { super(DbPlatformType.JSONB, PostgresHelper.JSONB_TYPE, docPropertyType, nullable, keepSource); } } - /** - * Base class for List handling. - */ @SuppressWarnings("rawtypes") private abstract static class Base extends ScalarTypeJsonCollection { - private boolean keepSource; - - public Base(int dbType, DocPropertyType docPropertyType, boolean nullable, boolean keepSource) { + final boolean keepSource; + + private Base(int dbType, DocPropertyType docPropertyType, boolean nullable, boolean keepSource) { super(Set.class, dbType, docPropertyType, nullable); this.keepSource = keepSource; } - @Override - public boolean isJsonMapper() { + public final boolean isJsonMapper() { return keepSource; } - + @Override - public Set read(DataReader reader) throws SQLException { + public final Set read(DataReader reader) throws SQLException { String json = reader.getString(); - if (isJsonMapper()) { + if (keepSource) { reader.pushJson(json); } try { @@ -100,7 +96,7 @@ public class ScalarTypeJsonSet { @Override public final void bind(DataBinder binder, Set value) throws SQLException { - String rawJson = isJsonMapper() ? binder.popJson() : null; + String rawJson = keepSource ? binder.popJson() : null; if (rawJson == null && value != null) { rawJson = formatValue(value); } @@ -125,7 +121,7 @@ public class ScalarTypeJsonSet { } @Override - public String formatValue(Set value) { + public final String formatValue(Set value) { if (value.isEmpty()) { return "[]"; } @@ -137,7 +133,7 @@ public class ScalarTypeJsonSet { } @Override - public Set parse(String value) { + public final Set parse(String value) { try { return convertList(EJson.parseList(value)); } catch (IOException e) { @@ -146,12 +142,12 @@ public class ScalarTypeJsonSet { } @Override - public Set jsonRead(JsonParser parser) throws IOException { + public final Set jsonRead(JsonParser parser) throws IOException { return convertList(EJson.parseList(parser, parser.getCurrentToken())); } @Override - public void jsonWrite(JsonGenerator writer, Set value) throws IOException { + public final void jsonWrite(JsonGenerator writer, Set value) throws IOException { EJson.write(value, writer); } @@ -174,12 +170,12 @@ public class ScalarTypeJsonSet { } @Override - protected void bindRawJson(DataBinder binder, String rawJson) throws SQLException { + protected final void bindRawJson(DataBinder binder, String rawJson) throws SQLException { binder.setObject(PostgresHelper.asObject(pgType, rawJson)); } @Override - protected void bindNull(DataBinder binder) throws SQLException { + protected final void bindNull(DataBinder binder) throws SQLException { binder.setObject(PostgresHelper.asObject(pgType, nullable ? null : "[]")); } }