Change mutate on drafts to be a bit faster.

This commit is contained in:
Greg Burd 2018-01-25 11:26:12 -05:00
parent f9b1563bdd
commit 11de7015c2

View file

@ -107,28 +107,24 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
return (T) mutate(this.<T>methodNameFor(getter), value); return (T) mutate(this.<T>methodNameFor(getter), value);
} }
public Object mutate(String key, Object value) { public <T> T mutate(String key, T value) {
Objects.requireNonNull(key); Objects.requireNonNull(key);
if (value == null) { if (value != null) {
return null;
}
if (entity != null) { if (entity != null) {
Map<String, Object> map = entity.toMap(); if (entityMap.containsKey(key)) {
T currentValue = this.<T>fetch(key);
if (map.containsKey(key) && !value.equals(map.get(key))) { if (currentValue != null && !value.equals(currentValue)) {
backingMap.put(key, value); backingMap.put(key, value);
return value; return value;
} }
}
return map.get(key);
} else { } else {
backingMap.put(key, value); backingMap.put(key, value);
return null;
} }
} }
return null;
}
private <T> String methodNameFor(Getter<T> getter) { private <T> String methodNameFor(Getter<T> getter) {
return MappingUtil.resolveMappingProperty(getter).getProperty().getPropertyName(); return MappingUtil.resolveMappingProperty(getter).getProperty().getPropertyName();
@ -153,7 +149,6 @@ 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) backingMap.get(key);
if (value == null) { if (value == null) {
value = (T) entityMap.get(key); value = (T) entityMap.get(key);
} }