package speiger.src.collections.PACKAGE.utils.maps; #if !TYPE_BOOLEAN import java.util.Map; import java.util.Objects; #if TYPE_OBJECT import java.util.Comparator; #else import java.util.function.BiConsumer; import java.util.function.BiFunction; #endif #endif import java.util.function.Consumer; #if !TYPE_OBJECT && !TYPE_BOOLEAN import java.util.function.Function; #endif import speiger.src.collections.objects.collections.ObjectIterable; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.ObjectSet; #if !TYPE_BOOLEAN import speiger.src.collections.objects.utils.ObjectSets; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; #endif import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; import speiger.src.collections.PACKAGE.functions.function.FUNCTION; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; #endif import speiger.src.collections.PACKAGE.maps.interfaces.MAP; #if !TYPE_BOOLEAN import speiger.src.collections.PACKAGE.maps.interfaces.NAVIGABLE_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP; import speiger.src.collections.PACKAGE.sets.NAVIGABLE_SET; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.sets.SET; import speiger.src.collections.PACKAGE.utils.SETS; #endif import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; #if !SAME_TYPE import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; #endif import speiger.src.collections.VALUE_PACKAGE.utils.VALUE_COLLECTIONS; #if !SAME_TYPE && !VALUE_OBJECT import speiger.src.collections.VALUE_PACKAGE.utils.VALUE_SETS; #endif #endif /** * A Helper class that provides you with Singleton/Empty/Synchronized/Unmodifyable Maps */ public class MAPS { #if !TYPE_BOOLEAN /** * Empty Map Variable */ public static final MAP NO_KV_GENERIC_TYPE EMPTY = new EmptyMapKV_BRACES(); #endif /** * Helper method that provides the fastIterator that recycles a single Entry to increase throughput. * @param map the map the fastIterator should be accessed from * @Type(T) * @ValueType(V) * @return either a normal iterator if it does not support this feature to a fastIterator */ public static GENERIC_KEY_VALUE_BRACES ObjectIterator fastIterator(MAP KEY_VALUE_GENERIC_TYPE map) { ObjectSet entries = map.ENTRY_SET(); return entries instanceof MAP.FastEntrySet ? ((MAP.FastEntrySet KEY_VALUE_GENERIC_TYPE)entries).fastIterator() : entries.iterator(); } /** * Helper method that provides the fastIterable that recycles a single Entry to increase throughput. * @param map the map the fastIterable should be accessed from * @Type(T) * @ValueType(V) * @return either a normal iterable if it does not support this feature to a fastIterable */ public static GENERIC_KEY_VALUE_BRACES ObjectIterable fastIterable(MAP KEY_VALUE_GENERIC_TYPE map) { ObjectSet entries = map.ENTRY_SET(); return map instanceof MAP.FastEntrySet ? new ObjectIterable(){ @Override public ObjectIterator iterator() { return ((MAP.FastEntrySet KEY_VALUE_GENERIC_TYPE)entries).fastIterator(); } @Override public void forEach(Consumer action) { ((MAP.FastEntrySet KEY_VALUE_GENERIC_TYPE)entries).fastForEach(action); } } : entries; } /** * A Helper function that provides a faster forEach iterator implementation that recycles the entry to increase throughput * @param map the map the fast forEach should be accessed from * @param action the action that should be performed on each entry * @Type(T) * @ValueType(V) * @note if the fast forEach is not supported will default to a normal forEach */ public static GENERIC_KEY_VALUE_BRACES void fastForEach(MAP KEY_VALUE_GENERIC_TYPE map, Consumer action) { ObjectSet entries = map.ENTRY_SET(); if(entries instanceof MAP.FastEntrySet) ((MAP.FastEntrySet KEY_VALUE_GENERIC_TYPE)entries).fastForEach(action); else entries.forEach(action); } #if !TYPE_BOOLEAN /** * Empty Map getter function that autocasts to the desired Key and Value * @Type(T) * @ValueType(V) * @return empty map of desired type */ public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE empty() { #if TYPE_OBJECT || VALUE_OBJECT return (MAP KEY_VALUE_GENERIC_TYPE)EMPTY; #else return EMPTY; #endif } /** * Helper function that creates a Helper wrapper to synchronize access into the map. * @param map the map that should be synchronized * @Type(T) * @ValueType(V) * @return a synchronized Map * @note if the inputted map is already synchronized then it will just return it instead * @note iterators do not support synchronization */ public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE synchronize(MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof SynchronizedMap ? map : new SynchronizedMapKV_BRACES(map); } /** * Helper function that creates a Helper wrapper to synchronize access with custom access control into the map. * @param map the map that should be synchronized * @param mutex the object that controls access * @Type(T) * @ValueType(V) * @return a synchronized Map * @note if the inputted map is already synchronized then it will just return it instead * @note iterators do not support synchronization */ public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE synchronize(MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { return map instanceof SynchronizedMap ? map : new SynchronizedMapKV_BRACES(map, mutex); } /** * Helper function that creates a Helper wrapper to synchronize access into the SortedMap. * @param map the SortedMap that should be synchronized * @Type(T) * @ValueType(V) * @return a synchronized SortedMap * @note if the inputted map is already synchronized then it will just return it instead * @note iterators do not support synchronization */ public static GENERIC_KEY_VALUE_BRACES SORTED_MAP KEY_VALUE_GENERIC_TYPE synchronize(SORTED_MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof SynchronizedSortedMap ? map : new SynchronizedSortedMapKV_BRACES(map); } /** * Helper function that creates a Helper wrapper to synchronize access with custom access control into the SortedMap. * @param map the SortedMap that should be synchronized * @param mutex the object that controls access * @Type(T) * @ValueType(V) * @return a synchronized SortedMap * @note if the inputted map is already synchronized then it will just return it instead * @note iterators do not support synchronization */ public static GENERIC_KEY_VALUE_BRACES SORTED_MAP KEY_VALUE_GENERIC_TYPE synchronize(SORTED_MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { return map instanceof SynchronizedSortedMap ? map : new SynchronizedSortedMapKV_BRACES(map, mutex); } /** * Helper function that creates a Helper wrapper to synchronize access into the NavigableMap. * @param map the NavigableMap that should be synchronized * @Type(T) * @ValueType(V) * @return a synchronized NavigableMap * @note if the inputted map is already synchronized then it will just return it instead * @note iterators do not support synchronization */ public static GENERIC_KEY_VALUE_BRACES NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE synchronize(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof SynchronizedNavigableMap ? map : new SynchronizedNavigableMapKV_BRACES(map); } /** * Helper function that creates a Helper wrapper to synchronize access with custom access control into the NavigableMap. * @param map the NavigableMap that should be synchronized * @param mutex the object that controls access * @Type(T) * @ValueType(V) * @return a synchronized NavigableMap * @note if the inputted map is already synchronized then it will just return it instead * @note iterators do not support synchronization */ public static GENERIC_KEY_VALUE_BRACES NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE synchronize(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { return map instanceof SynchronizedNavigableMap ? map : new SynchronizedNavigableMapKV_BRACES(map, mutex); } /** * A Helper function that creates a Helper wrapper to only allow Read Access into the Map * @param map the map that should be made Unmodifiable * @Type(T) * @ValueType(V) * @return a unmodifiable Map * @note if the inputted map is already unmodifiable then it will just return it instead */ public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE unmodifiable(MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof UnmodifyableMap ? map : new UnmodifyableMapKV_BRACES(map); } /** * A Helper function that creates a Helper wrapper to only allow Read Access into the SortedMap * @param map the SortedMap that should be made Unmodifiable * @Type(T) * @ValueType(V) * @return a unmodifiable SortedMap * @note if the inputted SortedMap is already unmodifiable then it will just return it instead */ public static GENERIC_KEY_VALUE_BRACES SORTED_MAP KEY_VALUE_GENERIC_TYPE unmodifiable(SORTED_MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof UnmodifyableSortedMap ? map : new UnmodifyableSortedMapKV_BRACES(map); } /** * A Helper function that creates a Helper wrapper to only allow Read Access into NavigableMap Map * @param map the NavigableMap that should be made Unmodifiable * @Type(T) * @ValueType(V) * @return a unmodifiable NavigableMap * @note if the inputted NavigableMap is already unmodifiable then it will just return it instead */ public static GENERIC_KEY_VALUE_BRACES NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE unmodifiable(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map) { return map instanceof UnmodifyableNavigableMap ? map : new UnmodifyableNavigableMapKV_BRACES(map); } /** * A Helper function that creates a Unmodifyable Entry * @param entry the Entry that should be made unmodifiable * @Type(T) * @ValueType(V) * @return a Unmodifyable Entry */ public static GENERIC_KEY_VALUE_BRACES MAP.Entry KEY_VALUE_GENERIC_TYPE unmodifiable(MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { return entry instanceof UnmodifyableEntry ? entry : new UnmodifyableEntryKV_BRACES(entry); } /** * A Helper function that creates a Unmodifyable Entry * @param entry the Entry that should be made unmodifiable * @Type(T) * @ValueType(V) * @return a Unmodifyable Entry */ public static GENERIC_KEY_VALUE_BRACES MAP.Entry KEY_VALUE_GENERIC_TYPE unmodifiable(Map.Entry entry) { return entry instanceof UnmodifyableEntry ? (UnmodifyableEntry KEY_VALUE_GENERIC_TYPE)entry : new UnmodifyableEntryKV_BRACES(entry); } /** * Creates a Singleton map from the provided values. * This reduces overhead that normal Map implementations have. * @param key the key that should be turned into a singleton * @param value the value that should be turned into a singleton * @Type(T) * @ValueType(V) * @return a unmodifiable Singleton map. */ public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE singleton(KEY_TYPE key, VALUE_TYPE value) { return new SingletonMapKV_BRACES(key, value); } /** * Singleton Map instance that is used in the helper method * @Type(T) * @ValueType(V) */ public static class SingletonMap KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE { final KEY_TYPE key; final VALUE_TYPE value; SET KEY_GENERIC_TYPE keySet; VALUE_COLLECTION VALUE_GENERIC_TYPE values; ObjectSet entrySet; SingletonMap(KEY_TYPE key, VALUE_TYPE value) { this.key = key; this.value = value; } @Override public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #if VALUE_PRIMITIVES @Override public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #endif @Override public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { throw new UnsupportedOperationException(); } #if !TYPE_OBJECT || !VALUE_OBJECT @Override public boolean remove(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #endif @Override public VALUE_TYPE GET_VALUE(KEY_TYPE key) { return EQUALS_KEY_TYPE(key, this.key) ? value : getDefaultReturnValue(); } #if !TYPE_OBJECT || !VALUE_OBJECT @Override public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { return EQUALS_KEY_TYPE(key, this.key) ? value : defaultValue; } #endif @Override public SET KEY_GENERIC_TYPE keySet() { if(keySet == null) keySet = SETS.singleton(key); return keySet; } @Override public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { if(values == null) values = VALUE_SETS.singleton(value); return values; } @Override public ObjectSet ENTRY_SET() { if(entrySet == null) entrySet = ObjectSets.singleton(new ABSTRACT_MAP.BasicEntryKV_BRACES(key, value)); return entrySet; } } /** * Empty Map impementation that is used for the emptyMap() function * @Type(T) * @ValueType(V) */ public static class EmptyMap KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE { @Override public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #if VALUE_PRIMITIVES @Override public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #endif @Override public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { throw new UnsupportedOperationException(); } #if !TYPE_OBJECT || !VALUE_OBJECT @Override public boolean remove(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #endif @Override public VALUE_TYPE GET_VALUE(KEY_TYPE key) { return getDefaultReturnValue(); } #if !TYPE_OBJECT || !VALUE_OBJECT @Override public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { return EMPTY_VALUE; } #endif @Override public SET KEY_GENERIC_TYPE keySet() { return SETS.empty(); } @Override public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { return VALUE_COLLECTIONS.empty(); } @Override public ObjectSet ENTRY_SET() { return ObjectSets.empty(); } } /** * The Unmodifyable Entry implementation for the helper function unmodifiableEntry() * @Type(T) * @ValueType(V) */ public static class UnmodifyableEntry KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP.BasicEntry KEY_VALUE_GENERIC_TYPE { UnmodifyableEntry(Map.Entry entry) { super(entry.getKey(), entry.getValue()); } UnmodifyableEntry(MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { super(entry.ENTRY_KEY(), entry.ENTRY_VALUE()); } @Override public void set(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } } /** * The Unmodifyable Navigable Map implementation that is sued for the unmodifyableMap function * @Type(T) * @ValueType(V) */ public static class UnmodifyableNavigableMap KEY_VALUE_GENERIC_TYPE extends UnmodifyableSortedMap KEY_VALUE_GENERIC_TYPE implements NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE { NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map; UnmodifyableNavigableMap(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map) { super(map); this.map = map; } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { return synchronize(map.descendingMap()); } @Override public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { return SETS.unmodifiable(map.navigableKeySet()); } @Override public NAVIGABLE_SET KEY_GENERIC_TYPE descendingKeySet() { return SETS.unmodifiable(map.descendingKeySet()); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { return unmodifiable(map.firstEntry()); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { return unmodifiable(map.lastEntry()); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { throw new UnsupportedOperationException(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { throw new UnsupportedOperationException(); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { return unmodifiable(map.subMap(fromKey, fromInclusive, toKey, toInclusive)); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { return unmodifiable(map.headMap(toKey, inclusive)); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { return unmodifiable(map.tailMap(fromKey, inclusive)); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { return unmodifiable(map.subMap(fromKey, toKey)); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { return unmodifiable(map.headMap(toKey)); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { return unmodifiable(map.tailMap(fromKey)); } #if !TYPE_OBJECT @Override public void setDefaultMaxValue(KEY_TYPE e) { throw new UnsupportedOperationException(); } @Override public KEY_TYPE getDefaultMaxValue() { return map.getDefaultMaxValue(); } @Override public void setDefaultMinValue(KEY_TYPE e) { throw new UnsupportedOperationException(); } @Override public KEY_TYPE getDefaultMinValue() { return map.getDefaultMinValue(); } #endif @Override public KEY_TYPE lowerKey(KEY_TYPE key) { return map.lowerKey(key); } @Override public KEY_TYPE higherKey(KEY_TYPE key) { return map.higherKey(key); } @Override public KEY_TYPE floorKey(KEY_TYPE key) { return map.floorKey(key); } @Override public KEY_TYPE ceilingKey(KEY_TYPE key) { return map.ceilingKey(key); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE key) { return unmodifiable(map.lowerEntry(key)); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE key) { return unmodifiable(map.higherEntry(key)); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { return unmodifiable(map.floorEntry(key)); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { return unmodifiable(map.ceilingEntry(key)); } } /** * The Unmodifyable Sorted Map implementation that is sued for the unmodifyableMap function * @Type(T) * @ValueType(V) */ public static class UnmodifyableSortedMap KEY_VALUE_GENERIC_TYPE extends UnmodifyableMap KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE { SORTED_MAP KEY_VALUE_GENERIC_TYPE map; UnmodifyableSortedMap(SORTED_MAP KEY_VALUE_GENERIC_TYPE map) { super(map); this.map = map; } @Override public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @Override public boolean moveToFirst(KEY_TYPE key) { throw new UnsupportedOperationException(); } @Override public boolean moveToLast(KEY_TYPE key) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { throw new UnsupportedOperationException(); } @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { return map.comparator(); } @Override public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { return unmodifiable(map.subMap(fromKey, toKey)); } @Override public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { return unmodifiable(map.headMap(toKey)); } @Override public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { return unmodifiable(map.tailMap(fromKey)); } @Override public KEY_TYPE FIRST_ENTRY_KEY() { return map.FIRST_ENTRY_KEY(); } @Override public KEY_TYPE POLL_FIRST_ENTRY_KEY() { return map.POLL_FIRST_ENTRY_KEY(); } @Override public KEY_TYPE LAST_ENTRY_KEY() { return map.LAST_ENTRY_KEY(); } @Override public KEY_TYPE POLL_LAST_ENTRY_KEY() { return map.POLL_LAST_ENTRY_KEY(); } @Override public VALUE_TYPE FIRST_ENTRY_VALUE() { return map.FIRST_ENTRY_VALUE(); } @Override public VALUE_TYPE LAST_ENTRY_VALUE() { return map.LAST_ENTRY_VALUE(); } } /** * The Unmodifyable Map implementation that is sued for the unmodifyableMap function * @Type(T) * @ValueType(V) */ public static class UnmodifyableMap KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements MAP KEY_VALUE_GENERIC_TYPE { MAP KEY_VALUE_GENERIC_TYPE map; VALUE_COLLECTION VALUE_GENERIC_TYPE values; SET KEY_GENERIC_TYPE keys; ObjectSet entrySet; UnmodifyableMap(MAP KEY_VALUE_GENERIC_TYPE map) { this.map = map; } @Override public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value){ throw new UnsupportedOperationException(); } #if VALUE_PRIMITIVES @Override public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #endif @Override public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { throw new UnsupportedOperationException(); } #if !TYPE_OBJECT || !VALUE_OBJECT @Override public boolean remove(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #endif @Override public VALUE_TYPE GET_VALUE(KEY_TYPE key) { return map.GET_VALUE(key); } #if !TYPE_OBJECT || !VALUE_OBJECT @Override public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { return map.getOrDefault(key, defaultValue); } #endif @Override public SET KEY_GENERIC_TYPE keySet() { if(keys == null) keys = SETS.unmodifiable(map.keySet()); return keys; } @Override public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { if(values == null) values = VALUE_COLLECTIONS.unmodifiable(map.values()); return values; } @Override public ObjectSet ENTRY_SET() { if(entrySet == null) entrySet = new UnmodifyableEntrySetKV_BRACES(map.ENTRY_SET()); return entrySet; } } /** * The Unmodifyable Set implementation for the Unmodifyable Map implementation * @Type(T) * @ValueType(V) */ public static class UnmodifyableEntrySet KEY_VALUE_GENERIC_TYPE extends ObjectSets.UnmodifiableSet { ObjectSet s; UnmodifyableEntrySet(ObjectSet c) { super(c); s = c; } @Override public void forEach(Consumer action) { s.forEach(T -> action.accept(unmodifiable(T))); } @Override public ObjectIterator iterator() { return new ObjectIterator() { ObjectIterator iter = s.iterator(); @Override public boolean hasNext() { return iter.hasNext(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { return unmodifiable(iter.next()); } }; } } /** * The Synchronized Navigable Map implementation used by the synchronizedMap helper function * @Type(T) * @ValueType(V) */ public static class SynchronizedNavigableMap KEY_VALUE_GENERIC_TYPE extends SynchronizedSortedMap KEY_VALUE_GENERIC_TYPE implements NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE { NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map; SynchronizedNavigableMap(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map) { super(map); this.map = map; } SynchronizedNavigableMap(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { super(map, mutex); this.map = map; } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { synchronized(mutex) { return synchronize(map.descendingMap(), mutex); } } @Override public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { synchronized(mutex) { return SETS.synchronize(map.navigableKeySet(), mutex); } } @Override public NAVIGABLE_SET KEY_GENERIC_TYPE descendingKeySet() { synchronized(mutex) { return SETS.synchronize(map.descendingKeySet(), mutex); } } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { synchronized(mutex) { return map.firstEntry(); } } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { synchronized(mutex) { return map.firstEntry(); } } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { synchronized(mutex) { return map.pollFirstEntry(); } } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { synchronized(mutex) { return map.pollLastEntry(); } } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { synchronized(mutex) { return synchronize(map.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex); } } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { synchronized(mutex) { return synchronize(map.headMap(toKey, inclusive), mutex); } } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { synchronized(mutex) { return synchronize(map.tailMap(fromKey, inclusive), mutex); } } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { synchronized(mutex) { return synchronize(map.subMap(fromKey, toKey), mutex); } } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { synchronized(mutex) { return synchronize(map.headMap(toKey), mutex); } } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { synchronized(mutex) { return synchronize(map.tailMap(fromKey), mutex); } } @Override public KEY_TYPE lowerKey(KEY_TYPE key) { synchronized(mutex) { return map.lowerKey(key); } } @Override public KEY_TYPE higherKey(KEY_TYPE key) { synchronized(mutex) { return map.higherKey(key); } } @Override public KEY_TYPE floorKey(KEY_TYPE key) { synchronized(mutex) { return map.floorKey(key); } } @Override public KEY_TYPE ceilingKey(KEY_TYPE key) { synchronized(mutex) { return map.ceilingKey(key); } } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE key) { synchronized(mutex) { return map.lowerEntry(key); } } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE key) { synchronized(mutex) { return map.higherEntry(key); } } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { synchronized(mutex) { return map.floorEntry(key); } } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { synchronized(mutex) { return map.ceilingEntry(key); } } #if !TYPE_OBJECT @Override @Deprecated public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, boolean fromInclusive, CLASS_TYPE toKey, boolean toInclusive) { synchronized(mutex) { return synchronize(map.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex); } } @Override @Deprecated public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey, boolean inclusive) { synchronized(mutex) { return synchronize(map.headMap(toKey, inclusive), mutex); } } @Override @Deprecated public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey, boolean inclusive) { synchronized(mutex) { return synchronize(map.tailMap(fromKey, inclusive), mutex); } } @Override @Deprecated public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, CLASS_TYPE toKey) { synchronized(mutex) { return synchronize(map.subMap(fromKey, toKey), mutex); } } @Override @Deprecated public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey) { synchronized(mutex) { return synchronize(map.headMap(toKey), mutex); } } @Override @Deprecated public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { synchronized(mutex) { return synchronize(map.tailMap(fromKey), mutex); } } @Override public void setDefaultMaxValue(KEY_TYPE e) { synchronized(mutex) { map.setDefaultMaxValue(e); } } @Override public KEY_TYPE getDefaultMaxValue() { synchronized(mutex) { return map.getDefaultMaxValue(); } } @Override public void setDefaultMinValue(KEY_TYPE e) { synchronized(mutex) { map.setDefaultMinValue(e); } } @Override public KEY_TYPE getDefaultMinValue() { synchronized(mutex) { return map.getDefaultMinValue(); } } @Override @Deprecated public CLASS_TYPE lowerKey(CLASS_TYPE key) { synchronized(mutex) { return map.lowerKey(key); } } @Override @Deprecated public CLASS_TYPE floorKey(CLASS_TYPE key) { synchronized(mutex) { return map.floorKey(key); } } @Override @Deprecated public CLASS_TYPE ceilingKey(CLASS_TYPE key) { synchronized(mutex) { return map.ceilingKey(key); } } @Override @Deprecated public CLASS_TYPE higherKey(CLASS_TYPE key) { synchronized(mutex) { return map.higherKey(key); } } @Override @Deprecated public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(CLASS_TYPE key) { synchronized(mutex) { return map.lowerEntry(key); } } @Override @Deprecated public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(CLASS_TYPE key) { synchronized(mutex) { return map.floorEntry(key); } } @Override @Deprecated public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(CLASS_TYPE key) { synchronized(mutex) { return map.ceilingEntry(key); } } @Override @Deprecated public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(CLASS_TYPE key) { synchronized(mutex) { return map.higherEntry(key); } } #endif } /** * The Synchronized Sorted Map implementation used by the synchronizedMap helper function * @Type(T) * @ValueType(V) */ public static class SynchronizedSortedMap KEY_VALUE_GENERIC_TYPE extends SynchronizedMap KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE { SORTED_MAP KEY_VALUE_GENERIC_TYPE map; SynchronizedSortedMap(SORTED_MAP KEY_VALUE_GENERIC_TYPE map) { super(map); this.map = map; } SynchronizedSortedMap(SORTED_MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { super(map, mutex); this.map = map; } @Override public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.putAndMoveToFirst(key, value); } } @Override public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.putAndMoveToLast(key, value); } } @Override public boolean moveToFirst(KEY_TYPE key) { synchronized(mutex) { return map.moveToFirst(key); } } @Override public boolean moveToLast(KEY_TYPE key) { synchronized(mutex) { return map.moveToLast(key); } } @Override public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { synchronized(mutex) { return map.getAndMoveToFirst(key); } } @Override public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { synchronized(mutex) { return map.getAndMoveToLast(key); } } @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { synchronized(mutex) { return map.comparator(); } } @Override public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { synchronized(mutex) { return synchronize(map.subMap(fromKey, toKey), mutex); } } @Override public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { synchronized(mutex) { return synchronize(map.headMap(toKey), mutex); } } @Override public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { synchronized(mutex) { return synchronize(map.tailMap(fromKey), mutex); } } @Override public KEY_TYPE FIRST_ENTRY_KEY() { synchronized(mutex) { return map.FIRST_ENTRY_KEY(); } } @Override public KEY_TYPE POLL_FIRST_ENTRY_KEY() { synchronized(mutex) { return map.POLL_FIRST_ENTRY_KEY(); } } @Override public KEY_TYPE LAST_ENTRY_KEY() { synchronized(mutex) { return map.LAST_ENTRY_KEY(); } } @Override public KEY_TYPE POLL_LAST_ENTRY_KEY() { synchronized(mutex) { return map.POLL_LAST_ENTRY_KEY(); } } @Override public VALUE_TYPE FIRST_ENTRY_VALUE() { synchronized(mutex) { return map.FIRST_ENTRY_VALUE(); } } @Override public VALUE_TYPE LAST_ENTRY_VALUE() { synchronized(mutex) { return map.LAST_ENTRY_VALUE(); } } #if !TYPE_OBJECT @Override @Deprecated public CLASS_TYPE firstKey() { synchronized(mutex) { return map.firstKey(); } } @Override @Deprecated public CLASS_TYPE lastKey() { synchronized(mutex) { return map.lastKey(); } } @Override @Deprecated public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, CLASS_TYPE toKey) { synchronized(mutex) { return synchronize(map.subMap(fromKey, toKey), mutex); } } @Override @Deprecated public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(CLASS_TYPE toKey) { synchronized(mutex) { return synchronize(map.headMap(toKey), mutex); } } @Override @Deprecated public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { synchronized(mutex) { return synchronize(map.tailMap(fromKey), mutex); } } #endif } /** * The Synchronized Map implementation used by the synchronizedMap helper function * @Type(T) * @ValueType(V) */ public static class SynchronizedMap KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements MAP KEY_VALUE_GENERIC_TYPE { MAP KEY_VALUE_GENERIC_TYPE map; VALUE_COLLECTION VALUE_GENERIC_TYPE values; SET KEY_GENERIC_TYPE keys; ObjectSet entrySet; protected Object mutex; SynchronizedMap(MAP KEY_VALUE_GENERIC_TYPE map) { this.map = map; mutex = this; } SynchronizedMap(MAP KEY_VALUE_GENERIC_TYPE map, Object mutex) { this.map = map; this.mutex = mutex; } @Override public VALUE_TYPE getDefaultReturnValue() { synchronized(mutex) { return map.getDefaultReturnValue(); } } @Override public ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE setDefaultReturnValue(VALUE_TYPE v) { synchronized(mutex) { map.setDefaultReturnValue(v); return this; } } @Override public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.put(key, value); } } @Override public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.putIfAbsent(key, value); } } public void putAllIfAbsent(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.putAllIfAbsent(m); } } #if VALUE_PRIMITIVES @Override public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.addTo(key, value); } } public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.addToAll(m); } } #endif @Override public void putAll(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.putAll(m); } } @Override public void putAll(Map m) { synchronized(mutex) { map.putAll(m); } } @Override public void putAll(KEY_TYPE[] keys, VALUE_TYPE[] values, int offset, int size) { synchronized(mutex) { map.putAll(keys, values, offset, size); } } #if !TYPE_OBJECT @Override public boolean containsKey(KEY_TYPE key) { synchronized(mutex) { return map.containsKey(key); } } #endif #if !VALUE_OBJECT @Override public boolean containsValue(VALUE_TYPE value) { synchronized(mutex) { return map.containsValue(value); } } #endif @Override public VALUE_TYPE GET_VALUE(KEY_TYPE key) { synchronized(mutex) { return map.GET_VALUE(key); } } @Override public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { synchronized(mutex) { return map.REMOVE_VALUE(key); } } @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { synchronized(mutex) { return map.REMOVE_VALUEOrDefault(key, defaultValue); } } #if !TYPE_OBJECT || !VALUE_OBJECT @Override public boolean remove(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.remove(key, value); } } #endif @Override public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) { synchronized(mutex) { return map.replace(key, oldValue, newValue); } } @Override public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.replace(key, value); } } @Override public void REPLACE_VALUES(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.REPLACE_VALUES(m); } } @Override public void REPLACE_VALUES(UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { map.REPLACE_VALUES(mappingFunction); } } @Override public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTE(key, mappingFunction); } } @Override public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTE_IF_ABSENT(key, mappingFunction); } } @Override public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTE_IF_PRESENT(key, mappingFunction); } } @Override public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.MERGE(key, value, mappingFunction); } } @Override public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { map.BULK_MERGE(m, mappingFunction); } } #if !TYPE_OBJECT || !VALUE_OBJECT @Override public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { synchronized(mutex) { return map.getOrDefault(key, defaultValue); } } #endif @Override public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) { synchronized(mutex) { map.forEach(action); } } @Override public int size() { synchronized(mutex) { return super.size(); } } @Override public SET KEY_GENERIC_TYPE keySet() { if(keys == null) keys = SETS.synchronize(map.keySet(), mutex); return keys; } @Override public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { if(values == null) values = VALUE_COLLECTIONS.synchronize(map.values(), mutex); return values; } @Override public ObjectSet ENTRY_SET() { if(entrySet == null) entrySet = ObjectSets.synchronize(map.ENTRY_SET(), mutex); return entrySet; } #if !TYPE_OBJECT @Override @Deprecated public CLASS_VALUE_TYPE get(Object key) { synchronized(mutex) { return map.get(key); } } @Override @Deprecated public CLASS_VALUE_TYPE getOrDefault(Object key, CLASS_VALUE_TYPE defaultValue) { synchronized(mutex) { return map.getOrDefault(key, defaultValue); } } @Override @Deprecated public boolean containsValue(Object value) { synchronized(mutex) { return map.containsValue(value); } } @Override @Deprecated public boolean containsKey(Object key) { synchronized(mutex) { return map.containsKey(key); } } @Override @Deprecated public CLASS_VALUE_TYPE put(CLASS_TYPE key, CLASS_VALUE_TYPE value) { synchronized(mutex) { return map.put(key, value); } } @Override @Deprecated public CLASS_VALUE_TYPE remove(Object key) { synchronized(mutex) { return map.remove(key); } } @Override @Deprecated public void clear() { synchronized(mutex) { map.clear(); } } @Override @Deprecated public CLASS_VALUE_TYPE putIfAbsent(CLASS_TYPE key, CLASS_VALUE_TYPE value) { synchronized(mutex) { return map.putIfAbsent(key, value); } } @Override @Deprecated public boolean remove(Object key, Object value) { synchronized(mutex) { return map.remove(key, value); } } @Override @Deprecated public boolean replace(CLASS_TYPE key, CLASS_VALUE_TYPE oldValue, CLASS_VALUE_TYPE newValue) { synchronized(mutex) { return map.replace(key, oldValue, newValue); } } @Override @Deprecated public CLASS_VALUE_TYPE replace(CLASS_TYPE key, CLASS_VALUE_TYPE value) { synchronized(mutex) { return map.replace(key, value); } } @Override @Deprecated public void replaceAll(BiFunction mappingFunction) { synchronized(mutex) { map.replaceAll(mappingFunction); } } @Override @Deprecated public CLASS_VALUE_TYPE compute(CLASS_TYPE key, BiFunction mappingFunction) { synchronized(mutex) { return map.compute(key, mappingFunction); } } @Override @Deprecated public CLASS_VALUE_TYPE computeIfAbsent(CLASS_TYPE key, Function mappingFunction) { synchronized(mutex) { return map.computeIfAbsent(key, mappingFunction); } } @Override @Deprecated public CLASS_VALUE_TYPE computeIfPresent(CLASS_TYPE key, BiFunction mappingFunction) { synchronized(mutex) { return computeIfPresent(key, mappingFunction); } } @Override @Deprecated public CLASS_VALUE_TYPE merge(CLASS_TYPE key, CLASS_VALUE_TYPE value, BiFunction mappingFunction) { synchronized(mutex) { return map.merge(key, value, mappingFunction); } } @Override @Deprecated public void forEach(BiConsumer action) { synchronized(mutex) { map.forEach(action); } } #endif } #endif }