Class SmallSortedMap<K extends FieldSet.FieldDescriptorLite<K>>
FieldSet.FieldDescriptorLite to Object.
This implementation is heavily optimized for insertion and iteration when the map is built
(via put()) with keys (FieldDescriptors) in ascending sorted order. When entries are
added in increasing order of the FieldDescriptor, no sorting overhead is incurred. The entries
are simply appended to a backing array, making memory allocation and insertion highly efficient.
Iteration over the entries is straightforward and avoids the creation of an Iterator
object. It should be done as follows:
for (int i = 0; i < fieldMap.size(); i++) {
process(fieldMap.getArrayEntryAt(i));
}
The resulting iteration is in order of ascending field tag number. The object returned by entrySet() adheres to the same contract but is less efficient as it necessarily involves
creating an object for iteration.
If entries are added out of order, the map defers sorting until necessary (e.g., when iterating, querying size, or retrieving elements). This lazily sorts and deduplicates the underlying array.
Modifying operations (such as put(), remove(), or clear()) are not
thread-safe until makeImmutable() is called, after which any modifying operation will
result in an UnsupportedOperationException. However, instances are thread-safe for
concurrent read-only operations (such as get() or size()) even before
makeImmutable() is called, as long as no modifying operations are performed concurrently.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classEntry implementation that implements Comparable in order to support binary search within the entry array.private classIterator implementation that switches from the entry array to the overflow entries appropriately.private classStateless view of the entries in the field map.Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final intprivate Object[]private booleanprivate booleanprivate SmallSortedMap<K>.EntrySetprivate static final intprivate int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate intbinarySearch(K key) private voidvoidclear()booleanThe implementation throws aClassCastExceptionif o is not an object of typeK.private voidensureCapacity(int minCapacity) private voidEnsures that the entries in the map are sorted and deduplicated.entrySet()Similar to the AbstractMap implementation ofkeySet()andvalues(), the entry set is created the first time this method is called, and returned in response to all subsequent calls.booleanThe implementation throws aClassCastExceptionif o is not an object of typeK.getArrayEntryAt(int index) inthashCode()booleanisEmpty()booleanvoidMake this map immutable from this point forward.voidThe implementation throws aClassCastExceptionif o is not an object of typeK.intsize()Methods inherited from class AbstractMap
clone, containsValue, keySet, toString, valuesMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
DEFAULT_FIELD_MAP_ARRAY_SIZE
static final int DEFAULT_FIELD_MAP_ARRAY_SIZE- See Also:
-
MAX_ARRAY_SIZE
private static final int MAX_ARRAY_SIZE- See Also:
-
entries
-
size
private int size -
isImmutable
private boolean isImmutable -
lazyEntrySet
-
isSortedAndDedupped
private volatile boolean isSortedAndDedupped
-
-
Constructor Details
-
SmallSortedMap
SmallSortedMap() -
SmallSortedMap
SmallSortedMap(int initialCapacity)
-
-
Method Details
-
makeImmutable
public void makeImmutable()Make this map immutable from this point forward. Immutable maps are guaranteed to be sorted and deduplicated. -
isImmutable
public boolean isImmutable()- Returns:
- Whether
makeImmutable()has been called.
-
getArrayEntryAt
-
size
public int size()- Specified by:
sizein interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
sizein classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
isEmptyin classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
containsKey
The implementation throws aClassCastExceptionif o is not an object of typeK.- Specified by:
containsKeyin interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
containsKeyin classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
get
The implementation throws aClassCastExceptionif o is not an object of typeK.- Specified by:
getin interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
getin classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
putAll
- Specified by:
putAllin interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
putAllin classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
put
- Specified by:
putin interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
putin classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
clear
public void clear()- Specified by:
clearin interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
clearin classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
remove
The implementation throws aClassCastExceptionif o is not an object of typeK.- Specified by:
removein interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
removein classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
ensureCapacity
private void ensureCapacity(int minCapacity) -
ensureSortedAndDeduplicated
private void ensureSortedAndDeduplicated()Ensures that the entries in the map are sorted and deduplicated.Immutable maps are guaranteed to be already sorted and deduplicated.
-
binarySearch
- Parameters:
key- The key to find in the entry array.- Returns:
- The returned integer position follows the same semantics as the value returned by
.
invalid reference
java.util.Arrays#binarySearch()
-
entrySet
Similar to the AbstractMap implementation ofkeySet()andvalues(), the entry set is created the first time this method is called, and returned in response to all subsequent calls.- Specified by:
entrySetin interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Specified by:
entrySetin classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
checkMutable
private void checkMutable()- Throws:
UnsupportedOperationException- ifmakeImmutable()has has been called.
-
equals
- Specified by:
equalsin interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
equalsin classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceMap<K extends FieldSet.FieldDescriptorLite<K>, Object>- Overrides:
hashCodein classAbstractMap<K extends FieldSet.FieldDescriptorLite<K>, Object>
-