Top Description Fields Constructors Methods
org.apache.logging.log4j.util

public Class SortedArrayStringMap

extends Object
implements IndexedStringMap
Class Inheritance
All Implemented Interfaces
org.apache.logging.log4j.util.IndexedStringMap, org.apache.logging.log4j.util.StringMap, org.apache.logging.log4j.util.ReadOnlyStringMap, java.io.Serializable, org.apache.logging.log4j.util.IndexedReadOnlyStringMap
Annotations
@InternalApi
Imports
java.io.IOException, .InvalidObjectException, .Serializable, java.util.Arrays, .ConcurrentModificationException, .HashMap, .Map, .Objects, org.apache.logging.log4j.util.internal.SerializationUtil

Consider this class private. Array-based implementation of the ReadOnlyStringMap interface. 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:

Since
2.7

Field Summary

Modifier and TypeField and Description
private static final int
DEFAULT_INITIAL_CAPACITY

The default initial capacity.

private static final String[]
EMPTY

An empty array instance to share when the table is not inflated.

private static final String
private static final int
private boolean
private transient boolean
private transient String[]
private static final TriConsumer<String, Object, StringMap>
private static final long
private transient int
size

The number of key-value mappings contained in this map.

private int
threshold

The next size value at which to resize (capacity * load factor).

private transient Object[]

Constructor Summary

AccessConstructor and Description
public
public
SortedArrayStringMap(final int initialCapacity)

public
public

Method Summary

Modifier and TypeMethod and Description
private void
private void
private static int

Returns:

The next power of 2 from x inclusive
ceilingNextPowerOfTwo
(final int
Value to round up
x
)

Calculate the next power of 2, greater than or equal to x.

public void
clear()

Implements org.apache.logging.log4j.util.StringMap.clear.

Removes all key-value pairs from this collection.

public boolean
containsKey(final String
the key whose presence to check. May be null.
key
)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.containsKey.

Returns true if this data structure contains the specified key, false otherwise.

private void
public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Implements org.apache.logging.log4j.util.StringMap.equals.

Indicates whether some other object is "equal to" this one.

public <V> void
forEach(final BiConsumer<String, ? super V>
The action to be performed for each key-value pair in this collection.
action
)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

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.

public <V, T> void
forEach(final TriConsumer<String, ? super V, T>
The action to be performed for each key-value pair in this collection.
action
,
final T
the object to be passed as the third parameter to each invocation on the specified triconsumer.
state
)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

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.

public void
freeze()

Implements org.apache.logging.log4j.util.StringMap.freeze.

Makes this collection immutable.

public String
getKeyAt(final int
the index of the key to return
index
)

Implements org.apache.logging.log4j.util.IndexedReadOnlyStringMap.getKeyAt.

Viewing all key-value pairs as a sequence sorted by key, this method returns the key at the specified index, or null if the specified index is less than zero or greater or equal to the size of this collection.

public <V> V
getValue(final String
the key whose value to return.
key
)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.getValue.

Returns the value for the specified key, or null if the specified key does not exist in this collection.

public <V> V
getValueAt(final int
the index of the value to return
index
)

Implements org.apache.logging.log4j.util.IndexedReadOnlyStringMap.getValueAt.

Viewing all key-value pairs as a sequence sorted by key, this method returns the value at the specified index, or null if the specified index is less than zero or greater or equal to the size of this collection.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements org.apache.logging.log4j.util.StringMap.hashCode.

Returns a hash code value for the object.

private static int
hashCode(final Object[] values, final int length)

public int
indexOfKey(final String
the key whose index in the ordered sequence of keys to return
key
)

Implements org.apache.logging.log4j.util.IndexedReadOnlyStringMap.indexOfKey.

Viewing all key-value pairs as a sequence sorted by key, this method returns the index of the specified key in that sequence.

private void
inflateTable(final int toSize)

Inflates the table.

private void
private void
insertAt(final int index, final String key, final Object value)

public boolean
isEmpty()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.isEmpty.

Returns true if this collection is empty (size is zero), false otherwise.

public boolean
isFrozen()

Implements org.apache.logging.log4j.util.StringMap.isFrozen.

Returns true if this object has been frozen, false otherwise.

private void
private int
public void
putAll(final ReadOnlyStringMap
the ReadOnlyStringMap to copy key-value pairs from
source
)

Implements org.apache.logging.log4j.util.StringMap.putAll.

Copies all key-value pairs from the specified ReadOnlyStringMap into this StringMap.

public void
putValue(final String
the key to add or remove. Keys may be null.
key
,
final Object
the value to add. Values may be null.
value
)

Implements org.apache.logging.log4j.util.StringMap.putValue.

Puts the specified key-value pair into the collection.

private void
readObject(final ObjectInputStream s)

Reconstitute the SortedArrayStringMap instance from a stream (i.e., deserialize it).

public void
remove(final String
the key to remove. May be null.
key
)

Implements org.apache.logging.log4j.util.StringMap.remove.

Removes the key-value pair for the specified key from this data structure.

private void
resize(final int newCapacity)

public int
size()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.size.

Returns the number of key-value pairs in this collection.

public Map<String, String>
toMap()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.toMap.

Returns a non-null mutable Map<String, String> containing a snapshot of this data structure.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

private void
writeObject(final ObjectOutputStream s)

Save the state of the SortedArrayStringMap instance to a stream (i.e., serialize it).

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

DEFAULT_INITIAL_CAPACITYback to summary
private static final int DEFAULT_INITIAL_CAPACITY

The default initial capacity.

EMPTYback to summary
private static final String[] EMPTY

An empty array instance to share when the table is not inflated.

FROZENback to summary
private static final String FROZEN
HASHVALback to summary
private static final int HASHVAL
immutableback to summary
private boolean immutable
iteratingback to summary
private transient boolean iterating
keysback to summary
private transient String[] keys
PUT_ALLback to summary
private static final TriConsumer<String, Object, StringMap> PUT_ALL
serialVersionUIDback to summary
private static final long serialVersionUID
sizeback to summary
private transient int size

The number of key-value mappings contained in this map.

thresholdback to summary
private int threshold

The next size value at which to resize (capacity * load factor).

valuesback to summary
private transient Object[] values

Constructor Detail

SortedArrayStringMapback to summary
public SortedArrayStringMap()
SortedArrayStringMapback to summary
public SortedArrayStringMap(final int initialCapacity)
SortedArrayStringMapback to summary
public SortedArrayStringMap(final ReadOnlyStringMap other)
SortedArrayStringMapback to summary
public SortedArrayStringMap(final Map<String, ?> map)

Method Detail

assertNoConcurrentModificationback to summary
private void assertNoConcurrentModification()
assertNotFrozenback to summary
private void assertNotFrozen()
ceilingNextPowerOfTwoback to summary
private static int ceilingNextPowerOfTwo(final 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:int

Value to round up

Returns:int

The next power of 2 from x inclusive

clearback to summary
public void clear()

Implements org.apache.logging.log4j.util.StringMap.clear.

Doc from org.apache.logging.log4j.util.StringMap.clear.

Removes all key-value pairs from this collection.

Annotations
@Override
containsKeyback to summary
public boolean containsKey(final String key)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.containsKey.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.containsKey.

Returns true if this data structure contains the specified key, false otherwise.

Parameters
key:String

the key whose presence to check. May be null.

Returns:boolean

true if this data structure contains the specified key, false otherwise.

Annotations
@Override
ensureCapacityback to summary
private void ensureCapacity()
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Implements org.apache.logging.log4j.util.StringMap.equals.

Doc from org.apache.logging.log4j.util.StringMap.equals.

Indicates whether some other object is "equal to" this one.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
forEachback to summary
public <V> void forEach(final BiConsumer<String, ? super V> action)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

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.

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's BiConsumer#accept(Object, Object) accept} method may cause a ConcurrentModificationException to be thrown.

Parameters
<V>
type of the value.
action:BiConsumer<String, ? super V>

The action to be performed for each key-value pair in this collection.

Annotations
@SuppressWarnings:unchecked
@Override
forEachback to summary
public <V, T> void forEach(final TriConsumer<String, ? super V, T> action, final T state)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.forEach.

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.

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's accept method may cause a ConcurrentModificationException to be thrown.

Parameters
<V>
type of the value.
<T>
type of the third parameter.
action:TriConsumer<String, ? super V, T>

The action to be performed for each key-value pair in this collection.

state:T

the object to be passed as the third parameter to each invocation on the specified triconsumer.

Annotations
@SuppressWarnings:unchecked
@Override
freezeback to summary
public void freeze()

Implements org.apache.logging.log4j.util.StringMap.freeze.

Doc from org.apache.logging.log4j.util.StringMap.freeze.

Makes this collection immutable. Attempts to modify the collection after the freeze() method was called will result in an UnsupportedOperationException being thrown.

Annotations
@Override
getKeyAtback to summary
public String getKeyAt(final int index)

Implements org.apache.logging.log4j.util.IndexedReadOnlyStringMap.getKeyAt.

Doc from org.apache.logging.log4j.util.IndexedReadOnlyStringMap.getKeyAt.

Viewing all key-value pairs as a sequence sorted by key, this method returns the key at the specified index, or null if the specified index is less than zero or greater or equal to the size of this collection.

Parameters
index:int

the index of the key to return

Returns:String

the key at the specified index or null

Annotations
@Override
getValueback to summary
public <V> V getValue(final String key)

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.getValue.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.getValue.

Returns the value for the specified key, or null if the specified key does not exist in this collection.

Parameters
key:String

the key whose value to return.

Returns:V

the value for the specified key or null.

Annotations
@SuppressWarnings:unchecked
@Override
getValueAtback to summary
public <V> V getValueAt(final int index)

Implements org.apache.logging.log4j.util.IndexedReadOnlyStringMap.getValueAt.

Doc from org.apache.logging.log4j.util.IndexedReadOnlyStringMap.getValueAt.

Viewing all key-value pairs as a sequence sorted by key, this method returns the value at the specified index, or null if the specified index is less than zero or greater or equal to the size of this collection.

Parameters
index:int

the index of the value to return

Returns:V

the value at the specified index or null

Annotations
@SuppressWarnings:unchecked
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements org.apache.logging.log4j.util.StringMap.hashCode.

Doc from org.apache.logging.log4j.util.StringMap.hashCode.

Returns a hash code value for the object.

Returns:int

a hash code value for this object.

Annotations
@Override
hashCodeback to summary
private static int hashCode(final Object[] values, final int length)
indexOfKeyback to summary
public int indexOfKey(final String key)

Implements org.apache.logging.log4j.util.IndexedReadOnlyStringMap.indexOfKey.

Doc from org.apache.logging.log4j.util.IndexedReadOnlyStringMap.indexOfKey.

Viewing 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).

Parameters
key:String

the key whose index in the ordered sequence of keys to return

Returns:int

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, or size() 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.

Annotations
@Override
inflateTableback to summary
private void inflateTable(final int toSize)

Inflates the table.

initFrom0back to summary
private void initFrom0(final SortedArrayStringMap other)
insertAtback to summary
private void insertAt(final int index, final String key, final Object value)
isEmptyback to summary
public boolean isEmpty()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.isEmpty.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.isEmpty.

Returns true if this collection is empty (size is zero), false otherwise.

Returns:boolean

true if this collection is empty (size is zero).

Annotations
@Override
isFrozenback to summary
public boolean isFrozen()

Implements org.apache.logging.log4j.util.StringMap.isFrozen.

Doc from org.apache.logging.log4j.util.StringMap.isFrozen.

Returns true if this object has been frozen, false otherwise.

Returns:boolean

true if this object has been frozen, false otherwise

Annotations
@Override
mergeback to summary
private void merge(final SortedArrayStringMap other)
nullKeyIndexback to summary
private int nullKeyIndex()
putAllback to summary
public void putAll(final ReadOnlyStringMap source)

Implements org.apache.logging.log4j.util.StringMap.putAll.

Doc from org.apache.logging.log4j.util.StringMap.putAll.

Copies all key-value pairs from the specified ReadOnlyStringMap into this StringMap.

Parameters
source:ReadOnlyStringMap

the ReadOnlyStringMap to copy key-value pairs from

Annotations
@Override
putValueback to summary
public void putValue(final String key, final Object value)

Implements org.apache.logging.log4j.util.StringMap.putValue.

Doc from org.apache.logging.log4j.util.StringMap.putValue.

Puts the specified key-value pair into the collection.

Parameters
key:String

the key to add or remove. Keys may be null.

value:Object

the value to add. Values may be null.

Annotations
@Override
readObjectback to summary
private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException

Reconstitute the SortedArrayStringMap instance from a stream (i.e., deserialize it).

removeback to summary
public void remove(final String key)

Implements org.apache.logging.log4j.util.StringMap.remove.

Doc from org.apache.logging.log4j.util.StringMap.remove.

Removes the key-value pair for the specified key from this data structure.

Parameters
key:String

the key to remove. May be null.

Annotations
@Override
resizeback to summary
private void resize(final int newCapacity)
sizeback to summary
public int size()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.size.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.size.

Returns the number of key-value pairs in this collection.

Returns:int

the number of key-value pairs in this collection.

Annotations
@Override
toMapback to summary
public Map<String, String> toMap()

Implements org.apache.logging.log4j.util.ReadOnlyStringMap.toMap.

Doc from org.apache.logging.log4j.util.ReadOnlyStringMap.toMap.

Returns a non-null mutable Map<String, String> containing a snapshot of this data structure.

Returns:Map<String, String>

a mutable copy of this data structure in Map<String, String> form.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
writeObjectback to summary
private void writeObject(final ObjectOutputStream s) throws IOException

Save the state of the SortedArrayStringMap instance to a stream (i.e., serialize it).

Serial data
The capacity of the SortedArrayStringMap (the length of the bucket array) is emitted (int), followed by the size (an int, the number of key-value mappings), followed by the key (Object) and value (Object) for each key-value mapping. The key-value mappings are emitted in no particular order.