DirtyData type automatically marks data as dirty when modified

This commit is contained in:
nossr50 2020-08-17 21:10:29 -07:00
parent 2ae70dddaa
commit 3b0b3d7eb0
11 changed files with 355 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package com.gmail.nossr50.datatypes.dirtydata;
import com.gmail.nossr50.datatypes.mutableprimitives.MutableBoolean;
import org.jetbrains.annotations.NotNull;
public class DirtyData<T> {
private final @NotNull MutableBoolean dirtyFlag; //Can be pointed at a reference
private @NotNull T data;
public DirtyData(@NotNull T data, @NotNull MutableBoolean referenceFlag) {
this.data = data;
this.dirtyFlag = referenceFlag;
}
public boolean isDirty() {
return dirtyFlag.getImmutableCopy();
}
public void setDirty(boolean newDirtyValue) {
dirtyFlag.setBoolean(newDirtyValue);
}
public @NotNull T getData() {
return data;
}
public T getData(boolean newDirty) {
setDirty(newDirty);
return data;
}
public void setData(@NotNull T data) {
this.data = data;
setDirty(true);
}
}

View File

@ -0,0 +1,148 @@
package com.gmail.nossr50.datatypes.dirtydata;
import com.gmail.nossr50.datatypes.mutableprimitives.MutableBoolean;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public class DirtyDataMap<K, V> {
private final @NotNull MutableBoolean dirtyFlag; //Can be pointed at a reference
private @NotNull Map<K, V> dataMap;
public DirtyDataMap(@NotNull Map<K, V> data, @NotNull MutableBoolean referenceFlag) {
this.dataMap = data;
this.dirtyFlag = referenceFlag;
}
public boolean isDirty() {
return dirtyFlag.getImmutableCopy();
}
private void setDirty() {
dirtyFlag.setBoolean(true);
}
public void setData(@NotNull Map<K, V> dataMap) {
this.dataMap = dataMap;
setDirty();
}
public @NotNull Map<K, V> getDataMap() {
setDirty();
return dataMap;
}
/* Map Interface Delegates */
public V get(K key) {
return dataMap.get(key);
}
public int size() {
return dataMap.size();
}
public boolean isEmpty() {
return dataMap.isEmpty();
}
public boolean containsKey(K key) {
return dataMap.containsKey(key);
}
public boolean containsValue(V value) {
return dataMap.containsValue(value);
}
public V put(K key, V value) {
setDirty();
return dataMap.put(key, value);
}
public V remove(K key) {
setDirty();
return dataMap.remove(key);
}
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
setDirty();
dataMap.putAll(m);
}
public void clear() {
setDirty();
dataMap.clear();
}
public Set<K> keySet() {
setDirty();
return dataMap.keySet();
}
public Collection<V> values() {
setDirty();
return dataMap.values();
}
public Set<Map.Entry<K, V>> entrySet() {
setDirty();
return dataMap.entrySet();
}
public V getOrDefault(K key, V defaultValue) {
return dataMap.getOrDefault(key, defaultValue);
}
public void forEach(BiConsumer<? super K, ? super V> action) {
setDirty();
dataMap.forEach(action);
}
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
setDirty();
dataMap.replaceAll(function);
}
public V putIfAbsent(K key, V value) {
setDirty();
return dataMap.putIfAbsent(key, value);
}
public boolean remove(K key, V value) {
setDirty();
return dataMap.remove(key, value);
}
public boolean replace(K key, V oldValue, V newValue) {
setDirty();
return dataMap.replace(key, oldValue, newValue);
}
public V replace(K key, V value) {
setDirty();
return dataMap.replace(key, value);
}
public V computeIfAbsent(K key, @NotNull Function<? super K, ? extends V> mappingFunction) {
return dataMap.computeIfAbsent(key, mappingFunction);
}
public V computeIfPresent(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return dataMap.computeIfPresent(key, remappingFunction);
}
public V compute(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return dataMap.compute(key, remappingFunction);
}
public V merge(K key, @NotNull V value, @NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
setDirty();
return dataMap.merge(key, value, remappingFunction);
}
}

View File

@ -0,0 +1,17 @@
package com.gmail.nossr50.datatypes.mutableprimitives;
public class MutableBoolean {
private boolean bool;
public MutableBoolean(boolean bool) {
this.bool = bool;
}
public boolean getImmutableCopy() {
return bool;
}
public void setBoolean(boolean bool) {
this.bool = bool;
}
}

View File

@ -0,0 +1,19 @@
package com.gmail.nossr50.datatypes.mutableprimitives;
public class MutableByte {
private byte byteValue;
public MutableByte(byte byteValue) {
this.byteValue = byteValue;
}
public byte getImmutableCopy() {
return byteValue;
}
public void setByte(byte byteValue) {
this.byteValue = byteValue;
}
}

View File

@ -0,0 +1,19 @@
package com.gmail.nossr50.datatypes.mutableprimitives;
public class MutableChar {
private char charValue;
public MutableChar(char charValue) {
this.charValue = charValue;
}
public char getImmutableCopy() {
return charValue;
}
public void setChar(char charValue) {
this.charValue = charValue;
}
}

View File

@ -0,0 +1,19 @@
package com.gmail.nossr50.datatypes.mutableprimitives;
public class MutableDouble {
private double doubleValue;
public MutableDouble(double doubleValue) {
this.doubleValue = doubleValue;
}
public double getImmutableCopy() {
return doubleValue;
}
public void setDouble(double doubleValue) {
this.doubleValue = doubleValue;
}
}

View File

@ -0,0 +1,19 @@
package com.gmail.nossr50.datatypes.mutableprimitives;
public class MutableFloat {
private float floatValue;
public MutableFloat(float floatValue) {
this.floatValue = floatValue;
}
public float getImmutableCopy() {
return floatValue;
}
public void setFloat(float floatValue) {
this.floatValue = floatValue;
}
}

View File

@ -0,0 +1,17 @@
package com.gmail.nossr50.datatypes.mutableprimitives;
public class MutableInteger {
private int integer;
public MutableInteger(int integer) {
this.integer = integer;
}
public int getImmutableCopy() {
return integer;
}
public void setInt(int integer) {
this.integer = integer;
}
}

View File

@ -0,0 +1,19 @@
package com.gmail.nossr50.datatypes.mutableprimitives;
public class MutableLong {
private long longValue;
public MutableLong(long longValue) {
this.longValue = longValue;
}
public long getImmutableCopy() {
return longValue;
}
public void setLong(long longValue) {
this.longValue = longValue;
}
}

View File

@ -0,0 +1,19 @@
package com.gmail.nossr50.datatypes.mutableprimitives;
public class MutableShort {
private short shortValue;
public MutableShort(short shortValue) {
this.shortValue = shortValue;
}
public short getImmutableCopy() {
return shortValue;
}
public void setShort(short shortValue) {
this.shortValue = shortValue;
}
}

View File

@ -0,0 +1,21 @@
package com.gmail.nossr50.datatypes.mutableprimitives;
import org.jetbrains.annotations.NotNull;
public class MutableString {
private @NotNull String string;
public MutableString(@NotNull String string) {
this.string = string;
}
public @NotNull String getImmutableCopy() {
return string;
}
public void setString(@NotNull String string) {
this.string = string;
}
}