Class SortedArrayStringMap
- java.lang.Object
-
- org.apache.logging.log4j.util.SortedArrayStringMap
-
- All Implemented Interfaces:
java.io.Serializable,IndexedReadOnlyStringMap,IndexedStringMap,ReadOnlyStringMap,StringMap
public class SortedArrayStringMap extends java.lang.Object implements IndexedStringMap
Consider this class private. Array-based implementation of theReadOnlyStringMapinterface. Keys are held in a sorted array.This is not a generic collection, but makes some trade-offs to optimize for the Log4j context data use case:
- Garbage-free iteration over key-value pairs with
BiConsumerandTriConsumer. - Fast copy. If the ThreadContextMap is also an instance of
SortedArrayStringMap, the full thread context data can be transferred with two array copies and two field updates. - Acceptable performance for small data sets. The current implementation stores keys in a sorted array, values
are stored in a separate array at the same index.
Worst-case performance of
getandcontainsKeyis O(log N), worst-case performance ofputandremoveis O(N log N). The expectation is that for the small values ofN(less than 100) that are the vast majority of ThreadContext use cases, the constants dominate performance more than the asymptotic performance of the algorithms used. - Compact representation.
- Since:
- 2.7
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static intDEFAULT_INITIAL_CAPACITYThe default initial capacity.private static java.lang.String[]EMPTYAn empty array instance to share when the table is not inflated.private static java.lang.StringFROZENprivate static java.lang.reflect.MethodgetObjectInputFilterprivate static intHASHVALprivate booleanimmutableprivate booleaniteratingprivate java.lang.String[]keysprivate static java.lang.reflect.MethodnewObjectInputFilterprivate static TriConsumer<java.lang.String,java.lang.Object,StringMap>PUT_ALLprivate static longserialVersionUIDprivate static java.lang.reflect.MethodsetObjectInputFilterprivate intsizeThe number of key-value mappings contained in this map.private intthresholdThe next size value at which to resize (capacity * load factor).private java.lang.Object[]values
-
Constructor Summary
Constructors Constructor Description SortedArrayStringMap()SortedArrayStringMap(int initialCapacity)SortedArrayStringMap(java.util.Map<java.lang.String,?> map)SortedArrayStringMap(ReadOnlyStringMap other)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidassertNoConcurrentModification()private voidassertNotFrozen()private static intceilingNextPowerOfTwo(int x)Calculate the next power of 2, greater than or equal to x.voidclear()Removes all key-value pairs from this collection.booleancontainsKey(java.lang.String key)Returnstrueif this data structure contains the specified key,falseotherwise.private voidensureCapacity()booleanequals(java.lang.Object obj)Indicates whether some other object is "equal to" this one.<V> voidforEach(BiConsumer<java.lang.String,? super V> action)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.<V,T>
voidforEach(TriConsumer<java.lang.String,? super V,T> action, T 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.voidfreeze()Makes this collection immutable.java.lang.StringgetKeyAt(int index)Viewing all key-value pairs as a sequence sorted by key, this method returns the key at the specified index, ornullif the specified index is less than zero or greater or equal to the size of this collection.<V> VgetValue(java.lang.String key)Returns the value for the specified key, ornullif the specified key does not exist in this collection.<V> VgetValueAt(int index)Viewing all key-value pairs as a sequence sorted by key, this method returns the value at the specified index, ornullif the specified index is less than zero or greater or equal to the size of this collection.private voidhandleSerializationException(java.lang.Throwable t, int i, java.lang.String key)inthashCode()Returns a hash code value for the object.private static inthashCode(java.lang.Object[] values, int length)intindexOfKey(java.lang.String key)Viewing all key-value pairs as a sequence sorted by key, this method returns the index of the specified key in that sequence.private voidinflateTable(int toSize)Inflates the table.private voidinitFrom0(SortedArrayStringMap other)private voidinsertAt(int index, java.lang.String key, java.lang.Object value)booleanisEmpty()Returnstrueif this collection is empty (size is zero),falseotherwise.booleanisFrozen()private static byte[]marshall(java.lang.Object obj)private voidmerge(SortedArrayStringMap other)private intnullKeyIndex()voidputAll(ReadOnlyStringMap source)Copies all key-value pairs from the specifiedReadOnlyStringMapinto thisStringMap.voidputValue(java.lang.String key, java.lang.Object value)Puts the specified key-value pair into the collection.private voidreadObject(java.io.ObjectInputStream s)Reconstitute theSortedArrayStringMapinstance from a stream (i.e., deserialize it).voidremove(java.lang.String key)Removes the key-value pair for the specified key from this data structure.private voidresize(int newCapacity)intsize()Returns the number of key-value pairs in this collection.java.util.Map<java.lang.String,java.lang.String>toMap()Returns a non-nullmutableMap<String, String>containing a snapshot of this data structure.java.lang.StringtoString()private static java.lang.Objectunmarshall(byte[] data, java.io.ObjectInputStream inputStream)private voidwriteObject(java.io.ObjectOutputStream s)Save the state of theSortedArrayStringMapinstance to a stream (i.e., serialize it).
-
-
-
Field Detail
-
DEFAULT_INITIAL_CAPACITY
private static final int DEFAULT_INITIAL_CAPACITY
The default initial capacity.- See Also:
- Constant Field Values
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
HASHVAL
private static final int HASHVAL
- See Also:
- Constant Field Values
-
PUT_ALL
private static final TriConsumer<java.lang.String,java.lang.Object,StringMap> PUT_ALL
-
EMPTY
private static final java.lang.String[] EMPTY
An empty array instance to share when the table is not inflated.
-
FROZEN
private static final java.lang.String FROZEN
- See Also:
- Constant Field Values
-
keys
private transient java.lang.String[] keys
-
values
private transient java.lang.Object[] values
-
size
private transient int size
The number of key-value mappings contained in this map.
-
setObjectInputFilter
private static final java.lang.reflect.Method setObjectInputFilter
-
getObjectInputFilter
private static final java.lang.reflect.Method getObjectInputFilter
-
newObjectInputFilter
private static final java.lang.reflect.Method newObjectInputFilter
-
threshold
private int threshold
The next size value at which to resize (capacity * load factor).
-
immutable
private boolean immutable
-
iterating
private transient boolean iterating
-
-
Constructor Detail
-
SortedArrayStringMap
public SortedArrayStringMap()
-
SortedArrayStringMap
public SortedArrayStringMap(int initialCapacity)
-
SortedArrayStringMap
public SortedArrayStringMap(ReadOnlyStringMap other)
-
SortedArrayStringMap
public SortedArrayStringMap(java.util.Map<java.lang.String,?> map)
-
-
Method Detail
-
assertNotFrozen
private void assertNotFrozen()
-
assertNoConcurrentModification
private void assertNoConcurrentModification()
-
clear
public void clear()
Description copied from interface:StringMapRemoves all key-value pairs from this collection.
-
containsKey
public boolean containsKey(java.lang.String key)
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.
-
toMap
public java.util.Map<java.lang.String,java.lang.String> 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.
-
freeze
public void freeze()
Description copied from interface:StringMapMakes this collection immutable. Attempts to modify the collection after thefreeze()method was called will result in anUnsupportedOperationExceptionbeing thrown.
-
isFrozen
public boolean isFrozen()
Description copied from interface:StringMap
-
getValue
public <V> V getValue(java.lang.String key)
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.
-
isEmpty
public boolean isEmpty()
Description copied from interface:ReadOnlyStringMapReturnstrueif this collection is empty (size is zero),falseotherwise.- Specified by:
isEmptyin interfaceReadOnlyStringMap- Returns:
trueif this collection is empty (size is zero).
-
indexOfKey
public int indexOfKey(java.lang.String key)
Description copied from interface:IndexedReadOnlyStringMapViewing all key-value pairs as a sequence sorted by key, this method returns the index of the specified key in that sequence. If the specified key is not found, this method returns(-(insertion point) - 1).- Specified by:
indexOfKeyin interfaceIndexedReadOnlyStringMap- Parameters:
key- the key whose index in the ordered sequence of keys to return- Returns:
- the index of the specified key or
(-(insertion point) - 1)if the key is not found. The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, orsize()if all elements are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
-
nullKeyIndex
private int nullKeyIndex()
-
putValue
public void putValue(java.lang.String key, java.lang.Object value)Description copied from interface:StringMapPuts the specified key-value pair into the collection.
-
insertAt
private void insertAt(int index, java.lang.String key, java.lang.Object value)
-
putAll
public void putAll(ReadOnlyStringMap source)
Description copied from interface:StringMapCopies all key-value pairs from the specifiedReadOnlyStringMapinto thisStringMap.
-
initFrom0
private void initFrom0(SortedArrayStringMap other)
-
merge
private void merge(SortedArrayStringMap other)
-
ensureCapacity
private void ensureCapacity()
-
resize
private void resize(int newCapacity)
-
inflateTable
private void inflateTable(int toSize)
Inflates the table.
-
remove
public void remove(java.lang.String key)
Description copied from interface:StringMapRemoves the key-value pair for the specified key from this data structure.
-
getKeyAt
public java.lang.String getKeyAt(int index)
Description copied from interface:IndexedReadOnlyStringMapViewing all key-value pairs as a sequence sorted by key, this method returns the key at the specified index, ornullif the specified index is less than zero or greater or equal to the size of this collection.- Specified by:
getKeyAtin interfaceIndexedReadOnlyStringMap- Parameters:
index- the index of the key to return- Returns:
- the key at the specified index or
null
-
getValueAt
public <V> V getValueAt(int index)
Description copied from interface:IndexedReadOnlyStringMapViewing all key-value pairs as a sequence sorted by key, this method returns the value at the specified index, ornullif the specified index is less than zero or greater or equal to the size of this collection.- Specified by:
getValueAtin interfaceIndexedReadOnlyStringMap- Parameters:
index- the index of the value to return- Returns:
- the value at the specified index or
null
-
size
public int size()
Description copied from interface:ReadOnlyStringMapReturns the number of key-value pairs in this collection.- Specified by:
sizein interfaceReadOnlyStringMap- Returns:
- the number of key-value pairs in this collection.
-
forEach
public <V> void forEach(BiConsumer<java.lang.String,? super V> action)
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.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
BiConsumer'sBiConsumer.accept(Object, Object)accept} method may cause aConcurrentModificationExceptionto be thrown.- 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
public <V,T> void forEach(TriConsumer<java.lang.String,? super V,T> action, T state)
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.T- 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.
-
equals
public boolean equals(java.lang.Object obj)
Description copied from interface:StringMapIndicates whether some other object is "equal to" this one.- Specified by:
equalsin interfaceStringMap- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the reference object with which to compare.- Returns:
trueif this object is the same as the obj argument;falseotherwise.- See Also:
StringMap.hashCode()
-
hashCode
public int hashCode()
Description copied from interface:StringMapReturns a hash code value for the object.
-
hashCode
private static int hashCode(java.lang.Object[] values, int length)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
writeObject
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOExceptionSave the state of theSortedArrayStringMapinstance to a stream (i.e., serialize it).- Throws:
java.io.IOException
-
marshall
private static byte[] marshall(java.lang.Object obj) throws java.io.IOException- Throws:
java.io.IOException
-
unmarshall
private static java.lang.Object unmarshall(byte[] data, java.io.ObjectInputStream inputStream) throws java.io.IOException, java.lang.ClassNotFoundException- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
ceilingNextPowerOfTwo
private static int ceilingNextPowerOfTwo(int x)
Calculate the next power of 2, greater than or equal to x.From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
- Parameters:
x- Value to round up- Returns:
- The next power of 2 from x inclusive
-
readObject
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundExceptionReconstitute theSortedArrayStringMapinstance from a stream (i.e., deserialize it).- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
handleSerializationException
private void handleSerializationException(java.lang.Throwable t, int i, java.lang.String key)
-
-