From ef0b9d23c3acb0adf42e3a7945c6a587be3125fc Mon Sep 17 00:00:00 2001 From: Speiger Date: Sun, 19 Sep 2021 22:31:04 +0200 Subject: [PATCH] Added Pairs --- .../speiger/src/builder/GlobalVariables.java | 5 + .../builder/PrimitiveCollectionsBuilder.java | 3 + .../templates/misc/pairs/Pair.template | 157 ++++++++++++++++++ .../misc/pairs/impl/ImmutablePair.template | 61 +++++++ .../misc/pairs/impl/MutablePair.template | 63 +++++++ 5 files changed, 289 insertions(+) create mode 100644 src/builder/resources/speiger/assets/collections/templates/misc/pairs/Pair.template create mode 100644 src/builder/resources/speiger/assets/collections/templates/misc/pairs/impl/ImmutablePair.template create mode 100644 src/builder/resources/speiger/assets/collections/templates/misc/pairs/impl/MutablePair.template diff --git a/src/builder/java/speiger/src/builder/GlobalVariables.java b/src/builder/java/speiger/src/builder/GlobalVariables.java index b820921a..37b7bed7 100644 --- a/src/builder/java/speiger/src/builder/GlobalVariables.java +++ b/src/builder/java/speiger/src/builder/GlobalVariables.java @@ -175,6 +175,8 @@ public class GlobalVariables addFunctionValueMappers("ENUM_MAP", valueType.isObject() ? "Enum2ObjectMap" : "Enum2%sMap"); addBiClassMapper("HASH_MAP", "OpenHashMap", "2"); addBiClassMapper("ARRAY_MAP", "ArrayMap", "2"); + addBiClassMapper("IMMUTABLE_PAIR", "ImmutablePair", ""); + addBiClassMapper("MUTABLE_PAIR", "MutablePair", ""); addClassMapper("RB_TREE_SET", "RBTreeSet"); addClassMapper("AVL_TREE_SET", "AVLTreeSet"); addClassMapper("ARRAY_SET", "ArraySet"); @@ -214,6 +216,7 @@ public class GlobalVariables addBiClassMapper("SORTED_MAP", "SortedMap", "2"); addBiClassMapper("MAP", "Map", "2"); addClassMapper("NAVIGABLE_SET", "NavigableSet"); + addBiClassMapper("PAIR", "Pair", ""); addClassMapper("PRIORITY_QUEUE", "PriorityQueue"); addClassMapper("PRIORITY_DEQUEUE", "PriorityDequeue"); addClassMapper("PREDICATE", "2BooleanFunction"); @@ -266,6 +269,8 @@ public class GlobalVariables addFunctionValueMappers("LAST_ENTRY_VALUE", "last%sValue"); addFunctionMappers("ENTRY_KEY", "get%sKey"); addFunctionValueMappers("ENTRY_VALUE", "get%sValue"); + addFunctionMappers("KEY_ENTRY", "set%sKey"); + addFunctionValueMappers("VALUE_ENTRY", "set%sValue"); addFunctionMapper("GET_KEY", "get"); if(type.isObject()) addFunctionValueMapper("GET_VALUE", valueType.isObject() ? "getObject" : "get"); else addSimpleMapper("GET_VALUE", "get"); diff --git a/src/builder/java/speiger/src/builder/PrimitiveCollectionsBuilder.java b/src/builder/java/speiger/src/builder/PrimitiveCollectionsBuilder.java index 2ebc2276..59c56762 100644 --- a/src/builder/java/speiger/src/builder/PrimitiveCollectionsBuilder.java +++ b/src/builder/java/speiger/src/builder/PrimitiveCollectionsBuilder.java @@ -70,6 +70,9 @@ public class PrimitiveCollectionsBuilder extends TemplateProcessor enumRequired.add("LinkedEnumMap"); biRequired.put("BiConsumer", ""); biRequired.put("UnaryOperator", ""); + biRequired.put("Pair", ""); + biRequired.put("MutablePair", ""); + biRequired.put("ImmutablePair", ""); addBiClass("Function", "Maps", "Map", "SortedMap", "NavigableMap", "AbstractMap", "ImmutableOpenHashMap", "OpenHashMap", "LinkedOpenHashMap", "OpenCustomHashMap", "LinkedOpenCustomHashMap", "ArrayMap", "RBTreeMap", "AVLTreeMap"); nameRemapper.put("BiConsumer", "%sConsumer"); nameRemapper.put("IArray", "I%sArray"); diff --git a/src/builder/resources/speiger/assets/collections/templates/misc/pairs/Pair.template b/src/builder/resources/speiger/assets/collections/templates/misc/pairs/Pair.template new file mode 100644 index 00000000..89c439fa --- /dev/null +++ b/src/builder/resources/speiger/assets/collections/templates/misc/pairs/Pair.template @@ -0,0 +1,157 @@ +package speiger.src.collections.PACKAGE.misc.pairs; + +import speiger.src.collections.PACKAGE.misc.pairs.impl.IMMUTABLE_PAIR; +import speiger.src.collections.PACKAGE.misc.pairs.impl.MUTABLE_PAIR; +/** + * Key Value Pair Interface that allows to reduce boxing/unboxing. + * @Type(T) + * @ValueType(V) + */ +public interface PAIR KEY_VALUE_GENERIC_TYPE +{ + /** + * Empty Reference for Immutable Pairs + */ + public static final PAIR NO_KV_GENERIC_TYPE EMPTY = new IMMUTABLE_PAIRKV_BRACES(); + + /** + * @Type(T) + * @ValueType(V) + * @return empty Immutable Pair + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE of() { +#if TYPE_OBJECT || VALUE_OBJECT + return (PAIR KEY_VALUE_GENERIC_TYPE)EMPTY; +#else + return EMPTY; +#endif + } + + /** + * @param key the key that should be in the pair + * @Type(T) + * @ValueType(V) + * @return Immutable Pair of Key + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE ofKey(KEY_TYPE key) { + return new IMMUTABLE_PAIRKV_BRACES(key, EMPTY_VALUE); + } + + /** + * @param value the value that should be in the pair + * @Type(T) + * @ValueType(V) + * @return Immutable Pair of Value + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE ofValue(VALUE_TYPE value) { + return new IMMUTABLE_PAIRKV_BRACES(EMPTY_KEY_VALUE, value); + } + + /** + * @param key the key that should be in the pair + * @param value the value that should be in the pair + * @Type(T) + * @ValueType(V) + * @return Immutable Pair of key and value + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE of(KEY_TYPE key, VALUE_TYPE value) { + return new IMMUTABLE_PAIRKV_BRACES(key, value); + } + + /** + * @param pair the Pair that should be immutably copied + * @Type(T) + * @ValueType(V) + * @return a Immutable Copy of the Provided Pair + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE of(PAIR KEY_VALUE_GENERIC_TYPE pair) { + return new IMMUTABLE_PAIRKV_BRACES(pair.ENTRY_KEY(), pair.ENTRY_VALUE()); + } + + /** + * @Type(T) + * @ValueType(V) + * @return empty Mutable Pair + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE mutable() { + return new MUTABLE_PAIRKV_BRACES(); + } + + /** + * @param key the key that should be in the pair + * @Type(T) + * @ValueType(V) + * @return Mutable Pair of key + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE mutableKey(KEY_TYPE key) { + return new MUTABLE_PAIRKV_BRACES(key, EMPTY_VALUE); + } + + /** + * @param value the value that should be in the pair + * @Type(T) + * @ValueType(V) + * @return Mutable Pair of value + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE mutableValue(VALUE_TYPE value) { + return new MUTABLE_PAIRKV_BRACES(EMPTY_KEY_VALUE, value); + } + + /** + * @param key the key that should be in the pair + * @param value the value that should be in the pair + * @Type(T) + * @ValueType(V) + * @return Mutable Pair of key and value + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE mutable(KEY_TYPE key, VALUE_TYPE value) { + return new MUTABLE_PAIRKV_BRACES(key, value); + } + + /** + * @param pair the Pair that should be copied + * @Type(T) + * @ValueType(V) + * @return a Mutable Copy of the Provided Pair + */ + public static GENERIC_KEY_VALUE_BRACES PAIR KEY_VALUE_GENERIC_TYPE mutable(PAIR KEY_VALUE_GENERIC_TYPE pair) { + return new MUTABLE_PAIRKV_BRACES(pair.ENTRY_KEY(), pair.ENTRY_VALUE()); + } + + /** + * Sets the Key of the Pair. + * @param key the key that should be set. + * @return self or a new Pair instance with the new key. (Map.Entry may throw error) + */ + public PAIR KEY_VALUE_GENERIC_TYPE KEY_ENTRY(KEY_TYPE key); + /** + * @return the Key of the Pair + */ + public KEY_TYPE ENTRY_KEY(); + + /** + * Sets the Value of the Pair. + * @param value the value that should be set. + * @return self or a new Pair instance with the new value. (Map.Entry may throw error) + */ + public PAIR KEY_VALUE_GENERIC_TYPE VALUE_ENTRY(VALUE_TYPE value); + + /** + * @return the Value of the Pair + */ + public VALUE_TYPE ENTRY_VALUE(); + + /** + * Sets key and value of the Pair + * @param key the key that should be set. + * @param value the value that should be set. + * @return self or a new Pair instance with the new key and value. (Map.Entry may throw error) + */ + public PAIR KEY_VALUE_GENERIC_TYPE set(KEY_TYPE key, VALUE_TYPE value); + + /** + * Clones the Pair if it is mutable. + * @return a New Mutable Instance if it is mutable + */ + public PAIR KEY_VALUE_GENERIC_TYPE shallowCopy(); +} \ No newline at end of file diff --git a/src/builder/resources/speiger/assets/collections/templates/misc/pairs/impl/ImmutablePair.template b/src/builder/resources/speiger/assets/collections/templates/misc/pairs/impl/ImmutablePair.template new file mode 100644 index 00000000..0f4a4fc2 --- /dev/null +++ b/src/builder/resources/speiger/assets/collections/templates/misc/pairs/impl/ImmutablePair.template @@ -0,0 +1,61 @@ +package speiger.src.collections.PACKAGE.misc.pairs.impl; + +import speiger.src.collections.PACKAGE.misc.pairs.PAIR; + +/** + * Mutable Pair Implementation that + * @Type(T) + * @ValueType(V) + */ +public class IMMUTABLE_PAIR KEY_VALUE_GENERIC_TYPE implements PAIR KEY_VALUE_GENERIC_TYPE +{ + protected final KEY_TYPE key; + protected final VALUE_TYPE value; + + /** + * Default Constructor + */ + public IMMUTABLE_PAIR() { + this(EMPTY_KEY_VALUE, EMPTY_VALUE); + } + + /** + * Key/Value Constructur + * @param key the key of the Pair + * @param value the value of the Pair + */ + public IMMUTABLE_PAIR(KEY_TYPE key, VALUE_TYPE value) { + this.key = key; + this.value = value; + } + + @Override + public PAIR KEY_VALUE_GENERIC_TYPE KEY_ENTRY(KEY_TYPE key) { + return new IMMUTABLE_PAIRKV_BRACES(key, value); + } + + @Override + public KEY_TYPE ENTRY_KEY() { + return key; + } + + @Override + public PAIR KEY_VALUE_GENERIC_TYPE VALUE_ENTRY(VALUE_TYPE value) { + return new IMMUTABLE_PAIRKV_BRACES(key, value); + } + + @Override + public VALUE_TYPE ENTRY_VALUE() { + return value; + } + + @Override + public PAIR KEY_VALUE_GENERIC_TYPE set(KEY_TYPE key, VALUE_TYPE value) { + return new IMMUTABLE_PAIRKV_BRACES(key, value); + } + + @Override + public PAIR KEY_VALUE_GENERIC_TYPE shallowCopy() { + return this; + } +} \ No newline at end of file diff --git a/src/builder/resources/speiger/assets/collections/templates/misc/pairs/impl/MutablePair.template b/src/builder/resources/speiger/assets/collections/templates/misc/pairs/impl/MutablePair.template new file mode 100644 index 00000000..2c488375 --- /dev/null +++ b/src/builder/resources/speiger/assets/collections/templates/misc/pairs/impl/MutablePair.template @@ -0,0 +1,63 @@ +package speiger.src.collections.PACKAGE.misc.pairs.impl; + +import speiger.src.collections.PACKAGE.misc.pairs.PAIR; + +/** + * Mutable Pair Implementation that + * @Type(T) + * @ValueType(V) + */ +public class MUTABLE_PAIR KEY_VALUE_GENERIC_TYPE implements PAIR KEY_VALUE_GENERIC_TYPE +{ + protected KEY_TYPE key; + protected VALUE_TYPE value; + + /** + * Default Constructor + */ + public MUTABLE_PAIR() {} + + /** + * Key/Value Constructur + * @param key the key of the Pair + * @param value the value of the Pair + */ + public MUTABLE_PAIR(KEY_TYPE key, VALUE_TYPE value) { + this.key = key; + this.value = value; + } + + @Override + public PAIR KEY_VALUE_GENERIC_TYPE KEY_ENTRY(KEY_TYPE key) { + this.key = key; + return this; + } + + @Override + public KEY_TYPE ENTRY_KEY() { + return key; + } + + @Override + public PAIR KEY_VALUE_GENERIC_TYPE VALUE_ENTRY(VALUE_TYPE value) { + this.value = value; + return this; + } + + @Override + public VALUE_TYPE ENTRY_VALUE() { + return value; + } + + @Override + public PAIR KEY_VALUE_GENERIC_TYPE set(KEY_TYPE key, VALUE_TYPE value) { + this.key = key; + this.value = value; + return this; + } + + @Override + public PAIR KEY_VALUE_GENERIC_TYPE shallowCopy() { + return PAIR.mutable(key, value); + } +} \ No newline at end of file