Explorar o código

DirtyData type automatically marks data as dirty when modified

nossr50 %!s(int64=5) %!d(string=hai) anos
pai
achega
3b0b3d7eb0

+ 38 - 0
src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyData.java

@@ -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);
+    }
+}

+ 148 - 0
src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyDataMap.java

@@ -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);
+    }
+}

+ 17 - 0
src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableBoolean.java

@@ -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;
+    }
+}

+ 19 - 0
src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableByte.java

@@ -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;
+    }
+
+}

+ 19 - 0
src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableChar.java

@@ -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;
+    }
+
+}

+ 19 - 0
src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableDouble.java

@@ -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;
+    }
+
+}

+ 19 - 0
src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableFloat.java

@@ -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;
+    }
+
+}

+ 17 - 0
src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableInteger.java

@@ -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;
+    }
+}

+ 19 - 0
src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableLong.java

@@ -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;
+    }
+
+}

+ 19 - 0
src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableShort.java

@@ -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;
+    }
+
+}

+ 21 - 0
src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableString.java

@@ -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;
+    }
+
+}