From 3b0b3d7eb0187f8c0e3da8fd618b1dbb781b8bf7 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Mon, 17 Aug 2020 21:10:29 -0700 Subject: [PATCH] DirtyData type automatically marks data as dirty when modified --- .../datatypes/dirtydata/DirtyData.java | 38 +++++ .../datatypes/dirtydata/DirtyDataMap.java | 148 ++++++++++++++++++ .../mutableprimitives/MutableBoolean.java | 17 ++ .../mutableprimitives/MutableByte.java | 19 +++ .../mutableprimitives/MutableChar.java | 19 +++ .../mutableprimitives/MutableDouble.java | 19 +++ .../mutableprimitives/MutableFloat.java | 19 +++ .../mutableprimitives/MutableInteger.java | 17 ++ .../mutableprimitives/MutableLong.java | 19 +++ .../mutableprimitives/MutableShort.java | 19 +++ .../mutableprimitives/MutableString.java | 21 +++ 11 files changed, 355 insertions(+) create mode 100644 src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyData.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyDataMap.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableBoolean.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableByte.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableChar.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableDouble.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableFloat.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableInteger.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableLong.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableShort.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableString.java diff --git a/src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyData.java b/src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyData.java new file mode 100644 index 000000000..37cb7098e --- /dev/null +++ b/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 { + + 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); + } +} diff --git a/src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyDataMap.java b/src/main/java/com/gmail/nossr50/datatypes/dirtydata/DirtyDataMap.java new file mode 100644 index 000000000..77c494b7e --- /dev/null +++ b/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 { + + private final @NotNull MutableBoolean dirtyFlag; //Can be pointed at a reference + private @NotNull Map dataMap; + + public DirtyDataMap(@NotNull Map 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 dataMap) { + this.dataMap = dataMap; + setDirty(); + } + + public @NotNull Map 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 m) { + setDirty(); + dataMap.putAll(m); + } + + public void clear() { + setDirty(); + dataMap.clear(); + } + + public Set keySet() { + setDirty(); + return dataMap.keySet(); + } + + public Collection values() { + setDirty(); + return dataMap.values(); + } + + public Set> entrySet() { + setDirty(); + return dataMap.entrySet(); + } + + public V getOrDefault(K key, V defaultValue) { + return dataMap.getOrDefault(key, defaultValue); + } + + public void forEach(BiConsumer action) { + setDirty(); + dataMap.forEach(action); + } + + public void replaceAll(BiFunction 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 mappingFunction) { + return dataMap.computeIfAbsent(key, mappingFunction); + } + + public V computeIfPresent(K key, @NotNull BiFunction remappingFunction) { + return dataMap.computeIfPresent(key, remappingFunction); + } + + public V compute(K key, @NotNull BiFunction remappingFunction) { + return dataMap.compute(key, remappingFunction); + } + + public V merge(K key, @NotNull V value, @NotNull BiFunction remappingFunction) { + setDirty(); + return dataMap.merge(key, value, remappingFunction); + } +} diff --git a/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableBoolean.java b/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableBoolean.java new file mode 100644 index 000000000..de4cd6804 --- /dev/null +++ b/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; + } +} diff --git a/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableByte.java b/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableByte.java new file mode 100644 index 000000000..ea823957e --- /dev/null +++ b/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; + } + +} \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableChar.java b/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableChar.java new file mode 100644 index 000000000..83531fafe --- /dev/null +++ b/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; + } + +} \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableDouble.java b/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableDouble.java new file mode 100644 index 000000000..cb911869c --- /dev/null +++ b/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; + } + +} \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableFloat.java b/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableFloat.java new file mode 100644 index 000000000..6d4e8a750 --- /dev/null +++ b/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; + } + +} \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableInteger.java b/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableInteger.java new file mode 100644 index 000000000..baab364a4 --- /dev/null +++ b/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; + } +} diff --git a/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableLong.java b/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableLong.java new file mode 100644 index 000000000..f02e0f682 --- /dev/null +++ b/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; + } + +} diff --git a/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableShort.java b/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableShort.java new file mode 100644 index 000000000..0213956c8 --- /dev/null +++ b/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; + } + +} \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableString.java b/src/main/java/com/gmail/nossr50/datatypes/mutableprimitives/MutableString.java new file mode 100644 index 000000000..9f8ca6b95 --- /dev/null +++ b/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; + } + +}