mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor move createMutableHash() method from ScalarType to BeanProperty / BeanPropertyJsonMapper
This commit is contained in:
@@ -819,12 +819,12 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty {
|
||||
public Object parse(String value) {
|
||||
return scalarType.parse(value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* creates a mutableHash for the given JSON value.
|
||||
*/
|
||||
public MutableHash createMutableHash(String json) {
|
||||
return scalarType.createMutableHash(json);
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+65
-1
@@ -4,11 +4,14 @@ import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
import io.ebean.bean.MutableHash;
|
||||
import io.ebean.core.type.DataReader;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebean.text.TextException;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
||||
import io.ebeaninternal.server.util.Md5;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class BeanPropertyJsonMapper extends BeanProperty {
|
||||
|
||||
@@ -16,6 +19,15 @@ public class BeanPropertyJsonMapper extends BeanProperty {
|
||||
super(desc, deployProp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutableHash createMutableHash(String json) {
|
||||
if (false) { // TODO should we make that configurable?
|
||||
return new Md5MutableHash(json);
|
||||
} else {
|
||||
return new JsonMutableHash(scalarType, json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the mutable value is considered dirty.
|
||||
* This is only used for 'mutable' scalar types like hstore etc.
|
||||
@@ -40,7 +52,7 @@ public class BeanPropertyJsonMapper extends BeanProperty {
|
||||
setValue(bean, value);
|
||||
String json = reader.popJson();
|
||||
if (json != null) {
|
||||
final MutableHash hash = scalarType.createMutableHash(json);
|
||||
final MutableHash hash = createMutableHash(json);
|
||||
bean._ebean_getIntercept().mutableHash(propertyIndex, hash);
|
||||
}
|
||||
}
|
||||
@@ -51,4 +63,56 @@ public class BeanPropertyJsonMapper extends BeanProperty {
|
||||
throw new PersistenceException("Error readSet on " + descriptor + "." + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Md5MutableHash implements MutableHash {
|
||||
|
||||
private final String md5;
|
||||
|
||||
Md5MutableHash(String json) {
|
||||
md5 = Md5.hash(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqualToObject(Object obj) {
|
||||
return true; // we cannot determine differences...
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqualToJson(String json) {
|
||||
return Md5.hash(json).equals(md5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get() {
|
||||
return null; // cannot create object from json
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class JsonMutableHash implements MutableHash {
|
||||
|
||||
private final String originalJson;
|
||||
private ScalarType<?> parent;
|
||||
|
||||
JsonMutableHash(ScalarType<?> parent, String json) {
|
||||
this.parent = parent;
|
||||
originalJson = json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqualToObject(Object obj) {
|
||||
return isEqualToJson(parent.format(obj));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqualToJson(String json) {
|
||||
return Objects.equals(originalJson, json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get() {
|
||||
return parent.parse(originalJson);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,59 +51,7 @@ class ScalarTypeJsonObjectMapper {
|
||||
}
|
||||
return new GenericObject(jsonManager, field, dbType, type);
|
||||
}
|
||||
|
||||
private static class Md5MutableHash implements MutableHash {
|
||||
|
||||
private final String md5;
|
||||
|
||||
Md5MutableHash(String json) {
|
||||
md5 = Md5.hash(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqualToObject(Object obj) {
|
||||
return true; // we cannot determine differences...
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqualToJson(String json) {
|
||||
return Md5.hash(json).equals(md5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get() {
|
||||
return null; // cannot create object from json
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class JsonMutableHash implements MutableHash {
|
||||
|
||||
private final String originalJson;
|
||||
private ScalarType<?> parent;
|
||||
|
||||
JsonMutableHash(ScalarType<?> parent, String json) {
|
||||
this.parent = parent;
|
||||
originalJson = json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqualToObject(Object obj) {
|
||||
return isEqualToJson(parent.format(obj));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqualToJson(String json) {
|
||||
return Objects.equals(originalJson, json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get() {
|
||||
return parent.parse(originalJson);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps any type (Object) using Jackson ObjectMapper.
|
||||
*/
|
||||
@@ -122,19 +70,6 @@ class ScalarTypeJsonObjectMapper {
|
||||
public String jsonMapper(Object value) {
|
||||
return formatValue(value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public MutableHash createMutableHash(String json) {
|
||||
if (false) { // TODO should we make that configurable?
|
||||
return new Md5MutableHash(json);
|
||||
} else {
|
||||
return new JsonMutableHash(this, json);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(DataReader reader) throws SQLException {
|
||||
|
||||
Reference in New Issue
Block a user