From 6a02a8d69f048592b14db0cfbf412b2fe0db9b24 Mon Sep 17 00:00:00 2001 From: rbygrave Date: Thu, 14 May 2015 22:42:37 +1200 Subject: [PATCH] No effective change - code format and tidy --- .../server/type/CtCompoundProperty.java | 114 +++-- .../server/type/CtCompoundType.java | 316 ++++++------- .../ebeaninternal/server/type/DataBind.java | 180 ++++---- .../server/type/DataEncryptSupport.java | 62 +-- .../ebeaninternal/server/type/DataReader.java | 54 +-- .../server/type/DefaultTypeFactory.java | 212 +++++---- .../server/type/DefaultTypeManager.java | 37 +- .../server/type/EnumToDbIntegerMap.java | 72 +-- .../server/type/EnumToDbStringMap.java | 61 ++- .../server/type/EnumToDbValueMap.java | 197 ++++---- .../server/type/LongToTimestampConverter.java | 28 +- .../server/type/ModifyAwareCollection.java | 16 +- .../server/type/ModifyAwareMap.java | 17 +- .../server/type/ModifyAwareOwner.java | 6 +- .../server/type/ModifyAwareType.java | 2 +- .../server/type/RsetDataReader.java | 425 +++++++++--------- .../server/type/RsetDataReaderIndexed.java | 36 +- .../server/type/ScalarDataReader.java | 32 +- .../ebeaninternal/server/type/ScalarType.java | 258 +++++------ .../server/type/ScalarTypeBaseDateTime.java | 3 +- .../server/type/ScalarTypeBaseVarchar.java | 6 +- .../server/type/ScalarTypeBigDecimal.java | 2 +- .../server/type/ScalarTypeBoolean.java | 16 +- .../server/type/ScalarTypeBytesBase.java | 6 +- .../server/type/ScalarTypeBytesEncrypted.java | 5 +- .../server/type/ScalarTypeClass.java | 6 +- .../server/type/ScalarTypeClob.java | 6 +- .../server/type/ScalarTypeCurrency.java | 2 +- .../server/type/ScalarTypeDouble.java | 4 +- .../server/type/ScalarTypeDuration.java | 4 +- .../type/ScalarTypeDurationWithNanos.java | 13 +- .../type/ScalarTypeEncryptedWrapper.java | 2 +- .../server/type/ScalarTypeEnum.java | 10 +- .../server/type/ScalarTypeEnumStandard.java | 6 +- .../type/ScalarTypeEnumWithMapping.java | 2 +- .../server/type/ScalarTypeFloat.java | 9 +- .../server/type/ScalarTypeInteger.java | 4 +- .../server/type/ScalarTypeJodaDateTime.java | 10 +- .../server/type/ScalarTypeJodaLocalDate.java | 2 +- .../server/type/ScalarTypeLocalDateTime.java | 1 - .../server/type/ScalarTypeLocalTime.java | 4 +- .../type/ScalarTypeLocalTimeWithNanos.java | 4 +- .../server/type/ScalarTypeLocale.java | 2 +- .../server/type/ScalarTypeLong.java | 4 +- .../type/ScalarTypeLongToTimestamp.java | 6 +- .../server/type/ScalarTypeMathBigInteger.java | 18 +- .../server/type/ScalarTypeMonth.java | 2 +- .../server/type/ScalarTypeOffsetDateTime.java | 4 +- .../server/type/ScalarTypePostgresHstore.java | 23 +- .../server/type/ScalarTypeShort.java | 4 +- .../server/type/ScalarTypeString.java | 2 +- .../server/type/ScalarTypeTimeZone.java | 2 +- .../server/type/ScalarTypeTimestamp.java | 8 +- .../server/type/ScalarTypeUtilDate.java | 2 +- .../server/type/ScalarTypeWrapper.java | 10 +- .../server/type/ScalarTypeYear.java | 2 +- .../server/type/ScalarTypeYearMonthDate.java | 2 +- .../server/type/ScalarTypeZoneId.java | 1 - .../server/type/ScalarTypeZonedDateTime.java | 5 +- .../server/type/SimpleAesEncryptor.java | 186 ++++---- .../server/type/TypeReflectHelper.java | 60 +-- .../server/type/ConvertInetAddressesTest.java | 31 ++ .../server/type/DefaultTypeFactoryTest.java | 16 + 63 files changed, 1314 insertions(+), 1328 deletions(-) create mode 100644 src/test/java/com/avaje/ebeaninternal/server/type/ConvertInetAddressesTest.java create mode 100644 src/test/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactoryTest.java diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundProperty.java b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundProperty.java index c572bc5f8..86dd0a8b7 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundProperty.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundProperty.java @@ -5,78 +5,76 @@ import com.avaje.ebean.config.CompoundTypeProperty; /** * Wraps a CompoundTypeProperty with it's type and parent for nested compound * types. - * - * @author rbygrave */ public class CtCompoundProperty { - private final String relativeName; + private final String relativeName; - private final CtCompoundProperty parent; + private final CtCompoundProperty parent; - private final CtCompoundType compoundType; + private final CtCompoundType compoundType; - @SuppressWarnings({ "rawtypes" }) - private final CompoundTypeProperty property; + @SuppressWarnings({"rawtypes"}) + private final CompoundTypeProperty property; - public CtCompoundProperty(String relativeName, CtCompoundProperty parent, CtCompoundType ctType, - CompoundTypeProperty property) { + public CtCompoundProperty(String relativeName, CtCompoundProperty parent, CtCompoundType ctType, + CompoundTypeProperty property) { - this.relativeName = relativeName; - this.parent = parent; - this.compoundType = ctType; - this.property = property; + this.relativeName = relativeName; + this.parent = parent; + this.compoundType = ctType; + this.property = property; + } + + /** + * The property name relative to the root of the compound type. + */ + public String getRelativeName() { + return relativeName; + } + + /** + * The property name local to its type. + */ + public String getPropertyName() { + return property.getName(); + } + + public String toString() { + return relativeName; + } + + @SuppressWarnings("unchecked") + public Object getValue(Object valueObject) { + if (valueObject == null) { + return null; } - - /** - * The property name relative to the root of the compound type. - */ - public String getRelativeName() { - return relativeName; + if (parent != null) { + valueObject = parent.getValue(valueObject); } + return property.getValue(valueObject); + } - /** - * The property name local to its type. - */ - public String getPropertyName() { - return property.getName(); - } + /** + * Set a scalar value that is used to build the immutable compound value + * object. + *

+ * When all the scalar values have been collected then the compound value + * object is built and this can be recursive for nested compound types. + *

+ */ + public Object setValue(Object bean, Object value) { - public String toString() { - return relativeName; - } + // compoundType and propertyName should be correct depth + Object compoundValue = ImmutableCompoundTypeBuilder.set(compoundType, property.getName(), value); - @SuppressWarnings("unchecked") - public Object getValue(Object valueObject) { - if (valueObject == null) { - return null; - } - if (parent != null) { - valueObject = parent.getValue(valueObject); - } - return property.getValue(valueObject); - } + if (compoundValue != null && parent != null) { + // Continue up the tree + return parent.setValue(bean, compoundValue); - /** - * Set a scalar value that is used to build the immutable compound value - * object. - *

- * When all the scalar values have been collected then the compound value - * object is built and this can be recursive for nested compound types. - *

- */ - public Object setValue(Object bean, Object value) { - - // compoundType and propertyName should be correct depth - Object compoundValue = ImmutableCompoundTypeBuilder.set(compoundType, property.getName(), value); - - if (compoundValue != null && parent != null) { - // Continue up the tree - return parent.setValue(bean, compoundValue); - - } else { - return compoundValue; - } + } else { + return compoundValue; } + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundType.java b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundType.java index 356d976c4..25c04ba21 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundType.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundType.java @@ -1,214 +1,193 @@ package com.avaje.ebeaninternal.server.type; -import java.io.IOException; -import java.sql.SQLException; -import java.util.LinkedHashMap; -import java.util.Map; - import com.avaje.ebean.config.CompoundType; import com.avaje.ebean.config.CompoundTypeProperty; import com.avaje.ebeaninternal.server.text.json.WriteJson; +import java.io.IOException; +import java.sql.SQLException; +import java.util.Map; + /** * The internal representation of a Compound Type (Immutable Compound Value Object). - * - * @param - * The Type of the "Immutable Compound Value Object". + * + * @param The Type of the "Immutable Compound Value Object". */ public final class CtCompoundType implements ScalarDataReader { - private final Class cvoClass; - - private final CompoundType cvoType; + private final Class cvoClass; - private final Map> propertyMap; + private final CompoundType cvoType; - private final ScalarDataReader[] propReaders; + private final ScalarDataReader[] propReaders; - private final CompoundTypeProperty[] properties; + private final CompoundTypeProperty[] properties; - public CtCompoundType(Class cvoClass, CompoundType cvoType, ScalarDataReader[] propReaders) { - - this.cvoClass = cvoClass; - this.cvoType = cvoType; - this.properties = cvoType.getProperties(); - this.propReaders = propReaders; - - this.propertyMap = new LinkedHashMap>(); - for (CompoundTypeProperty cp: properties) { - propertyMap.put(cp.getName(), cp); - } - } - - public String toString() { - return cvoClass.toString(); + public CtCompoundType(Class cvoClass, CompoundType cvoType, ScalarDataReader[] propReaders) { + + this.cvoClass = cvoClass; + this.cvoType = cvoType; + this.properties = cvoType.getProperties(); + this.propReaders = propReaders; + } + + public String toString() { + return cvoClass.toString(); + } + + public Class getCompoundTypeClass() { + return cvoClass; + } + + public V create(Object[] propertyValues) { + return cvoType.create(propertyValues); + } + + + public V create(Map valueMap) { + + if (valueMap.size() != properties.length) { + // not enough elements in the map + return null; } - public Class getCompoundTypeClass() { - return cvoClass; + // we expect the map to contain a value for + // each property and that the values are the + // correct type + Object[] propertyValues = new Object[properties.length]; + for (int i = 0; i < properties.length; i++) { + propertyValues[i] = valueMap.get(properties[i].getName()); + if (propertyValues[i] == null) { + String m = "Null value for " + properties[i].getName() + " in map " + valueMap; + throw new RuntimeException(m); + } } - public V create(Object[] propertyValues) { - return cvoType.create(propertyValues); + return create(propertyValues); + } + + public CompoundTypeProperty[] getProperties() { + + return cvoType.getProperties(); + } + + public V read(DataReader source) throws SQLException { + + boolean nullValue = false; + Object[] values = new Object[propReaders.length]; + + for (int i = 0; i < propReaders.length; i++) { + Object o = propReaders[i].read(source); + values[i] = o; + if (o == null) { + nullValue = true; + } } - - public V create(Map valueMap) { - - if (valueMap.size() != properties.length) { - // not enough elements in the map - return null; - } - - // we expect the map to contain a value for - // each property and that the values are the - // correct type - Object[] propertyValues = new Object[properties.length]; - for (int i = 0; i < properties.length; i++) { - propertyValues[i] = valueMap.get(properties[i].getName()); - if (propertyValues[i] == null) { - String m = "Null value for " + properties[i].getName() + " in map " + valueMap; - throw new RuntimeException(m); - } - } - - return create(propertyValues); + if (nullValue) { + return null; } - public CompoundTypeProperty[] getProperties() { + return create(values); + } - return cvoType.getProperties(); + public void loadIgnore(DataReader dataReader) { + for (int i = 0; i < propReaders.length; i++) { + propReaders[i].loadIgnore(dataReader); + } + } + + public void bind(DataBind b, V value) throws SQLException { + + CompoundTypeProperty[] props = cvoType.getProperties(); + + for (int i = 0; i < props.length; i++) { + Object o = props[i].getValue(value); + propReaders[i].bind(b, o); + } + } + + /** + * Recursively accumulate all the scalar types (in depth first order). + *

+ * This creates a flat list of scalars even when compound types are embedded + * inside compound types. + *

+ */ + public void accumulateScalarTypes(String parent, CtCompoundTypeScalarList list) { + + CompoundTypeProperty[] props = cvoType.getProperties(); + + for (int i = 0; i < propReaders.length; i++) { + String propName = getFullPropName(parent, props[i].getName()); + + list.addCompoundProperty(propName, this, props[i]); + + propReaders[i].accumulateScalarTypes(propName, list); } - public Object[] getPropertyValues(V valueObject) { + } - Object[] values = new Object[properties.length]; - for (int i = 0; i < properties.length; i++) { - values[i] = properties[i].getValue(valueObject); - } - return values; + /** + * Return the full property name (for compound types embedded in other + * compound types). + * + * @param parent the parent property name + * @param propName the local property name + */ + private String getFullPropName(String parent, String propName) { + if (parent == null) { + return propName; + } else { + return parent + "." + propName; + } + } + + public Object jsonConvert(Map map) { + return readJsonElementObject(map); + } + + @SuppressWarnings("unchecked") + private Object readJsonElementObject(Map jsonObject) { + + boolean nullValue = false; + Object[] values = new Object[propReaders.length]; + + for (int i = 0; i < propReaders.length; i++) { + String propName = properties[i].getName(); + Object jsonElement = jsonObject.get(propName); + + if (propReaders[i] instanceof CtCompoundType) { + values[i] = ((CtCompoundType) propReaders[i]).readJsonElementObject((Map) jsonElement); + } else { + values[i] = ((ScalarType) propReaders[i]).parse(jsonElement.toString()); + } + if (values[i] == null) { + nullValue = true; + } } - public V read(DataReader source) throws SQLException { - - boolean nullValue = false; - Object[] values = new Object[propReaders.length]; - - for (int i = 0; i < propReaders.length; i++) { - Object o = propReaders[i].read(source); - values[i] = o; - if (o == null){ - nullValue = true; - } - } - - if (nullValue){ - return null; - } - - return create(values); + if (nullValue) { + return null; } - public void loadIgnore(DataReader dataReader) { - for (int i = 0; i < propReaders.length; i++) { - propReaders[i].loadIgnore(dataReader); - } - } + return create(values); + } - public void bind(DataBind b, V value) throws SQLException { - CompoundTypeProperty[] props = cvoType.getProperties(); - - for (int i = 0; i < props.length; i++) { - Object o = props[i].getValue(value); - propReaders[i].bind(b, o); - } - } - - /** - * Recursively accumulate all the scalar types (in depth first order). - *

- * This creates a flat list of scalars even when compound types are embedded - * inside compound types. - *

- */ - public void accumulateScalarTypes(String parent, CtCompoundTypeScalarList list) { - - CompoundTypeProperty[] props = cvoType.getProperties(); - - for (int i = 0; i < propReaders.length; i++) { - String propName = getFullPropName(parent, props[i].getName()); - - list.addCompoundProperty(propName, this, props[i]); - - propReaders[i].accumulateScalarTypes(propName, list); - } - - } - - /** - * Return the full property name (for compound types embedded in other - * compound types). - * - * @param parent - * the parent property name - * @param propName - * the local property name - */ - private String getFullPropName(String parent, String propName) { - if (parent == null) { - return propName; - } else { - return parent + "." + propName; - } - } - - public Object jsonConvert(Map map) { - return readJsonElementObject(map); - } - - @SuppressWarnings("unchecked") - private Object readJsonElementObject(Map jsonObject){ - - boolean nullValue = false; - Object[] values = new Object[propReaders.length]; - - for (int i = 0; i < propReaders.length; i++) { - String propName = properties[i].getName(); - Object jsonElement = jsonObject.get(propName); - - if (propReaders[i] instanceof CtCompoundType) { - values[i] = ((CtCompoundType)propReaders[i]).readJsonElementObject((Map)jsonElement); - } else { - //((ScalarType)propReaders[i]).jsonFromString(jsonElement.toPrimitiveString(), ctx.getValueAdapter()); - values[i] = ((ScalarType)propReaders[i]).parse(jsonElement.toString());; - } - if (values[i] == null){ - nullValue = true; - } - } - - if (nullValue){ - return null; - } - - return create(values); - } - - public void jsonWrite(WriteJson ctx, Object valueObject, String propertyName) throws IOException { ctx.beginAssocOne(propertyName, valueObject); jsonWriteProps(ctx, valueObject, propertyName); ctx.endAssocOne(); } - - @SuppressWarnings({ "unchecked", "rawtypes" }) + + @SuppressWarnings({"unchecked", "rawtypes"}) private void jsonWriteProps(WriteJson ctx, Object valueObject, String propertyName) throws IOException { if (propertyName != null) { ctx.gen().writeFieldName(propertyName); - } + } ctx.gen().writeStartObject(); for (int i = 0; i < properties.length; i++) { @@ -219,7 +198,6 @@ public final class CtCompoundType implements ScalarDataReader { } else { ((ScalarType) propReaders[i]).jsonWrite(ctx.gen(), propName, value); - //ctx.appendNameValue(propName, (ScalarType) propReaders[i], value); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DataBind.java b/src/main/java/com/avaje/ebeaninternal/server/type/DataBind.java index 4f55a95f4..aabfe38a0 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DataBind.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DataBind.java @@ -12,109 +12,105 @@ import java.sql.Timestamp; public class DataBind { - private final PreparedStatement pstmt; + private final PreparedStatement pstmt; - private int pos; + private int pos; - public DataBind(PreparedStatement pstmt) { - this.pstmt = pstmt; - } + public DataBind(PreparedStatement pstmt) { + this.pstmt = pstmt; + } - public void close() throws SQLException { - pstmt.close(); - } - - public int currentPos() { - return pos; - } - - public void resetPos() { - pos = 0; - } + public void close() throws SQLException { + pstmt.close(); + } - public void setObject(Object value) throws SQLException { - pstmt.setObject(++pos, value); - } + public int currentPos() { + return pos; + } - public void setObject(Object value, int sqlType) throws SQLException { - pstmt.setObject(++pos, value, sqlType); - } + public void setObject(Object value) throws SQLException { + pstmt.setObject(++pos, value); + } - public void setNull(int jdbcType) throws SQLException { - pstmt.setNull(++pos, jdbcType); - } + public void setObject(Object value, int sqlType) throws SQLException { + pstmt.setObject(++pos, value, sqlType); + } - public int nextPos() { - return ++pos; - } + public void setNull(int jdbcType) throws SQLException { + pstmt.setNull(++pos, jdbcType); + } - public int decrementPos() { - return ++pos; - } - - public int executeUpdate() throws SQLException { - return pstmt.executeUpdate(); - } + public int nextPos() { + return ++pos; + } - public PreparedStatement getPstmt() { - return pstmt; - } + public int decrementPos() { + return ++pos; + } - public void setString(String s) throws SQLException { - pstmt.setString(++pos, s); - } + public int executeUpdate() throws SQLException { + return pstmt.executeUpdate(); + } - public void setInt(int i) throws SQLException { - pstmt.setInt(++pos, i); - } + public PreparedStatement getPstmt() { + return pstmt; + } - public void setLong(long i) throws SQLException { - pstmt.setLong(++pos, i); - } + public void setString(String s) throws SQLException { + pstmt.setString(++pos, s); + } - public void setShort(short i) throws SQLException { - pstmt.setShort(++pos, i); - } + public void setInt(int i) throws SQLException { + pstmt.setInt(++pos, i); + } - public void setFloat(float i) throws SQLException { - pstmt.setFloat(++pos, i); - } + public void setLong(long i) throws SQLException { + pstmt.setLong(++pos, i); + } - public void setDouble(double i) throws SQLException { - pstmt.setDouble(++pos, i); - } + public void setShort(short i) throws SQLException { + pstmt.setShort(++pos, i); + } - public void setBigDecimal(BigDecimal v) throws SQLException { - pstmt.setBigDecimal(++pos, v); - } + public void setFloat(float i) throws SQLException { + pstmt.setFloat(++pos, i); + } - public void setDate(java.sql.Date v) throws SQLException { - pstmt.setDate(++pos, v); - } + public void setDouble(double i) throws SQLException { + pstmt.setDouble(++pos, i); + } - public void setTimestamp(Timestamp v) throws SQLException { - pstmt.setTimestamp(++pos, v); - } + public void setBigDecimal(BigDecimal v) throws SQLException { + pstmt.setBigDecimal(++pos, v); + } - public void setTime(Time v) throws SQLException { - pstmt.setTime(++pos, v); - } + public void setDate(java.sql.Date v) throws SQLException { + pstmt.setDate(++pos, v); + } - public void setBoolean(boolean v) throws SQLException { - pstmt.setBoolean(++pos, v); - } - - public void setBytes(byte[] v) throws SQLException { - pstmt.setBytes(++pos, v); - } - - public void setByte(byte v) throws SQLException { - pstmt.setByte(++pos, v); - } - - public void setChar(char v) throws SQLException { - pstmt.setString(++pos, String.valueOf(v)); - } + public void setTimestamp(Timestamp v) throws SQLException { + pstmt.setTimestamp(++pos, v); + } + + public void setTime(Time v) throws SQLException { + pstmt.setTime(++pos, v); + } + + public void setBoolean(boolean v) throws SQLException { + pstmt.setBoolean(++pos, v); + } + + public void setBytes(byte[] v) throws SQLException { + pstmt.setBytes(++pos, v); + } + + public void setByte(byte v) throws SQLException { + pstmt.setByte(++pos, v); + } + + public void setChar(char v) throws SQLException { + pstmt.setString(++pos, String.valueOf(v)); + } public void setBlob(InputStream inputStream, long length) throws SQLException { pstmt.setBlob(++pos, inputStream, length); @@ -124,14 +120,14 @@ public class DataBind { pstmt.setBlob(++pos, inputStream); } - public void setBlob(byte[] bytes) throws SQLException { - ByteArrayInputStream is = new ByteArrayInputStream(bytes); - pstmt.setBinaryStream(++pos, is, bytes.length); - } - - public void setClob(String content) throws SQLException { - Reader reader = new StringReader(content); - pstmt.setCharacterStream(++pos, reader, content.length()); - } + public void setBlob(byte[] bytes) throws SQLException { + ByteArrayInputStream is = new ByteArrayInputStream(bytes); + pstmt.setBinaryStream(++pos, is, bytes.length); + } + + public void setClob(String content) throws SQLException { + Reader reader = new StringReader(content); + pstmt.setCharacterStream(++pos, reader, content.length()); + } } \ No newline at end of file diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DataEncryptSupport.java b/src/main/java/com/avaje/ebeaninternal/server/type/DataEncryptSupport.java index b049f7f09..37504d291 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DataEncryptSupport.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DataEncryptSupport.java @@ -6,38 +6,38 @@ import com.avaje.ebean.config.Encryptor; public class DataEncryptSupport { - private final EncryptKeyManager encryptKeyManager; - private final Encryptor encryptor; - private final String table; - private final String column; - - public DataEncryptSupport(EncryptKeyManager encryptKeyManager, Encryptor encryptor, String table, String column) { - this.encryptKeyManager = encryptKeyManager; - this.encryptor = encryptor; - this.table = table; - this.column = column; - } - - public byte[] encrypt(byte[] data){ - - EncryptKey key = encryptKeyManager.getEncryptKey(table, column); - return encryptor.encrypt(data, key); - } + private final EncryptKeyManager encryptKeyManager; + private final Encryptor encryptor; + private final String table; + private final String column; - public byte[] decrypt(byte[] data){ - - EncryptKey key = encryptKeyManager.getEncryptKey(table, column); - return encryptor.decrypt(data, key); - } - - public String decryptObject(byte[] data) { - EncryptKey key = encryptKeyManager.getEncryptKey(table, column); - return encryptor.decryptString(data, key); - } + public DataEncryptSupport(EncryptKeyManager encryptKeyManager, Encryptor encryptor, String table, String column) { + this.encryptKeyManager = encryptKeyManager; + this.encryptor = encryptor; + this.table = table; + this.column = column; + } - public byte[] encryptObject(String formattedValue) { - EncryptKey key = encryptKeyManager.getEncryptKey(table, column); - return encryptor.encryptString(formattedValue, key); - } + public byte[] encrypt(byte[] data) { + + EncryptKey key = encryptKeyManager.getEncryptKey(table, column); + return encryptor.encrypt(data, key); + } + + public byte[] decrypt(byte[] data) { + + EncryptKey key = encryptKeyManager.getEncryptKey(table, column); + return encryptor.decrypt(data, key); + } + + public String decryptObject(byte[] data) { + EncryptKey key = encryptKeyManager.getEncryptKey(table, column); + return encryptor.decryptString(data, key); + } + + public byte[] encryptObject(String formattedValue) { + EncryptKey key = encryptKeyManager.getEncryptKey(table, column); + return encryptor.encryptString(formattedValue, key); + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DataReader.java b/src/main/java/com/avaje/ebeaninternal/server/type/DataReader.java index 7e8dfc33f..71911a53d 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DataReader.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DataReader.java @@ -7,51 +7,51 @@ import java.sql.SQLException; public interface DataReader { - public void close() throws SQLException; + void close() throws SQLException; - public boolean next() throws SQLException; + boolean next() throws SQLException; - public void resetColumnPosition(); - - public void incrementPos(int increment); + void resetColumnPosition(); - public byte[] getBinaryBytes() throws SQLException; + void incrementPos(int increment); - public byte[] getBlobBytes() throws SQLException; + byte[] getBinaryBytes() throws SQLException; - public String getStringFromStream() throws SQLException; + byte[] getBlobBytes() throws SQLException; - public String getStringClob() throws SQLException; - - public String getString() throws SQLException; + String getStringFromStream() throws SQLException; - public Boolean getBoolean() throws SQLException; + String getStringClob() throws SQLException; - public Byte getByte() throws SQLException; + String getString() throws SQLException; - public Short getShort() throws SQLException; + Boolean getBoolean() throws SQLException; - public Integer getInt() throws SQLException; + Byte getByte() throws SQLException; - public Long getLong() throws SQLException; + Short getShort() throws SQLException; - public Float getFloat() throws SQLException; + Integer getInt() throws SQLException; - public Double getDouble() throws SQLException; + Long getLong() throws SQLException; - public byte[] getBytes() throws SQLException; + Float getFloat() throws SQLException; - public java.sql.Date getDate() throws SQLException; + Double getDouble() throws SQLException; - public java.sql.Time getTime() throws SQLException; + byte[] getBytes() throws SQLException; - public java.sql.Timestamp getTimestamp() throws SQLException; + java.sql.Date getDate() throws SQLException; - public BigDecimal getBigDecimal() throws SQLException; + java.sql.Time getTime() throws SQLException; - public Array getArray() throws SQLException; - - public Object getObject() throws SQLException; + java.sql.Timestamp getTimestamp() throws SQLException; - public InputStream getBinaryStream() throws SQLException; + BigDecimal getBigDecimal() throws SQLException; + + Array getArray() throws SQLException; + + Object getObject() throws SQLException; + + InputStream getBinaryStream() throws SQLException; } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactory.java b/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactory.java index 0baeef804..369d5fe8f 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactory.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactory.java @@ -14,130 +14,128 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter; */ public class DefaultTypeFactory { - private final ServerConfig serverConfig; + private final ServerConfig serverConfig; - public DefaultTypeFactory(ServerConfig serverConfig) { - this.serverConfig = serverConfig; + public DefaultTypeFactory(ServerConfig serverConfig) { + this.serverConfig = serverConfig; + } + + protected ScalarType createBoolean(String trueValue, String falseValue) { + + try { + // first try Integer based boolean + Integer intTrue = BasicTypeConverter.toInteger(trueValue); + Integer intFalse = BasicTypeConverter.toInteger(falseValue); + + return new ScalarTypeBoolean.IntBoolean(intTrue, intFalse); + + } catch (NumberFormatException e) { + // treat as Varchar/String based boolean + return new ScalarTypeBoolean.StringBoolean(trueValue, falseValue); + } + } + + /** + * Create the ScalarType for mapping Booleans. For some databases this is a + * native data type and for others Booleans will be converted to Y/N or 0/1 + * etc. + */ + public ScalarType createBoolean() { + + if (serverConfig == null) { + return new ScalarTypeBoolean.Native(); + } + String trueValue = serverConfig.getDatabaseBooleanTrue(); + String falseValue = serverConfig.getDatabaseBooleanFalse(); + + if (falseValue != null && trueValue != null) { + // explicit integer or string based booleans + return createBoolean(trueValue, falseValue); } - private ScalarType createBoolean(String trueValue, String falseValue) { - - try { - // first try Integer based boolean - Integer intTrue = BasicTypeConverter.toInteger(trueValue); - Integer intFalse = BasicTypeConverter.toInteger(falseValue); - - return new ScalarTypeBoolean.IntBoolean(intTrue, intFalse); - - } catch (NumberFormatException e) { - } - - // treat as Varchar/String based boolean - return new ScalarTypeBoolean.StringBoolean(trueValue, falseValue); + // determine based on database platform configuration + int booleanDbType = serverConfig.getDatabasePlatform().getBooleanDbType(); + // Some dbs use BIT e.g. MySQL + if (booleanDbType == Types.BIT) { + return new ScalarTypeBoolean.BitBoolean(); } - /** - * Create the ScalarType for mapping Booleans. For some databases this is a - * native data type and for others Booleans will be converted to Y/N or 0/1 - * etc. - */ - public ScalarType createBoolean() { - - if (serverConfig == null) { - return new ScalarTypeBoolean.Native(); - } - String trueValue = serverConfig.getDatabaseBooleanTrue(); - String falseValue = serverConfig.getDatabaseBooleanFalse(); - - if (falseValue != null && trueValue != null) { - // explicit integer or string based booleans - return createBoolean(trueValue, falseValue); - } - - // determine based on database platform configuration - int booleanDbType = serverConfig.getDatabasePlatform().getBooleanDbType(); - - // Some dbs use BIT e.g. MySQL - if (booleanDbType == Types.BIT) { - return new ScalarTypeBoolean.BitBoolean(); - } - - if (booleanDbType == Types.INTEGER) { - return new ScalarTypeBoolean.IntBoolean(1, 0); - } - if (booleanDbType == Types.VARCHAR) { - return new ScalarTypeBoolean.StringBoolean("T", "F"); - } - - if (booleanDbType == Types.BOOLEAN) { - return new ScalarTypeBoolean.Native(); - } - - // assume the JDBC driver can convert the type - return new ScalarTypeBoolean.Native(); + if (booleanDbType == Types.INTEGER) { + return new ScalarTypeBoolean.IntBoolean(1, 0); + } + if (booleanDbType == Types.VARCHAR) { + return new ScalarTypeBoolean.StringBoolean("T", "F"); } - /** - * Create the default ScalarType for java.util.Date. - */ - public ScalarType createUtilDate(JsonConfig.DateTime mode) { - // by default map anonymous java.util.Date to java.sql.Timestamp. - // String mapType = - // properties.getProperty("type.mapping.java.util.Date","timestamp"); - int utilDateType = getTemporalMapType("timestamp"); - - return createUtilDate(mode, utilDateType); + if (booleanDbType == Types.BOOLEAN) { + return new ScalarTypeBoolean.Native(); } - /** - * Create a ScalarType for java.util.Date explicitly specifying the type to - * map to. - */ - public ScalarType createUtilDate(JsonConfig.DateTime mode, int utilDateType) { + // assume the JDBC driver can convert the type + return new ScalarTypeBoolean.Native(); + } - switch (utilDateType) { - case Types.DATE: - return new ScalarTypeUtilDate.DateType(); + /** + * Create the default ScalarType for java.util.Date. + */ + public ScalarType createUtilDate(JsonConfig.DateTime mode) { + // by default map anonymous java.util.Date to java.sql.Timestamp. + // String mapType = + // properties.getProperty("type.mapping.java.util.Date","timestamp"); + int utilDateType = getTemporalMapType("timestamp"); - case Types.TIMESTAMP: - return new ScalarTypeUtilDate.TimestampType(mode); + return createUtilDate(mode, utilDateType); + } - default: - throw new RuntimeException("Invalid type " + utilDateType); - } + /** + * Create a ScalarType for java.util.Date explicitly specifying the type to + * map to. + */ + public ScalarType createUtilDate(JsonConfig.DateTime mode, int utilDateType) { + + switch (utilDateType) { + case Types.DATE: + return new ScalarTypeUtilDate.DateType(); + + case Types.TIMESTAMP: + return new ScalarTypeUtilDate.TimestampType(mode); + + default: + throw new RuntimeException("Invalid type " + utilDateType); } + } - /** - * Create the default ScalarType for java.util.Calendar. - */ - public ScalarType createCalendar(JsonConfig.DateTime mode) { + /** + * Create the default ScalarType for java.util.Calendar. + */ + public ScalarType createCalendar(JsonConfig.DateTime mode) { - int jdbcType = getTemporalMapType("timestamp"); - return createCalendar(mode, jdbcType); + int jdbcType = getTemporalMapType("timestamp"); + return createCalendar(mode, jdbcType); + } + + /** + * Create a ScalarType for java.util.Calendar explicitly specifying the type + * to map to. + */ + public ScalarType createCalendar(JsonConfig.DateTime mode, int jdbcType) { + + return new ScalarTypeCalendar(mode, jdbcType); + } + + private int getTemporalMapType(String mapType) { + if (mapType.equalsIgnoreCase("date")) { + return java.sql.Types.DATE; } + return java.sql.Types.TIMESTAMP; + } - /** - * Create a ScalarType for java.util.Calendar explicitly specifying the type - * to map to. - */ - public ScalarType createCalendar(JsonConfig.DateTime mode, int jdbcType) { + /** + * Create a ScalarType for java.math.BigInteger. + */ + public ScalarType createMathBigInteger() { - return new ScalarTypeCalendar(mode, jdbcType); - } - - private int getTemporalMapType(String mapType) { - if (mapType.equalsIgnoreCase("date")) { - return java.sql.Types.DATE; - } - return java.sql.Types.TIMESTAMP; - } - - /** - * Create a ScalarType for java.math.BigInteger. - */ - public ScalarType createMathBigInteger() { - - return new ScalarTypeMathBigInteger(); - } + return new ScalarTypeMathBigInteger(); + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeManager.java b/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeManager.java index edaca8754..6a94fcefe 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeManager.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeManager.java @@ -1,6 +1,7 @@ package com.avaje.ebeaninternal.server.type; import java.io.File; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.math.BigDecimal; import java.math.BigInteger; @@ -25,6 +26,7 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import com.avaje.ebean.config.*; +import com.fasterxml.jackson.databind.ObjectMapper; import org.joda.time.DateMidnight; import org.joda.time.DateTime; import org.joda.time.LocalDate; @@ -125,6 +127,8 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { private final JsonConfig.DateTime jsonDateTime; + private final boolean objectMapperPresent; + /** * Create the DefaultTypeManager. */ @@ -144,6 +148,8 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { this.customTypeMap.put(ScalarTypePostgresHstore.KEY, new ScalarTypePostgresHstore()); + this.objectMapperPresent = ClassUtil.isPresent("com.fasterxml.jackson.databind.ObjectMapper", this.getClass()); + this.extraTypeFactory = new DefaultTypeFactory(config); initialiseStandard(jsonDateTime, clobType, blobType, config.isUuidStoreAsBinary()); @@ -151,7 +157,7 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { initialiseJodaTypes(jsonDateTime); if (bootupClasses != null) { - initialiseCustomScalarTypes(jsonDateTime, bootupClasses); + initialiseCustomScalarTypes(jsonDateTime, bootupClasses, config); initialiseScalarConverters(bootupClasses); initialiseCompoundTypes(bootupClasses); } @@ -468,7 +474,7 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { * interface and register it with this TypeManager. *

*/ - protected void initialiseCustomScalarTypes(JsonConfig.DateTime mode, BootupClasses bootupClasses) { + protected void initialiseCustomScalarTypes(JsonConfig.DateTime mode, BootupClasses bootupClasses, ServerConfig serverConfig) { ScalarTypeLongToTimestamp longToTimestamp = new ScalarTypeLongToTimestamp(mode); @@ -480,9 +486,22 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { Class cls = foundTypes.get(i); try { - ScalarType scalarType = (ScalarType) cls.newInstance(); - add(scalarType); + ScalarType scalarType; + if (!objectMapperPresent) { + scalarType = (ScalarType) cls.newInstance(); + } else { + try { + // first try objectMapper constructor + Constructor constructor = cls.getConstructor(ObjectMapper.class); + ObjectMapper objectMapper = getObjectMapper(serverConfig); + scalarType = (ScalarType)constructor.newInstance(objectMapper); + } catch (NoSuchMethodException e) { + scalarType = (ScalarType) cls.newInstance(); + } + } + + add(scalarType); customScalarTypes.add(scalarType); } catch (Exception e) { @@ -492,6 +511,16 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable { } } + private ObjectMapper getObjectMapper(ServerConfig serverConfig) { + + ObjectMapper objectMapper = (ObjectMapper)serverConfig.getObjectMapper(); + if (objectMapper == null) { + objectMapper = new ObjectMapper(); + serverConfig.setObjectMapper(objectMapper); + } + return objectMapper; + } + @SuppressWarnings({ "unchecked", "rawtypes" }) protected void initialiseScalarConverters(BootupClasses bootupClasses) { diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbIntegerMap.java b/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbIntegerMap.java index 432bfc0cd..635516673 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbIntegerMap.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbIntegerMap.java @@ -10,52 +10,52 @@ import javax.persistence.PersistenceException; */ public class EnumToDbIntegerMap extends EnumToDbValueMap { - @Override - public int getDbType() { - return Types.INTEGER; - } + @Override + public int getDbType() { + return Types.INTEGER; + } public EnumToDbIntegerMap add(Object beanValue, Integer dbValue) { addInternal(beanValue, dbValue); return this; } - @Override - public EnumToDbIntegerMap add(Object beanValue, String stringDbValue) { + @Override + public EnumToDbIntegerMap add(Object beanValue, String stringDbValue) { - try { - Integer value = Integer.valueOf(stringDbValue); - addInternal(beanValue, value); - - return this; + try { + Integer value = Integer.valueOf(stringDbValue); + addInternal(beanValue, value); - } catch (Exception e) { - String msg = "Error converted enum type[" + beanValue.getClass().getName(); - msg += "] enum value[" + beanValue + "] string value [" + stringDbValue + "]"; - msg += " to an Integer."; - throw new PersistenceException(msg, e); - } - } + return this; - @Override - public void bind(DataBind b, Object value) throws SQLException { - if (value == null) { - b.setNull(Types.INTEGER); - } else { - Integer s = getDbValue(value); - b.setInt(s); - } + } catch (Exception e) { + String msg = "Error converted enum type[" + beanValue.getClass().getName(); + msg += "] enum value[" + beanValue + "] string value [" + stringDbValue + "]"; + msg += " to an Integer."; + throw new PersistenceException(msg, e); + } + } - } + @Override + public void bind(DataBind b, Object value) throws SQLException { + if (value == null) { + b.setNull(Types.INTEGER); + } else { + Integer s = getDbValue(value); + b.setInt(s); + } - @Override - public Object read(DataReader dataReader) throws SQLException { - Integer i = dataReader.getInt(); - if (i == null) { - return null; - } else { - return getBeanValue(i); - } - } + } + + @Override + public Object read(DataReader dataReader) throws SQLException { + Integer i = dataReader.getInt(); + if (i == null) { + return null; + } else { + return getBeanValue(i); + } + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbStringMap.java b/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbStringMap.java index b8f57bbb9..d5f77818b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbStringMap.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbStringMap.java @@ -8,38 +8,37 @@ import java.sql.Types; */ public class EnumToDbStringMap extends EnumToDbValueMap { - - - @Override - public int getDbType() { - return Types.VARCHAR; - } - @Override - public EnumToDbStringMap add(Object beanValue, String dbValue) { - addInternal(beanValue, dbValue); - return this; - } + @Override + public int getDbType() { + return Types.VARCHAR; + } - @Override - public void bind(DataBind b, Object value) throws SQLException { - if (value == null){ - b.setNull(Types.VARCHAR); - } else { - String s = getDbValue(value); - b.setString(s); - } - - } + @Override + public EnumToDbStringMap add(Object beanValue, String dbValue) { + addInternal(beanValue, dbValue); + return this; + } + + @Override + public void bind(DataBind b, Object value) throws SQLException { + if (value == null) { + b.setNull(Types.VARCHAR); + } else { + String s = getDbValue(value); + b.setString(s); + } + + } + + @Override + public Object read(DataReader dataReader) throws SQLException { + String s = dataReader.getString(); + if (s == null) { + return null; + } else { + return getBeanValue(s); + } + } - @Override - public Object read(DataReader dataReader) throws SQLException { - String s = dataReader.getString(); - if (s == null){ - return null; - } else { - return getBeanValue(s); - } - } - } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbValueMap.java b/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbValueMap.java index edbf93592..dfe037a30 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbValueMap.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbValueMap.java @@ -13,122 +13,115 @@ import java.util.LinkedHashMap; */ public abstract class EnumToDbValueMap { - public static EnumToDbValueMap create(boolean integerType) { - return integerType ? new EnumToDbIntegerMap() : new EnumToDbStringMap(); - } + public static EnumToDbValueMap create(boolean integerType) { + return integerType ? new EnumToDbIntegerMap() : new EnumToDbStringMap(); + } - final LinkedHashMap keyMap; + final LinkedHashMap keyMap; - final LinkedHashMap valueMap; + final LinkedHashMap valueMap; - final boolean allowNulls; + final boolean allowNulls; - final boolean isIntegerType; + final boolean isIntegerType; - /** - * Construct with allowNulls defaulting to false. - */ - public EnumToDbValueMap() { - this(false, false); - } + /** + * Construct with allowNulls defaulting to false. + */ + public EnumToDbValueMap() { + this(false, false); + } - /** - * Construct with allowNulls setting. - *

- * If allowNulls is false then an IllegalArgumentException is thrown by - * either the getDBValue or getBeanValue methods if not matching Bean or DB - * value is found. - *

- */ - public EnumToDbValueMap(boolean allowNulls, boolean isIntegerType) { - this.allowNulls = allowNulls; - this.isIntegerType = isIntegerType; - keyMap = new LinkedHashMap(); - valueMap = new LinkedHashMap(); - } + /** + * Construct with allowNulls setting. + *

+ * If allowNulls is false then an IllegalArgumentException is thrown by + * either the getDBValue or getBeanValue methods if not matching Bean or DB + * value is found. + *

+ */ + public EnumToDbValueMap(boolean allowNulls, boolean isIntegerType) { + this.allowNulls = allowNulls; + this.isIntegerType = isIntegerType; + keyMap = new LinkedHashMap(); + valueMap = new LinkedHashMap(); + } - /** - * Return true if this is mapping to integers, false - * if mapping to Strings. - */ - public boolean isIntegerType() { - return isIntegerType; - } + /** + * Return true if this is mapping to integers, false + * if mapping to Strings. + */ + public boolean isIntegerType() { + return isIntegerType; + } - /** - * Return the DB values. - */ - public Iterator dbValues() { - return valueMap.keySet().iterator(); - } + /** + * Return the DB values. + */ + public Iterator dbValues() { + return valueMap.keySet().iterator(); + } - /** - * Return the bean 'key' value. - */ - public Iterator beanValues() { - return valueMap.values().iterator(); - } + /** + * Bind using the correct database type. + */ + public abstract void bind(DataBind b, Object value) throws SQLException; - /** - * Bind using the correct database type. - */ - public abstract void bind(DataBind b, Object value) throws SQLException; + /** + * Read using the correct database type. + */ + public abstract Object read(DataReader dataReader) throws SQLException; - /** - * Read using the correct database type. - */ - public abstract Object read(DataReader dataReader) throws SQLException; + /** + * Return the database type. + */ + public abstract int getDbType(); - /** - * Return the database type. - */ - public abstract int getDbType(); + /** + * Add name value pair where the dbValue is the raw string and may need to + * be converted (to an Integer for example). + */ + public abstract EnumToDbValueMap add(Object beanValue, String dbValue); - /** - * Add name value pair where the dbValue is the raw string and may need to - * be converted (to an Integer for example). - */ - public abstract EnumToDbValueMap add(Object beanValue, String dbValue); + /** + * Add a bean value and DB value pair. + *

+ * The dbValue will be converted to an Integer if isIntegerType is true; + *

+ */ + protected void addInternal(Object beanValue, T dbValue) { - /** - * Add a bean value and DB value pair. - *

- * The dbValue will be converted to an Integer if isIntegerType is true; - *

- */ - protected void addInternal(Object beanValue, T dbValue) { + keyMap.put(beanValue, dbValue); + valueMap.put(dbValue, beanValue); + } - keyMap.put(beanValue, dbValue); - valueMap.put(dbValue, beanValue); - } + /** + * Return the DB value given the bean value. + */ + public T getDbValue(Object beanValue) { + if (beanValue == null) { + return null; + } + T dbValue = keyMap.get(beanValue); + if (dbValue == null && !allowNulls) { + String msg = "DB value for " + beanValue + " not found in " + keyMap; + throw new IllegalArgumentException(msg); + } + return dbValue; + } - /** - * Return the DB value given the bean value. - */ - public T getDbValue(Object beanValue) { - if (beanValue == null) { - return null; - } - T dbValue = keyMap.get(beanValue); - if (dbValue == null && !allowNulls) { - String msg = "DB value for " + beanValue + " not found in " + keyMap; - throw new IllegalArgumentException(msg); - } - return dbValue; - } - - /** - * Return the Bean value given the DB value. - */ - public Object getBeanValue(T dbValue) { - if (dbValue == null) { - return null; - } - Object beanValue = valueMap.get(dbValue); - if (beanValue == null && !allowNulls) { - String msg = "Bean value for " + dbValue + " not found in " + valueMap; - throw new IllegalArgumentException(msg); - } - return beanValue; - } + /** + * Return the Bean value given the DB value. + */ + public Object getBeanValue(T dbValue) { + if (dbValue == null) { + return null; + } + Object beanValue = valueMap.get(dbValue); + if (beanValue == null && !allowNulls) { + String msg = "Bean value for " + dbValue + " not found in " + valueMap; + throw new IllegalArgumentException(msg); + } + return beanValue; + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/LongToTimestampConverter.java b/src/main/java/com/avaje/ebeaninternal/server/type/LongToTimestampConverter.java index 10309d393..b34226875 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/LongToTimestampConverter.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/LongToTimestampConverter.java @@ -4,21 +4,21 @@ import java.sql.Timestamp; import com.avaje.ebean.config.ScalarTypeConverter; -public class LongToTimestampConverter implements ScalarTypeConverter{ - - public Long getNullValue() { - return null; - } +public class LongToTimestampConverter implements ScalarTypeConverter { - public Timestamp unwrapValue(Long beanType) { - - return new Timestamp(beanType.longValue()); - } + public Long getNullValue() { + return null; + } + + public Timestamp unwrapValue(Long beanType) { + + return new Timestamp(beanType.longValue()); + } + + public Long wrapValue(Timestamp scalarType) { + + return scalarType.getTime(); + } - public Long wrapValue(Timestamp scalarType) { - - return scalarType.getTime(); - } - } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareCollection.java b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareCollection.java index 174a47d24..02d66dfd7 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareCollection.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareCollection.java @@ -22,7 +22,7 @@ public class ModifyAwareCollection implements Collection { this.owner = owner; this.c = c; } - + public String toString() { return c.toString(); } @@ -37,9 +37,7 @@ public class ModifyAwareCollection implements Collection { public boolean addAll(Collection collection) { boolean changed = false; - Iterator it = collection.iterator(); - while (it.hasNext()) { - E o = it.next(); + for (E o : collection) { if (c.add(o)) { owner.markAsModified(); changed = true; @@ -68,7 +66,7 @@ public class ModifyAwareCollection implements Collection { } public Iterator iterator() { - return new ModifyAwareIterator(owner, c.iterator()); + return new ModifyAwareIterator(owner, c.iterator()); } public boolean remove(Object o) { @@ -81,10 +79,8 @@ public class ModifyAwareCollection implements Collection { public boolean removeAll(Collection collection) { boolean changed = false; - Iterator it = collection.iterator(); - while (it.hasNext()) { - Object o = (Object) it.next(); - if (c.remove(o)) { + for (Object element : collection) { + if (c.remove(element)) { owner.markAsModified(); changed = true; } @@ -96,7 +92,7 @@ public class ModifyAwareCollection implements Collection { boolean changed = false; Iterator it = c.iterator(); while (it.hasNext()) { - Object o = (Object) it.next(); + Object o = it.next(); if (!collection.contains(o)) { it.remove(); owner.markAsModified(); diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareMap.java b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareMap.java index efc46820c..234c231bd 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareMap.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareMap.java @@ -7,26 +7,26 @@ import java.util.Set; /** * Map that is wraps an underlying map for the purpose of detecting changes. */ -public class ModifyAwareMap implements Map, ModifyAwareOwner { +public class ModifyAwareMap implements Map, ModifyAwareOwner { /** * Dirty flag set when the map has been modified. */ private boolean dirty; - + /** * The underlying map. */ - private Map map; - - public ModifyAwareMap(Map underyling) { + private Map map; + + public ModifyAwareMap(Map underyling) { this.map = underyling; } public String toString() { return map.toString(); } - + @Override public boolean isMarkedDirty() { return dirty; @@ -36,7 +36,7 @@ public class ModifyAwareMap implements Map, ModifyAwareOwner { public void markAsModified() { dirty = true; } - + @Override public int size() { return map.size(); @@ -72,7 +72,7 @@ public class ModifyAwareMap implements Map, ModifyAwareOwner { public V remove(Object key) { V value = map.remove(key); if (value != null) { - markAsModified(); + markAsModified(); } return value; } @@ -107,5 +107,4 @@ public class ModifyAwareMap implements Map, ModifyAwareOwner { return new ModifyAwareSet>(this, map.entrySet()); } - } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareOwner.java b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareOwner.java index 93ca8cc31..542278c9f 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareOwner.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareOwner.java @@ -8,10 +8,10 @@ public interface ModifyAwareOwner { /** * Return true if the value is considered dirty. */ - public boolean isMarkedDirty(); - + boolean isMarkedDirty(); + /** * Marks the object as modified. */ - public void markAsModified(); + void markAsModified(); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareType.java b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareType.java index ca44984ca..a30ec5c1e 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareType.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareType.java @@ -2,5 +2,5 @@ package com.avaje.ebeaninternal.server.type; public interface ModifyAwareType { - public boolean isDirty(); + boolean isDirty(); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReader.java b/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReader.java index 9ab0f4587..851a45935 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReader.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReader.java @@ -19,225 +19,224 @@ import com.avaje.ebeaninternal.server.core.Message; public class RsetDataReader implements DataReader { - private static final int bufferSize = 512; + private static final int bufferSize = 512; - static final int clobBufferSize = 512; - - static final int stringInitialSize = 512; + static final int clobBufferSize = 512; - private final ResultSet rset; - - protected int pos; - - public RsetDataReader(ResultSet rset) { - this.rset = rset; + static final int stringInitialSize = 512; + + private final ResultSet rset; + + protected int pos; + + public RsetDataReader(ResultSet rset) { + this.rset = rset; + } + + public void close() throws SQLException { + rset.close(); + } + + public boolean next() throws SQLException { + return rset.next(); + } + + public void resetColumnPosition() { + pos = 0; + } + + public void incrementPos(int increment) { + pos += increment; + } + + protected int pos() { + return ++pos; + } + + public Array getArray() throws SQLException { + return rset.getArray(pos()); + } + + public InputStream getAsciiStream() throws SQLException { + return rset.getAsciiStream(pos()); + } + + public Object getObject() throws SQLException { + return rset.getObject(pos()); + } + + public BigDecimal getBigDecimal() throws SQLException { + return rset.getBigDecimal(pos()); + } + + + public InputStream getBinaryStream() throws SQLException { + return rset.getBinaryStream(pos()); + } + + public Boolean getBoolean() throws SQLException { + boolean v = rset.getBoolean(pos()); + if (rset.wasNull()) { + return null; + } + return v; + } + + public Byte getByte() throws SQLException { + byte v = rset.getByte(pos()); + if (rset.wasNull()) { + return null; + } + return v; + } + + public byte[] getBytes() throws SQLException { + return rset.getBytes(pos()); + } + + public Date getDate() throws SQLException { + return rset.getDate(pos()); + } + + public Double getDouble() throws SQLException { + double v = rset.getDouble(pos()); + if (rset.wasNull()) { + return null; + } + return v; + } + + public Float getFloat() throws SQLException { + float v = rset.getFloat(pos()); + if (rset.wasNull()) { + return null; + } + return v; + } + + public Integer getInt() throws SQLException { + int v = rset.getInt(pos()); + if (rset.wasNull()) { + return null; + } + return v; + } + + + public Long getLong() throws SQLException { + long v = rset.getLong(pos()); + if (rset.wasNull()) { + return null; + } + return v; + } + + + public Ref getRef() throws SQLException { + return rset.getRef(pos()); + } + + + public Short getShort() throws SQLException { + short s = rset.getShort(pos()); + if (rset.wasNull()) { + return null; + } + return s; + } + + + public String getString() throws SQLException { + return rset.getString(pos()); + } + + + public Time getTime() throws SQLException { + return rset.getTime(pos()); + } + + + public Timestamp getTimestamp() throws SQLException { + return rset.getTimestamp(pos()); + } + + public String getStringFromStream() throws SQLException { + Reader reader = rset.getCharacterStream(pos()); + if (reader == null) { + return null; + } + return readStringLob(reader); + } + + public String getStringClob() throws SQLException { + + Clob clob = rset.getClob(pos()); + if (clob == null) { + return null; + } + Reader reader = clob.getCharacterStream(); + if (reader == null) { + return null; + } + return readStringLob(reader); + } + + protected String readStringLob(Reader reader) throws SQLException { + + char[] buffer = new char[clobBufferSize]; + int readLength; + StringBuilder out = new StringBuilder(stringInitialSize); + try { + while ((readLength = reader.read(buffer)) != -1) { + out.append(buffer, 0, readLength); + } + reader.close(); + } catch (IOException e) { + throw new SQLException(Message.msg("persist.clob.io", e.getMessage())); } - public void close() throws SQLException { - rset.close(); + return out.toString(); + } + + public byte[] getBinaryBytes() throws SQLException { + InputStream in = rset.getBinaryStream(pos()); + return getBinaryLob(in); + } + + public byte[] getBlobBytes() throws SQLException { + Blob blob = rset.getBlob(pos()); + if (blob == null) { + return null; } + InputStream in = blob.getBinaryStream(); + return getBinaryLob(in); + } - public boolean next() throws SQLException { - return rset.next(); - } - - public void resetColumnPosition() { - pos = 0; - } - - public void incrementPos(int increment){ - pos += increment; - } - - protected int pos() { - return ++pos; - } - - public Array getArray() throws SQLException { - return rset.getArray(pos()); - } - - - public InputStream getAsciiStream() throws SQLException { - return rset.getAsciiStream(pos()); - } - - public Object getObject() throws SQLException { - return rset.getObject(pos()); - } - - public BigDecimal getBigDecimal() throws SQLException { - return rset.getBigDecimal(pos()); - } - - - public InputStream getBinaryStream() throws SQLException { - return rset.getBinaryStream(pos()); - } - - public Boolean getBoolean() throws SQLException { - boolean v = rset.getBoolean(pos()); - if (rset.wasNull()){ - return null; - } - return Boolean.valueOf(v); - } - - public Byte getByte() throws SQLException { - byte v = rset.getByte(pos()); - if (rset.wasNull()){ - return null; - } - return Byte.valueOf(v); - } - - public byte[] getBytes() throws SQLException { - return rset.getBytes(pos()); - } - - public Date getDate() throws SQLException { - return rset.getDate(pos()); - } - - public Double getDouble() throws SQLException { - double v = rset.getDouble(pos()); - if (rset.wasNull()){ - return null; - } - return Double.valueOf(v); - } - - public Float getFloat() throws SQLException { - float v = rset.getFloat(pos()); - if (rset.wasNull()){ - return null; - } - return Float.valueOf(v); - } - - public Integer getInt() throws SQLException { - int v = rset.getInt(pos()); - if (rset.wasNull()){ - return null; - } - return Integer.valueOf(v); - } - - - public Long getLong() throws SQLException { - long v = rset.getLong(pos()); - if (rset.wasNull()){ - return null; - } - return Long.valueOf(v); - } - - - public Ref getRef() throws SQLException { - return rset.getRef(pos()); - } - - - public Short getShort() throws SQLException { - short s = rset.getShort(pos()); - if (rset.wasNull()){ - return null; - } - return Short.valueOf(s); - } - - - public String getString() throws SQLException { - return rset.getString(pos()); - } - - - public Time getTime() throws SQLException { - return rset.getTime(pos()); - } - - - public Timestamp getTimestamp() throws SQLException { - return rset.getTimestamp(pos()); - } - - public String getStringFromStream() throws SQLException { - Reader reader = rset.getCharacterStream(pos()); - if (reader == null) { - return null; - } - return readStringLob(reader); - } - - public String getStringClob() throws SQLException { - - Clob clob = rset.getClob(pos()); - if (clob == null) { - return null; - } - Reader reader = clob.getCharacterStream(); - if (reader == null) { - return null; - } - return readStringLob(reader); - } - - protected String readStringLob(Reader reader) throws SQLException { - - char[] buffer = new char[clobBufferSize]; - int readLength = 0; - StringBuilder out = new StringBuilder(stringInitialSize); - try { - while ((readLength = reader.read(buffer)) != -1) { - out.append(buffer, 0, readLength); - } - reader.close(); - } catch (IOException e) { - throw new SQLException(Message.msg("persist.clob.io", e.getMessage())); - } - - return out.toString(); - } - - public byte[] getBinaryBytes() throws SQLException { - InputStream in = rset.getBinaryStream(pos()); - return getBinaryLob(in); - } - - public byte[] getBlobBytes() throws SQLException { - Blob blob = rset.getBlob(pos()); - if (blob == null) { - return null; - } - InputStream in = blob.getBinaryStream(); - return getBinaryLob(in); - } - - protected byte[] getBinaryLob(InputStream in) throws SQLException { - - try { - if (in == null) { - return null; - } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - - byte[] buf = new byte[bufferSize]; - int len; - while ((len = in.read(buf, 0, buf.length)) != -1) { - out.write(buf, 0, len); - } - byte[] data = out.toByteArray(); - - if (data.length == 0) { - data = null; - } - in.close(); - out.close(); - return data; - - } catch (IOException e) { - throw new SQLException(e.getClass().getName() + ":" + e.getMessage()); - } + protected byte[] getBinaryLob(InputStream in) throws SQLException { + + try { + if (in == null) { + return null; + } + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + byte[] buf = new byte[bufferSize]; + int len; + while ((len = in.read(buf, 0, buf.length)) != -1) { + out.write(buf, 0, len); + } + byte[] data = out.toByteArray(); + + if (data.length == 0) { + data = null; + } + in.close(); + out.close(); + return data; + + } catch (IOException e) { + throw new SQLException(e.getClass().getName() + ":" + e.getMessage()); } + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReaderIndexed.java b/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReaderIndexed.java index 8d72e0db5..0b774949a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReaderIndexed.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReaderIndexed.java @@ -4,25 +4,25 @@ import java.sql.ResultSet; public class RsetDataReaderIndexed extends RsetDataReader { - private final int[] rsetIndexPositions; - - public RsetDataReaderIndexed(ResultSet rset, int[] rsetIndexPositions, boolean rowNumberIncluded) { - super(rset); - if (!rowNumberIncluded){ - this.rsetIndexPositions = rsetIndexPositions; - } else { - this.rsetIndexPositions = new int[rsetIndexPositions.length+1]; - for (int i = 0; i < rsetIndexPositions.length; i++) { - // increment all the column indexes by 1 - this.rsetIndexPositions[i+1] = rsetIndexPositions[i]+1; - } - } - } + private final int[] rsetIndexPositions; - @Override - protected int pos() { - int i = pos++; - return rsetIndexPositions[i]; + public RsetDataReaderIndexed(ResultSet rset, int[] rsetIndexPositions, boolean rowNumberIncluded) { + super(rset); + if (!rowNumberIncluded) { + this.rsetIndexPositions = rsetIndexPositions; + } else { + this.rsetIndexPositions = new int[rsetIndexPositions.length + 1]; + for (int i = 0; i < rsetIndexPositions.length; i++) { + // increment all the column indexes by 1 + this.rsetIndexPositions[i + 1] = rsetIndexPositions[i] + 1; + } } + } + + @Override + protected int pos() { + int i = pos++; + return rsetIndexPositions[i]; + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarDataReader.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarDataReader.java index 1140bfb10..73984338b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarDataReader.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarDataReader.java @@ -7,24 +7,24 @@ import java.sql.SQLException; */ public interface ScalarDataReader { - /** - * Read and return the appropriate value from the dataReader. - */ - public T read(DataReader dataReader) throws SQLException; + /** + * Read and return the appropriate value from the dataReader. + */ + T read(DataReader dataReader) throws SQLException; - /** - * Ignore typically by moving the index position. - */ - public void loadIgnore(DataReader dataReader); + /** + * Ignore typically by moving the index position. + */ + void loadIgnore(DataReader dataReader); - /** - * Bind the value to the underlying preparedStatement. - */ - public void bind(DataBind b, T value) throws SQLException; + /** + * Bind the value to the underlying preparedStatement. + */ + void bind(DataBind b, T value) throws SQLException; - /** - * Accumulate all the scalar types used by an immutable compound value type. - */ - public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list); + /** + * Accumulate all the scalar types used by an immutable compound value type. + */ + void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarType.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarType.java index 3ad716c72..03a2323e9 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarType.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarType.java @@ -38,143 +38,143 @@ public interface ScalarType extends StringParser, StringFormatter, ScalarData /** * Return true if this is a mutable scalar type (like hstore). */ - public boolean isMutable(); - + boolean isMutable(); + /** * For mutable scalarType's return true if the value is dirty. * Non-dirty properties may be excluded from updates. */ - public boolean isDirty(Object value); - - /** - * Return the default DB column length for this type. - *

- * If a BeanProperty has no explicit length defined then this length should - * be assigned. - *

- *

- * This is primarily to support defining a length on Enum types (to - * supplement defining the length on the BeanProperty directly). - *

- */ - public int getLength(); + boolean isDirty(Object value); - /** - * Return true if the type is native to JDBC. - *

- * If it is native to JDBC then its values/instances do not need to be - * converted to and from an associated JDBC type. - *

- */ - public boolean isJdbcNative(); + /** + * Return the default DB column length for this type. + *

+ * If a BeanProperty has no explicit length defined then this length should + * be assigned. + *

+ *

+ * This is primarily to support defining a length on Enum types (to + * supplement defining the length on the BeanProperty directly). + *

+ */ + int getLength(); - /** - * Return the type as per java.sql.Types that this maps to. - *

- * This type should be consistent with the toJdbcType() method in converting - * the type to the appropriate type for binding to preparedStatements. - *

- */ - public int getJdbcType(); + /** + * Return true if the type is native to JDBC. + *

+ * If it is native to JDBC then its values/instances do not need to be + * converted to and from an associated JDBC type. + *

+ */ + boolean isJdbcNative(); - /** - * Return the type that matches the bean property type. - *

- * This represents the 'logical' type rather than the JDBC type this maps - * to. - *

- */ - public Class getType(); + /** + * Return the type as per java.sql.Types that this maps to. + *

+ * This type should be consistent with the toJdbcType() method in converting + * the type to the appropriate type for binding to preparedStatements. + *

+ */ + int getJdbcType(); - /** - * Read the value from the resultSet and convert if necessary to the logical - * bean property value. - */ - public T read(DataReader dataReader) throws SQLException; + /** + * Return the type that matches the bean property type. + *

+ * This represents the 'logical' type rather than the JDBC type this maps + * to. + *

+ */ + Class getType(); - /** - * Ignore the reading of this value. Typically this means moving the index - * position in the ResultSet. - */ - public void loadIgnore(DataReader dataReader); + /** + * Read the value from the resultSet and convert if necessary to the logical + * bean property value. + */ + T read(DataReader dataReader) throws SQLException; - /** - * Convert (if necessary) and bind the value to the preparedStatement. - *

- * value may need to be converted from the logical bean property type to the - * JDBC type. - *

- */ - public void bind(DataBind b, T value) throws SQLException; + /** + * Ignore the reading of this value. Typically this means moving the index + * position in the ResultSet. + */ + void loadIgnore(DataReader dataReader); - /** - * Convert the value as necessary to the JDBC type. - *

- * Note that this should also match the type as per the getJdbcType() - * method. - *

- *

- * This is typically used when the matching type is used in a where clause - * and we use this to ensure it is an appropriate jdbc type. - *

- */ - public Object toJdbcType(Object value); + /** + * Convert (if necessary) and bind the value to the preparedStatement. + *

+ * value may need to be converted from the logical bean property type to the + * JDBC type. + *

+ */ + void bind(DataBind b, T value) throws SQLException; - /** - * Convert the value as necessary to the logical Bean type. - *

- * The type as per the bean property. - *

- *

- * This is used to automatically convert id values (typically from a string - * to a int, long or UUID). - *

- */ - public T toBeanType(Object value); + /** + * Convert the value as necessary to the JDBC type. + *

+ * Note that this should also match the type as per the getJdbcType() + * method. + *

+ *

+ * This is typically used when the matching type is used in a where clause + * and we use this to ensure it is an appropriate jdbc type. + *

+ */ + Object toJdbcType(Object value); - /** - * Convert the type into a string representation. - *

- * Reciprocal of parse(). - *

- */ - public String formatValue(T v); + /** + * Convert the value as necessary to the logical Bean type. + *

+ * The type as per the bean property. + *

+ *

+ * This is used to automatically convert id values (typically from a string + * to a int, long or UUID). + *

+ */ + T toBeanType(Object value); - /** - * Convert the type into a string representation. - *

- * This assumes the value is of the correct type. - *

- *

- * This is so that ScalarType also implements the StringFormatter interface. - *

- */ - public String format(Object v); + /** + * Convert the type into a string representation. + *

+ * Reciprocal of parse(). + *

+ */ + String formatValue(T v); - /** - * Convert the string value to the appropriate java object. - *

- * Mostly used to support CSV, JSON and XML parsing. - *

- *

- * Reciprocal of formatValue(). - *

- */ - public T parse(String value); + /** + * Convert the type into a string representation. + *

+ * This assumes the value is of the correct type. + *

+ *

+ * This is so that ScalarType also implements the StringFormatter interface. + *

+ */ + String format(Object v); - /** - * Return true if the type can accept long systemTimeMillis input. - *

- * This is used to determine if is is sensible to use the - * {@link #convertFromMillis(long)} method. - *

- *

- * This includes the Date, Calendar, sql Date, Time, Timestamp, JODA types - * as well as Long, BigDecimal and String (although it generally is not - * expected to parse systemTimeMillis to a String or BigDecimal). - *

- */ - public boolean isDateTimeCapable(); + /** + * Convert the string value to the appropriate java object. + *

+ * Mostly used to support CSV, JSON and XML parsing. + *

+ *

+ * Reciprocal of formatValue(). + *

+ */ + T parse(String value); + + /** + * Return true if the type can accept long systemTimeMillis input. + *

+ * This is used to determine if is is sensible to use the + * {@link #convertFromMillis(long)} method. + *

+ *

+ * This includes the Date, Calendar, sql Date, Time, Timestamp, JODA types + * as well as Long, BigDecimal and String (although it generally is not + * expected to parse systemTimeMillis to a String or BigDecimal). + *

+ */ + boolean isDateTimeCapable(); /** * Convert the systemTimeMillis into the appropriate java object. @@ -182,26 +182,26 @@ public interface ScalarType extends StringParser, StringFormatter, ScalarData * For non dateTime types this will throw an exception. *

*/ - public T convertFromMillis(long dateTime); + T convertFromMillis(long dateTime); /** - * Read the value from binary input. - */ - public T readData(DataInput dataInput) throws IOException; + * Read the value from binary input. + */ + T readData(DataInput dataInput) throws IOException; /** * Write the value to binary output. */ - public void writeData(DataOutput dataOutput, T v) throws IOException; + void writeData(DataOutput dataOutput, T v) throws IOException; /** * Read the value from JsonParser. */ - public T jsonRead(JsonParser ctx, JsonToken event) throws IOException; + T jsonRead(JsonParser ctx, JsonToken event) throws IOException; /** * Write the value to the JsonGenerator. */ - public void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException; - + void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException; + } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDateTime.java index 708f2d36b..984a48f13 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDateTime.java @@ -12,7 +12,6 @@ import java.math.BigDecimal; import java.sql.SQLException; import java.sql.Timestamp; import java.sql.Types; -import java.time.Instant; /** * Base type for DateTime types. @@ -98,7 +97,7 @@ public abstract class ScalarTypeBaseDateTime extends ScalarTypeBase { } default: { String jsonDateTime = ctx.getText(); - return convertFromTimestamp(dateTimeParser.parse(jsonDateTime)); + return convertFromTimestamp(dateTimeParser.parse(jsonDateTime)); } } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseVarchar.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseVarchar.java index f3009983e..5a5c94298 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseVarchar.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseVarchar.java @@ -89,7 +89,7 @@ public abstract class ScalarTypeBaseVarchar extends ScalarTypeBase { public T convertFromMillis(long systemTimeMillis) { throw new TextException("Not Supported"); } - + @Override public boolean isDateTimeCapable() { return false; @@ -121,12 +121,12 @@ public abstract class ScalarTypeBaseVarchar extends ScalarTypeBase { dataOutput.writeUTF(s); } } - + @Override public T jsonRead(JsonParser ctx, JsonToken event) throws IOException { return parse(ctx.getValueAsString()); } - + @Override public void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException { ctx.writeStringField(name, format(value)); diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBigDecimal.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBigDecimal.java index c45e960db..a5abb5d99 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBigDecimal.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBigDecimal.java @@ -83,7 +83,7 @@ public class ScalarTypeBigDecimal extends ScalarTypeBase { } public void jsonWrite(JsonGenerator ctx, String name, BigDecimal value) throws IOException { - ctx.writeNumberField(name, (BigDecimal) value); + ctx.writeNumberField(name, value); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBoolean.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBoolean.java index 4d76e3805..9a38561ae 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBoolean.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBoolean.java @@ -53,7 +53,7 @@ public class ScalarTypeBoolean { /** * The Class BitBoolean converts a JDBC type BIT to a java boolean - * + *

*

* Sometimes booleans may be mapped to the JDBC type BIT. To use the BitBoolean specify * type.boolean.dbtype="bit" in the ebean configuration @@ -143,11 +143,7 @@ public class ScalarTypeBoolean { return null; } Boolean b = (Boolean) value; - if (b.booleanValue()) { - return trueValue; - } else { - return falseValue; - } + return b ? trueValue : falseValue; } /** @@ -222,11 +218,7 @@ public class ScalarTypeBoolean { return null; } Boolean b = (Boolean) value; - if (b.booleanValue()) { - return trueValue; - } else { - return falseValue; - } + return b ? trueValue : falseValue; } /** @@ -293,7 +285,7 @@ public class ScalarTypeBoolean { } public void jsonWrite(JsonGenerator ctx, String name, Boolean value) throws IOException { - ctx.writeBooleanField(name, (Boolean) value); + ctx.writeBooleanField(name, value); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBase.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBase.java index 0fa8c65a0..405672490 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBase.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBase.java @@ -20,10 +20,6 @@ public abstract class ScalarTypeBytesBase extends ScalarTypeBase { super(byte[].class, jdbcNative, jdbcType); } - public Object convertFromBytes(byte[] bytes) { - return bytes; - } - public byte[] convertToBytes(Object value) { return (byte[]) value; } @@ -46,7 +42,7 @@ public abstract class ScalarTypeBytesBase extends ScalarTypeBase { @Override public void jsonWrite(JsonGenerator ctx, String name, byte[] value) throws IOException { - ctx.writeBinaryField(name, (byte[]) value); + ctx.writeBinaryField(name, value); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesEncrypted.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesEncrypted.java index d739a24f6..439ccf4ad 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesEncrypted.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesEncrypted.java @@ -12,9 +12,6 @@ import com.fasterxml.jackson.core.JsonToken; /** * Encrypted ScalarType that wraps a byte[] types. - * - * @author rbygrave - * */ public class ScalarTypeBytesEncrypted implements ScalarType { @@ -68,7 +65,7 @@ public class ScalarTypeBytesEncrypted implements ScalarType { @Override public void jsonWrite(JsonGenerator ctx, String name, byte[] value) throws IOException { - ctx.writeBinaryField(name, (byte[]) value); + ctx.writeBinaryField(name, value); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClass.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClass.java index a1227e719..8ee8f8b30 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClass.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClass.java @@ -4,11 +4,11 @@ import javax.persistence.PersistenceException; /** * ScalarType for Class that persists it to VARCHAR column. - * + * * @author emcgreal * @author rbygrave */ -@SuppressWarnings({ "rawtypes" }) +@SuppressWarnings({"rawtypes"}) public class ScalarTypeClass extends ScalarTypeBaseVarchar { public ScalarTypeClass() { @@ -41,5 +41,5 @@ public class ScalarTypeClass extends ScalarTypeBaseVarchar { throw new PersistenceException("Unable to find Class " + value, e); } } - + } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClob.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClob.java index d052373d5..6a9665870 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClob.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClob.java @@ -9,11 +9,7 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter; * ScalarType for String. */ public class ScalarTypeClob extends ScalarTypeBaseVarchar { - - static final int clobBufferSize = 512; - - static final int stringInitialSize = 512; - + protected ScalarTypeClob(boolean jdbcNative, int jdbcType) { super(String.class, jdbcNative, jdbcType); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCurrency.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCurrency.java index 076c65c21..70cdcb3cf 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCurrency.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCurrency.java @@ -23,7 +23,7 @@ public class ScalarTypeCurrency extends ScalarTypeBaseVarchar { @Override public String convertToDbString(Currency beanValue) { - return ((Currency) beanValue).getCurrencyCode(); + return beanValue.getCurrencyCode(); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDouble.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDouble.java index ce8c4f3ad..979529583 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDouble.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDouble.java @@ -25,7 +25,7 @@ public class ScalarTypeDouble extends ScalarTypeBase { if (value == null) { b.setNull(Types.DOUBLE); } else { - b.setDouble(value.doubleValue()); + b.setDouble(value); } } @@ -56,7 +56,7 @@ public class ScalarTypeDouble extends ScalarTypeBase { @Override public Double convertFromMillis(long systemTimeMillis) { - return Double.valueOf(systemTimeMillis); + return (double) systemTimeMillis; } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDuration.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDuration.java index 5cd093ed3..f224b8f6d 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDuration.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDuration.java @@ -52,8 +52,8 @@ public class ScalarTypeDuration extends ScalarTypeBase { @Override public Object toJdbcType(Object value) { - if (value instanceof Long) return value; - return ((Duration)value).getSeconds(); + if (value instanceof Long) return value; + return ((Duration) value).getSeconds(); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDurationWithNanos.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDurationWithNanos.java index 42f05ffb8..d629ece79 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDurationWithNanos.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDurationWithNanos.java @@ -1,14 +1,7 @@ package com.avaje.ebeaninternal.server.type; -import com.avaje.ebean.text.TextException; import com.avaje.ebeaninternal.server.core.BasicTypeConverter; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -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; @@ -17,7 +10,7 @@ import java.time.Duration; /** * ScalarType for java.time.Duration (with Nanos precision). *

- * Stored in the DB as DECIMAL value. + * Stored in the DB as DECIMAL value. *

*/ public class ScalarTypeDurationWithNanos extends ScalarTypeDuration { @@ -42,8 +35,8 @@ public class ScalarTypeDurationWithNanos extends ScalarTypeDuration { @Override public Object toJdbcType(Object value) { - if (value instanceof BigDecimal) return value; - return convertToBigDecimal((Duration)value); + if (value instanceof BigDecimal) return value; + return convertToBigDecimal((Duration) value); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEncryptedWrapper.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEncryptedWrapper.java index 49153679c..ca5901a01 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEncryptedWrapper.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEncryptedWrapper.java @@ -136,7 +136,7 @@ public class ScalarTypeEncryptedWrapper implements ScalarType { public T jsonRead(JsonParser ctx, JsonToken event) throws IOException { return wrapped.jsonRead(ctx, event); } - + @Override public void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException { wrapped.jsonWrite(ctx, name, value); diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnum.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnum.java index f9479adcd..a857488d0 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnum.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnum.java @@ -5,9 +5,9 @@ package com.avaje.ebeaninternal.server.type; */ public interface ScalarTypeEnum { - /** - * Return the IN values for DB constraint construction. - */ - public String getConstraintInValues(); - + /** + * Return the IN values for DB constraint construction. + */ + String getConstraintInValues(); + } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumStandard.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumStandard.java index 4180250bb..6be6e65a3 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumStandard.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumStandard.java @@ -25,7 +25,7 @@ import com.fasterxml.jackson.core.JsonToken; */ public class ScalarTypeEnumStandard { - @SuppressWarnings({ "rawtypes", "unchecked" }) + @SuppressWarnings({"rawtypes", "unchecked"}) public static class StringEnum extends EnumBase implements ScalarTypeEnum { private final int length; @@ -113,7 +113,7 @@ public class ScalarTypeEnumStandard { } - @SuppressWarnings({ "rawtypes", "unchecked" }) + @SuppressWarnings({"rawtypes", "unchecked"}) public static class OrdinalEnum extends EnumBase implements ScalarTypeEnum { private final Object[] enumArray; @@ -196,7 +196,7 @@ public class ScalarTypeEnumStandard { } - @SuppressWarnings({ "rawtypes", "unchecked" }) + @SuppressWarnings({"rawtypes", "unchecked"}) public abstract static class EnumBase extends ScalarTypeBase { protected final Class enumType; diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumWithMapping.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumWithMapping.java index cedb8e767..4e6124169 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumWithMapping.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumWithMapping.java @@ -6,7 +6,7 @@ import java.util.Iterator; /** * Additional control over mapping to DB values. */ -@SuppressWarnings({ "unchecked", "rawtypes" }) +@SuppressWarnings({"unchecked", "rawtypes"}) public class ScalarTypeEnumWithMapping extends ScalarTypeEnumStandard.EnumBase implements ScalarType, ScalarTypeEnum { private final EnumToDbValueMap beanDbMap; diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFloat.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFloat.java index 06f4f3c1a..b9276dce6 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFloat.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFloat.java @@ -25,7 +25,7 @@ public class ScalarTypeFloat extends ScalarTypeBase { if (value == null) { b.setNull(Types.REAL); } else { - b.setFloat(value.floatValue()); + b.setFloat(value); } } @@ -56,7 +56,7 @@ public class ScalarTypeFloat extends ScalarTypeBase { @Override public Float convertFromMillis(long systemTimeMillis) { - return Float.valueOf(systemTimeMillis); + return (float) systemTimeMillis; } @Override @@ -69,8 +69,7 @@ public class ScalarTypeFloat extends ScalarTypeBase { if (!dataInput.readBoolean()) { return null; } else { - float val = dataInput.readFloat(); - return Float.valueOf(val); + return dataInput.readFloat(); } } @@ -92,6 +91,6 @@ public class ScalarTypeFloat extends ScalarTypeBase { @Override public void jsonWrite(JsonGenerator ctx, String name, Float value) throws IOException { - ctx.writeNumberField(name, (Float) value); + ctx.writeNumberField(name, value); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInteger.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInteger.java index 40d851867..574b16895 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInteger.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInteger.java @@ -26,7 +26,7 @@ public class ScalarTypeInteger extends ScalarTypeBase { if (value == null) { b.setNull(Types.INTEGER); } else { - b.setInt(value.intValue()); + b.setInt(value); } } @@ -40,7 +40,7 @@ public class ScalarTypeInteger extends ScalarTypeBase { if (!dataInput.readBoolean()) { return null; } else { - return Integer.valueOf(dataInput.readInt()); + return dataInput.readInt(); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateTime.java index 797ce69f9..2f2e554a0 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateTime.java @@ -1,14 +1,12 @@ package com.avaje.ebeaninternal.server.type; +import com.avaje.ebean.config.JsonConfig; +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; +import org.joda.time.DateTime; + import java.sql.Timestamp; import java.sql.Types; -import com.avaje.ebean.config.JsonConfig; -import org.joda.time.DateTime; - -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; -import org.joda.time.LocalDateTime; - /** * ScalarType for Joda DateTime. This maps to a JDBC Timestamp. */ diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDate.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDate.java index 30f39d4b9..529aaf8b3 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDate.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDate.java @@ -23,7 +23,7 @@ public class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate { @Override public LocalDate convertFromDate(Date ts) { - return new LocalDate(((java.util.Date) ts).getTime()); + return new LocalDate(ts.getTime()); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDateTime.java index e745480ad..12f209a46 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDateTime.java @@ -6,7 +6,6 @@ import java.sql.Timestamp; import java.sql.Types; import java.time.LocalDateTime; import java.time.ZoneId; -import java.time.ZoneOffset; import java.time.ZonedDateTime; /** diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTime.java index 32971086e..2dcfeb079 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTime.java @@ -44,8 +44,8 @@ public class ScalarTypeLocalTime extends ScalarTypeBase { @Override public Object toJdbcType(Object value) { - if (value instanceof Time) return value; - return Time.valueOf((LocalTime)value); + if (value instanceof Time) return value; + return Time.valueOf((LocalTime) value); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java index deab02e3e..cecaa40d4 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java @@ -32,8 +32,8 @@ public class ScalarTypeLocalTimeWithNanos extends ScalarTypeLocalTime { @Override public Object toJdbcType(Object value) { - if (value instanceof Long) return value; - return ((LocalTime)value).toNanoOfDay(); + if (value instanceof Long) return value; + return ((LocalTime) value).toNanoOfDay(); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocale.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocale.java index 62ec137ca..05c876b85 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocale.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocale.java @@ -23,7 +23,7 @@ public class ScalarTypeLocale extends ScalarTypeBaseVarchar { @Override public String convertToDbString(Locale beanValue) { - return ((Locale) beanValue).toString(); + return beanValue.toString(); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLong.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLong.java index f14190777..2a114c08a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLong.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLong.java @@ -25,7 +25,7 @@ public class ScalarTypeLong extends ScalarTypeBase { if (value == null) { b.setNull(Types.BIGINT); } else { - b.setLong(value.longValue()); + b.setLong(value); } } @@ -91,6 +91,6 @@ public class ScalarTypeLong extends ScalarTypeBase { @Override public void jsonWrite(JsonGenerator ctx, String name, Long value) throws IOException { - ctx.writeNumberField(name, (Long) value); + ctx.writeNumberField(name, value); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongToTimestamp.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongToTimestamp.java index b6444a772..51c1eda9d 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongToTimestamp.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongToTimestamp.java @@ -6,7 +6,7 @@ import java.sql.Timestamp; public class ScalarTypeLongToTimestamp extends ScalarTypeWrapper { - public ScalarTypeLongToTimestamp(JsonConfig.DateTime mode) { - super(Long.class, new ScalarTypeTimestamp(mode), new LongToTimestampConverter()); - } + public ScalarTypeLongToTimestamp(JsonConfig.DateTime mode) { + super(Long.class, new ScalarTypeTimestamp(mode), new LongToTimestampConverter()); + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMathBigInteger.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMathBigInteger.java index d32e1150b..60d56cd23 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMathBigInteger.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMathBigInteger.java @@ -1,19 +1,17 @@ package com.avaje.ebeaninternal.server.type; -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.sql.SQLException; -import java.sql.Types; -import java.time.Instant; - import com.avaje.ebeaninternal.server.core.BasicTypeConverter; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.math.BigInteger; +import java.sql.SQLException; +import java.sql.Types; + /** * ScalarType for java.math.BigInteger. */ @@ -100,7 +98,7 @@ public class ScalarTypeMathBigInteger extends ScalarTypeBase { @Override public void jsonWrite(JsonGenerator ctx, String name, BigInteger value) throws IOException { - ctx.writeNumberField(name, ((BigInteger) value).longValue()); + ctx.writeNumberField(name, value.longValue()); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMonth.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMonth.java index e6f65417c..faa863c5b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMonth.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMonth.java @@ -31,7 +31,7 @@ public class ScalarTypeMonth extends ScalarTypeEnumWithMapping { b.setNull(Types.INTEGER); } else { // avoiding the map lookup - b.setInt(((Month)value).getValue()); + b.setInt(((Month) value).getValue()); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetDateTime.java index 0f6dc1ec3..9864580e6 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetDateTime.java @@ -50,12 +50,12 @@ public class ScalarTypeOffsetDateTime extends ScalarTypeBaseDateTime { public static final String KEY = "hstore"; - + public static final int HSTORE_TYPE = PostgresPlatform.TYPE_HSTORE; - + public ScalarTypePostgresHstore() { super(Map.class, false, HSTORE_TYPE); } - + @Override public boolean isMutable() { return true; } - + @Override public boolean isDirty(Object value) { - if (value instanceof ModifyAwareOwner) { - return ((ModifyAwareOwner)value).isMarkedDirty(); - } - return true; + return !(value instanceof ModifyAwareOwner) || ((ModifyAwareOwner) value).isMarkedDirty(); } @SuppressWarnings("unchecked") @Override public Map read(DataReader dataReader) throws SQLException { - + Object value = dataReader.getObject(); if (value == null) { return null; } if (!(value instanceof Map)) { - throw new RuntimeException("Expecting Hstore to return as Map but got type "+value.getClass()); + throw new RuntimeException("Expecting Hstore to return as Map but got type " + value.getClass()); } - return new ModifyAwareMap((Map)value); + return new ModifyAwareMap((Map) value); } @Override @@ -66,7 +63,7 @@ public class ScalarTypePostgresHstore extends ScalarTypeBase { @Override public Map toBeanType(Object value) { - return (Map)value; + return (Map) value; } @Override @@ -136,5 +133,5 @@ public class ScalarTypePostgresHstore extends ScalarTypeBase { // the EJson parsing so that it knows the first token has been read return EJson.parseObject(ctx, event); } - + } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeShort.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeShort.java index 43f7a7b3f..f1993c50a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeShort.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeShort.java @@ -26,7 +26,7 @@ public class ScalarTypeShort extends ScalarTypeBase { if (value == null) { b.setNull(Types.SMALLINT); } else { - b.setShort(value.shortValue()); + b.setShort(value); } } @@ -92,6 +92,6 @@ public class ScalarTypeShort extends ScalarTypeBase { @Override public void jsonWrite(JsonGenerator ctx, String name, Short value) throws IOException { - ctx.writeNumberField(name, (Short) value); + ctx.writeNumberField(name, value); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeString.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeString.java index 3608c14bd..a9fd9c72a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeString.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeString.java @@ -91,6 +91,6 @@ public class ScalarTypeString extends ScalarTypeBase { @Override public void jsonWrite(JsonGenerator ctx, String name, String value) throws IOException { - ctx.writeStringField(name, (String) value); + ctx.writeStringField(name, value); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTimeZone.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTimeZone.java index 8134b2b6a..c20392b73 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTimeZone.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTimeZone.java @@ -23,7 +23,7 @@ public class ScalarTypeTimeZone extends ScalarTypeBaseVarchar { @Override public String convertToDbString(TimeZone beanValue) { - return ((TimeZone) beanValue).getID(); + return beanValue.getID(); } @Override diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTimestamp.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTimestamp.java index 0f0630de8..07ddc9fd8 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTimestamp.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTimestamp.java @@ -1,12 +1,11 @@ package com.avaje.ebeaninternal.server.type; +import com.avaje.ebean.config.JsonConfig; +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + import java.sql.SQLException; import java.sql.Timestamp; import java.sql.Types; -import java.time.OffsetDateTime; - -import com.avaje.ebean.config.JsonConfig; -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; /** * ScalarType for java.sql.Timestamp. @@ -48,7 +47,6 @@ public class ScalarTypeTimestamp extends ScalarTypeBaseDateTime { } - @Override public void bind(DataBind b, Timestamp value) throws SQLException { if (value == null) { diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeUtilDate.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeUtilDate.java index dd7ef67bc..d0dfaa6ee 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeUtilDate.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeUtilDate.java @@ -46,7 +46,7 @@ public class ScalarTypeUtilDate { @Override public void bind(DataBind b, java.util.Date value) - throws SQLException { + throws SQLException { if (value == null) { b.setNull(Types.TIMESTAMP); } else { diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeWrapper.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeWrapper.java index 5a277ac57..7c7dcd1b1 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeWrapper.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeWrapper.java @@ -16,13 +16,9 @@ import com.fasterxml.jackson.core.JsonToken; *

* Enables the use of a simple interface to add additional scalarTypes. *

- * - * @author rbygrave - * - * @param - * the logical type - * @param - * the underlying scalar type this is converted to + * + * @param the logical type + * @param the underlying scalar type this is converted to */ public class ScalarTypeWrapper implements ScalarType { diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeYear.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeYear.java index 40c669288..dda42be1c 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeYear.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeYear.java @@ -58,7 +58,7 @@ public class ScalarTypeYear extends ScalarTypeBase { @Override public Object toJdbcType(Object value) { - if (value instanceof Year) return ((Year)value).getValue(); + if (value instanceof Year) return ((Year) value).getValue(); return BasicTypeConverter.toInteger(value); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeYearMonthDate.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeYearMonthDate.java index 46f5d88db..feac3737d 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeYearMonthDate.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeYearMonthDate.java @@ -53,7 +53,7 @@ public class ScalarTypeYearMonthDate extends ScalarTypeBaseDate { public Object toJdbcType(Object value) { if (value instanceof Date) return value; if (value instanceof YearMonth) return Date.valueOf(toLocalDate((YearMonth) value)); - if (value instanceof LocalDate) return Date.valueOf((LocalDate)value); + if (value instanceof LocalDate) return Date.valueOf((LocalDate) value); return BasicTypeConverter.toDate(value); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeZoneId.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeZoneId.java index 74338493b..b31424a5d 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeZoneId.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeZoneId.java @@ -1,6 +1,5 @@ package com.avaje.ebeaninternal.server.type; -import java.time.OffsetTime; import java.time.ZoneId; /** diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeZonedDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeZonedDateTime.java index 079f8499b..5b970da4a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeZonedDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeZonedDateTime.java @@ -5,7 +5,6 @@ import com.avaje.ebean.config.JsonConfig; import java.sql.Timestamp; import java.sql.Types; import java.time.Instant; -import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -51,12 +50,12 @@ public class ScalarTypeZonedDateTime extends ScalarTypeBaseDateTime 0) { + return key + padding.substring(0, addChars); + } + return key; + } + + private byte[] getKeyBytes(String skey) { + + try { + return skey.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + private IvParameterSpec getIvParameterSpec(String initialVector) { + return new IvParameterSpec(initialVector.getBytes()); + } + + public byte[] decrypt(byte[] data, EncryptKey encryptKey) { + + if (data == null) { + return null; } - private String paddKey(EncryptKey encryptKey) { + String key = paddKey(encryptKey); - String key = encryptKey.getStringValue(); - int addChars = 16 - key.length(); - if (addChars < 0) { - return key.substring(0, 16); - } else if (addChars > 0) { - return key + padding.substring(0, addChars); - } - return key; + try { + + byte[] keyBytes = getKeyBytes(key); + IvParameterSpec iv = getIvParameterSpec(key); + + SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES"); + Cipher c = Cipher.getInstance(AES_CIPHER); + + c.init(Cipher.DECRYPT_MODE, sks, iv); + + return c.doFinal(data); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public byte[] encrypt(byte[] data, EncryptKey encryptKey) { + + if (data == null) { + return null; } - private byte[] getKeyBytes(String skey) { + String key = paddKey(encryptKey); - try { - return skey.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + try { + byte[] keyBytes = getKeyBytes(key); + IvParameterSpec iv = getIvParameterSpec(key); + + SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES"); + Cipher c = Cipher.getInstance(AES_CIPHER); + + c.init(Cipher.ENCRYPT_MODE, sks, iv); + + return c.doFinal(data); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public String decryptString(byte[] data, EncryptKey key) { + if (data == null) { + return null; } - private IvParameterSpec getIvParameterSpec(String initialVector) { - return new IvParameterSpec(initialVector.getBytes()); + byte[] bytes = decrypt(data, key); + try { + return new String(bytes, "UTF-8"); + + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); } + } - public byte[] decrypt(byte[] data, EncryptKey encryptKey) { + public byte[] encryptString(String valueFormatValue, EncryptKey key) { - if (data == null) { - return null; - } - - String key = paddKey(encryptKey); - - try { - - byte[] keyBytes = getKeyBytes(key); - IvParameterSpec iv = getIvParameterSpec(key); - - SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES"); - Cipher c = Cipher.getInstance(AES_CIPHER); - - c.init(Cipher.DECRYPT_MODE, sks, iv); - - return c.doFinal(data); - - } catch (Exception e) { - throw new RuntimeException(e); - } + if (valueFormatValue == null) { + return null; } + try { + byte[] d = valueFormatValue.getBytes("UTF-8"); + return encrypt(d, key); - public byte[] encrypt(byte[] data, EncryptKey encryptKey) { - - if (data == null) { - return null; - } - - String key = paddKey(encryptKey); - - try { - byte[] keyBytes = getKeyBytes(key); - IvParameterSpec iv = getIvParameterSpec(key); - - SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES"); - Cipher c = Cipher.getInstance(AES_CIPHER); - - c.init(Cipher.ENCRYPT_MODE, sks, iv); - - return c.doFinal(data); - - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - public String decryptString(byte[] data, EncryptKey key) { - if (data == null) { - return null; - } - - byte[] bytes = decrypt(data, key); - try { - return new String(bytes, "UTF-8"); - - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - - public byte[] encryptString(String valueFormatValue, EncryptKey key) { - - if (valueFormatValue == null) { - return null; - } - try { - byte[] d = valueFormatValue.getBytes("UTF-8"); - return encrypt(d, key); - - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); } + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/TypeReflectHelper.java b/src/main/java/com/avaje/ebeaninternal/server/type/TypeReflectHelper.java index 27a3798c1..90bdab8c6 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/TypeReflectHelper.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/TypeReflectHelper.java @@ -5,39 +5,39 @@ import java.lang.reflect.Type; public class TypeReflectHelper { - public static Class[] getParams(Class cls, Class matchRawType) { + public static Class[] getParams(Class cls, Class matchRawType) { - Type[] types = getParamType(cls, matchRawType); - Class[] result = new Class[types.length]; - for (int i = 0; i < result.length; i++) { - result[i] = getClass(types[i]); - } - return result; + Type[] types = getParamType(cls, matchRawType); + Class[] result = new Class[types.length]; + for (int i = 0; i < result.length; i++) { + result[i] = getClass(types[i]); } - - public static Class getClass(Type type){ - - if (type instanceof ParameterizedType){ - return getClass(((ParameterizedType)type).getRawType()); - } - - return (Class)type; + return result; + } + + public static Class getClass(Type type) { + + if (type instanceof ParameterizedType) { + return getClass(((ParameterizedType) type).getRawType()); } - - private static Type[] getParamType(Class cls, Class matchRawType) { - - Type[] gis = cls.getGenericInterfaces(); - for (int i = 0; i < gis.length; i++) { - Type type = gis[i]; - if (type instanceof ParameterizedType) { - ParameterizedType paramType = (ParameterizedType) type; - Type rawType = paramType.getRawType(); - if (rawType.equals(matchRawType)) { - - return paramType.getActualTypeArguments(); - } - } + + return (Class) type; + } + + private static Type[] getParamType(Class cls, Class matchRawType) { + + Type[] gis = cls.getGenericInterfaces(); + for (int i = 0; i < gis.length; i++) { + Type type = gis[i]; + if (type instanceof ParameterizedType) { + ParameterizedType paramType = (ParameterizedType) type; + Type rawType = paramType.getRawType(); + if (rawType.equals(matchRawType)) { + + return paramType.getActualTypeArguments(); } - return null; + } } + return null; + } } diff --git a/src/test/java/com/avaje/ebeaninternal/server/type/ConvertInetAddressesTest.java b/src/test/java/com/avaje/ebeaninternal/server/type/ConvertInetAddressesTest.java new file mode 100644 index 000000000..444ef2979 --- /dev/null +++ b/src/test/java/com/avaje/ebeaninternal/server/type/ConvertInetAddressesTest.java @@ -0,0 +1,31 @@ +package com.avaje.ebeaninternal.server.type; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by rob on 14/05/15. + */ +public class ConvertInetAddressesTest { + + @Test + public void testForString() throws Exception { + + } + + @Test + public void testIsInetAddress() throws Exception { + + } + + @Test + public void testToUriString() throws Exception { + + } + + @Test + public void testForUriString() throws Exception { + + } +} \ No newline at end of file diff --git a/src/test/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactoryTest.java b/src/test/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactoryTest.java new file mode 100644 index 000000000..08ab0ec08 --- /dev/null +++ b/src/test/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactoryTest.java @@ -0,0 +1,16 @@ +package com.avaje.ebeaninternal.server.type; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by rob on 14/05/15. + */ +public class DefaultTypeFactoryTest { + + @Test + public void testCreateBoolean() throws Exception { + + } +} \ No newline at end of file