Class UnmodifiableArrayBackedMap
java.lang.Object
java.util.AbstractMap<String,String>
org.apache.logging.log4j.internal.map.UnmodifiableArrayBackedMap
- All Implemented Interfaces:
Serializable,Map<String,,String> ReadOnlyStringMap
public class UnmodifiableArrayBackedMap
extends AbstractMap<String,String>
implements ReadOnlyStringMap
This class represents an immutable map, which stores its state inside a single Object[]:
- [0] contains the number of entries
- Others contain alternating key-value pairs, for example [1]="1" and [2]="value_for_1"
- Implements very low-cost copies: shallow-copy the array.
- Doesn't matter for mutable operations, since we don't allow them.
- Iterates very quickly, since it iterates directly across the array. This contrasts with HashMap's requirement to scan each bucket in the table and chase each pointer.
- Is linear on gets, puts, and removes, since the table must be scanned to find a matching key.
- Zero on reads.
- Copy-and-modify operations allocate exactly two objects: the new array and the new Map instance. This is substantially better than HashMap, which requires a new Node for each entry.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classImplementation of Map.Entry.private classSimple Entry iterator, tracking solely the index in the array.private classSimple Entry set, providing a reference to UnmodifiableEntryIterator and blocking modifications.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Object[]backingArray is functionally final, but marking it as such can cause performance problems.static final UnmodifiableArrayBackedMapprivate static final intprivate intprivate static final long -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateUnmodifiableArrayBackedMap(int capacity) privateUnmodifiableArrayBackedMap(Object[] backingArray) (package private) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidprivate voidaddOrOverwriteKey(String key, String value) Find an existing entry (if any) and overwrites the value, if foundvoidclear()booleancontainsKey(Object key) Scans the array to find a matching key.booleancontainsKey(String key) Returnstrueif this data structure contains the specified key,falseotherwise.booleancontainsValue(Object value) Scans the array to find a matching value, with linear time.copyAndPut(String key, String value) Creates a new instance that contains the same entries as this map, plus either the new entry or updated value passed in the parameters.copyAndPutAll(Map<String, String> entriesToAdd) Creates a new instance that contains the same entries as this map, plus the new entries or updated values passed in the parameters.copyAndRemove(String key) Creates a new instance that contains the same entries as this map, minus the entry with the specified key (if such an entry exists).copyAndRemoveAll(Iterable<String> keysToRemoveIterable) Creates a new instance where the entries of provided keys are removed.entrySet()voidforEach(BiConsumer<? super String, ? super String> action) This version of forEach is defined on the Map interface.<V> voidforEach(BiConsumer<String, ? super V> action) This version of forEach is defined on the ReadOnlyStringMap interface.<V,S> void forEach(TriConsumer<String, ? super V, S> action, S state) Performs the given action for each key-value pair in this data structure until all entries have been processed or the action throws an exception.Scans the array to find a matching key.private static intgetArrayIndexForKey(int entryIndex) private static intgetArrayIndexForValue(int entryIndex) Object[]static UnmodifiableArrayBackedMap<V> VReturns the value for the specified key, ornullif the specified key does not exist in this collection.voidintsize()Returns the number of key-value pairs in this collection.toMap()Returns a non-nullmutableMap<String, String>containing a snapshot of this data structure.private voidCopies the locally-tracked numEntries into the first array slot.Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, isEmpty, keySet, toString, valuesMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllMethods inherited from interface org.apache.logging.log4j.util.ReadOnlyStringMap
isEmpty
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
EMPTY_MAP
-
NUM_FIXED_ARRAY_ENTRIES
private static final int NUM_FIXED_ARRAY_ENTRIES- See Also:
-
backingArray
backingArray is functionally final, but marking it as such can cause performance problems. Consider marking it final after https://bugs.openjdk.org/browse/JDK-8324186 is solved. -
numEntries
private int numEntries
-
-
Constructor Details
-
UnmodifiableArrayBackedMap
private UnmodifiableArrayBackedMap(int capacity) -
UnmodifiableArrayBackedMap
-
UnmodifiableArrayBackedMap
UnmodifiableArrayBackedMap(UnmodifiableArrayBackedMap other)
-
-
Method Details
-
getArrayIndexForKey
private static int getArrayIndexForKey(int entryIndex) -
getArrayIndexForValue
private static int getArrayIndexForValue(int entryIndex) -
getMap
-
add
-
clear
public void clear() -
containsKey
Scans the array to find a matching key. Linear performance.- Specified by:
containsKeyin interfaceMap<String,String> - Overrides:
containsKeyin classAbstractMap<String,String>
-
containsKey
Description copied from interface:ReadOnlyStringMapReturnstrueif this data structure contains the specified key,falseotherwise.- Specified by:
containsKeyin interfaceReadOnlyStringMap- Parameters:
key- the key whose presence to check. May benull.- Returns:
trueif this data structure contains the specified key,falseotherwise.
-
getBackingArray
-
containsValue
Scans the array to find a matching value, with linear time. Allows null parameter.- Specified by:
containsValuein interfaceMap<String,String> - Overrides:
containsValuein classAbstractMap<String,String>
-
copyAndPut
Creates a new instance that contains the same entries as this map, plus either the new entry or updated value passed in the parameters.- Parameters:
key-value-- Returns:
-
copyAndPutAll
Creates a new instance that contains the same entries as this map, plus the new entries or updated values passed in the parameters. -
copyAndRemove
Creates a new instance that contains the same entries as this map, minus the entry with the specified key (if such an entry exists). -
copyAndRemoveAll
Creates a new instance where the entries of provided keys are removed. -
updateNumEntriesInArray
private void updateNumEntriesInArray()Copies the locally-tracked numEntries into the first array slot. Requires autoboxing so call should be minimized - for example, once per bulk update operation. -
forEach
This version of forEach is defined on the Map interface. -
forEach
This version of forEach is defined on the ReadOnlyStringMap interface.- Specified by:
forEachin interfaceReadOnlyStringMap- Type Parameters:
V- type of the value.- Parameters:
action- The action to be performed for each key-value pair in this collection.
-
forEach
Description copied from interface:ReadOnlyStringMapPerforms the given action for each key-value pair in this data structure until all entries have been processed or the action throws an exception.The third parameter lets callers pass in a stateful object to be modified with the key-value pairs, so the TriConsumer implementation itself can be stateless and potentially reusable.
Some implementations may not support structural modifications (adding new elements or removing elements) while iterating over the contents. In such implementations, attempts to add or remove elements from the
TriConsumer'sacceptmethod may cause aConcurrentModificationExceptionto be thrown.- Specified by:
forEachin interfaceReadOnlyStringMap- Type Parameters:
V- type of the value.S- type of the third parameter.- Parameters:
action- The action to be performed for each key-value pair in this collection.state- the object to be passed as the third parameter to each invocation on the specified triconsumer.
-
entrySet
-
get
Scans the array to find a matching key. Linear-time. -
getValue
Description copied from interface:ReadOnlyStringMapReturns the value for the specified key, ornullif the specified key does not exist in this collection.- Specified by:
getValuein interfaceReadOnlyStringMap- Parameters:
key- the key whose value to return.- Returns:
- the value for the specified key or
null.
-
addOrOverwriteKey
Find an existing entry (if any) and overwrites the value, if found- Parameters:
key-value-
-
put
-
putAll
-
remove
-
size
public int size()Description copied from interface:ReadOnlyStringMapReturns the number of key-value pairs in this collection.- Specified by:
sizein interfaceMap<String,String> - Specified by:
sizein interfaceReadOnlyStringMap- Overrides:
sizein classAbstractMap<String,String> - Returns:
- the number of key-value pairs in this collection.
-
toMap
Description copied from interface:ReadOnlyStringMapReturns a non-nullmutableMap<String, String>containing a snapshot of this data structure.- Specified by:
toMapin interfaceReadOnlyStringMap- Returns:
- a mutable copy of this data structure in
Map<String, String>form.
-