Mutation only needs to be different and can occur even when current is null.

This commit is contained in:
Greg Burd 2018-01-25 12:57:37 -05:00
parent 96a8476fd8
commit 8b9d582fa5

View file

@ -18,19 +18,19 @@ import org.apache.commons.lang3.SerializationUtils;
public abstract class AbstractEntityDraft<E> implements Drafted<E> { public abstract class AbstractEntityDraft<E> implements Drafted<E> {
private final MapExportable entity; private final MapExportable entity;
private final Map<String, Object> backingMap = new HashMap<String, Object>(); private final Map<String, Object> valuesMap;
private final Set<String> read; private final Set<String> readSet;
private final Map<String, Object> entityMap; private final Map<String, Object> mutationsMap = new HashMap<String, Object>();
public AbstractEntityDraft(MapExportable entity) { public AbstractEntityDraft(MapExportable entity) {
this.entity = entity; this.entity = entity;
// Entities can mutate their map. // Entities can mutate their map.
if (entity != null) { if (entity != null) {
this.entityMap = entity.toMap(true); this.valuesMap = entity.toMap(true);
this.read = entity.toReadSet(); this.readSet = entity.toReadSet();
} else { } else {
this.entityMap = new HashMap<String, Object>(); this.valuesMap = new HashMap<String, Object>();
this.read = new HashSet<String>(); this.readSet = new HashSet<String>();
} }
} }
@ -47,11 +47,11 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T get(String key, Class<?> returnType) { public <T> T get(String key, Class<?> returnType) {
read.add(key); readSet.add(key);
T value = (T) backingMap.get(key); T value = (T) mutationsMap.get(key);
if (value == null) { if (value == null) {
value = (T) entityMap.get(key); value = (T) valuesMap.get(key);
if (value == null) { if (value == null) {
if (Primitives.allPrimitiveTypes().contains(returnType)) { if (Primitives.allPrimitiveTypes().contains(returnType)) {
@ -64,7 +64,7 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
return (T) type.getDefaultValue(); return (T) type.getDefaultValue();
} }
} else { } else {
// Collections fetched from the entityMap // Collections fetched from the valuesMap
if (value instanceof Collection) { if (value instanceof Collection) {
value = (T) SerializationUtils.<Serializable>clone((Serializable) value); value = (T) SerializationUtils.<Serializable>clone((Serializable) value);
} }
@ -84,7 +84,7 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
return null; return null;
} }
backingMap.put(key, value); mutationsMap.put(key, value);
return value; return value;
} }
@ -93,12 +93,12 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
return null; return null;
} }
backingMap.put(key, value); mutationsMap.put(key, value);
return value; return value;
} }
public void put(String key, Object value) { public void put(String key, Object value) {
backingMap.put(key, value); mutationsMap.put(key, value);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -112,12 +112,12 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
if (value != null) { if (value != null) {
if (entity != null) { if (entity != null) {
T currentValue = this.<T>fetch(key); T currentValue = this.<T>fetch(key);
if (currentValue != null && !value.equals(currentValue)) { if (!value.equals(currentValue)) {
backingMap.put(key, value); mutationsMap.put(key, value);
return value; return value;
} }
} else { } else {
backingMap.put(key, value); mutationsMap.put(key, value);
} }
} }
return null; return null;
@ -133,8 +133,8 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
public Object unset(String key) { public Object unset(String key) {
if (key != null) { if (key != null) {
Object value = backingMap.get(key); Object value = mutationsMap.get(key);
backingMap.put(key, null); mutationsMap.put(key, null);
return value; return value;
} }
return null; return null;
@ -145,9 +145,9 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
} }
private <T> T fetch(String key) { private <T> T fetch(String key) {
T value = (T) backingMap.get(key); T value = (T) mutationsMap.get(key);
if (value == null) { if (value == null) {
value = (T) entityMap.get(key); value = (T) valuesMap.get(key);
} }
return value; return value;
} }
@ -166,7 +166,7 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
@Override @Override
public Map<String, Object> toMap() { public Map<String, Object> toMap() {
return toMap(entityMap); return toMap(valuesMap);
} }
public Map<String, Object> toMap(Map<String, Object> entityMap) { public Map<String, Object> toMap(Map<String, Object> entityMap) {
@ -177,26 +177,26 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
combined.put(e.getKey(), e.getValue()); combined.put(e.getKey(), e.getValue());
} }
} else { } else {
combined = new HashMap<String, Object>(backingMap.size()); combined = new HashMap<String, Object>(mutationsMap.size());
} }
for (String key : mutated()) { for (String key : mutated()) {
combined.put(key, backingMap.get(key)); combined.put(key, mutationsMap.get(key));
} }
return combined; return combined;
} }
@Override @Override
public Set<String> mutated() { public Set<String> mutated() {
return backingMap.keySet(); return mutationsMap.keySet();
} }
@Override @Override
public Set<String> read() { public Set<String> read() {
return read; return readSet;
} }
@Override @Override
public String toString() { public String toString() {
return backingMap.toString(); return mutationsMap.toString();
} }
} }