Class SmallSortedMap<K extends FieldSet.FieldDescriptorLite<K>>
- java.lang.Object
-
- java.util.AbstractMap<K,java.lang.Object>
-
- com.google.protobuf.SmallSortedMap<K>
-
- All Implemented Interfaces:
java.util.Map<K,java.lang.Object>
class SmallSortedMap<K extends FieldSet.FieldDescriptorLite<K>> extends java.util.AbstractMap<K,java.lang.Object>A custom map implementation fromFieldSet.FieldDescriptorLiteto 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
Iteratorobject. It should be done as follows:
The resulting iteration is in order of ascending field tag number. The object returned byfor (int i = 0; i < fieldMap.size(); i++) { process(fieldMap.getArrayEntryAt(i)); }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(), orclear()) are not thread-safe untilmakeImmutable()is called, after which any modifying operation will result in anUnsupportedOperationException. However, instances are thread-safe for concurrent read-only operations (such asget()orsize()) even beforemakeImmutable()is called, as long as no modifying operations are performed concurrently.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classSmallSortedMap.EntryEntry implementation that implements Comparable in order to support binary search within the entry array.private classSmallSortedMap.EntryIteratorIterator implementation that switches from the entry array to the overflow entries appropriately.private classSmallSortedMap.EntrySetStateless view of the entries in the field map.
-
Field Summary
Fields Modifier and Type Field Description (package private) static intDEFAULT_FIELD_MAP_ARRAY_SIZEprivate java.lang.Object[]entriesprivate booleanisImmutableprivate booleanisSortedAndDeduppedprivate SmallSortedMap.EntrySetlazyEntrySetprivate static intMAX_ARRAY_SIZEprivate intsize
-
Constructor Summary
Constructors Constructor Description SmallSortedMap()SmallSortedMap(int initialCapacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private intbinarySearch(K key)private voidcheckMutable()voidclear()booleancontainsKey(java.lang.Object o)The implementation throws aClassCastExceptionif o is not an object of typeK.private voidensureCapacity(int minCapacity)private voidensureSortedAndDeduplicated()Ensures that the entries in the map are sorted and deduplicated.java.util.Set<java.util.Map.Entry<K,java.lang.Object>>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.booleanequals(java.lang.Object o)java.lang.Objectget(java.lang.Object o)The implementation throws aClassCastExceptionif o is not an object of typeK.java.util.Map.Entry<K,java.lang.Object>getArrayEntryAt(int index)inthashCode()booleanisEmpty()booleanisImmutable()voidmakeImmutable()Make this map immutable from this point forward.java.lang.Objectput(K key, java.lang.Object value)voidputAll(java.util.Map<? extends K,?> map)java.lang.Objectremove(java.lang.Object o)The implementation throws aClassCastExceptionif o is not an object of typeK.intsize()
-
-
-
Field Detail
-
DEFAULT_FIELD_MAP_ARRAY_SIZE
static final int DEFAULT_FIELD_MAP_ARRAY_SIZE
- See Also:
- Constant Field Values
-
MAX_ARRAY_SIZE
private static final int MAX_ARRAY_SIZE
- See Also:
- Constant Field Values
-
entries
private java.lang.Object[] entries
-
size
private int size
-
isImmutable
private boolean isImmutable
-
lazyEntrySet
private volatile SmallSortedMap.EntrySet lazyEntrySet
-
isSortedAndDedupped
private volatile boolean isSortedAndDedupped
-
-
Method Detail
-
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
public java.util.Map.Entry<K,java.lang.Object> getArrayEntryAt(int index)
- Returns:
- The array entry at the given
index.
-
size
public int size()
- Specified by:
sizein interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
sizein classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
isEmptyin classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
containsKey
public boolean containsKey(java.lang.Object o)
The implementation throws aClassCastExceptionif o is not an object of typeK.- Specified by:
containsKeyin interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
containsKeyin classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
get
public java.lang.Object get(java.lang.Object o)
The implementation throws aClassCastExceptionif o is not an object of typeK.- Specified by:
getin interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
getin classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
putAll
public void putAll(java.util.Map<? extends K,?> map)
- Specified by:
putAllin interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
putAllin classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
put
@CanIgnoreReturnValue public java.lang.Object put(K key, java.lang.Object value)
- Specified by:
putin interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
putin classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
clearin classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
remove
@CanIgnoreReturnValue public java.lang.Object remove(java.lang.Object o)
The implementation throws aClassCastExceptionif o is not an object of typeK.- Specified by:
removein interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
removein classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.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
private int binarySearch(K key)
- Parameters:
key- The key to find in the entry array.- Returns:
- The returned integer position follows the same semantics as the value returned by
java.util.Arrays#binarySearch().
-
entrySet
public java.util.Set<java.util.Map.Entry<K,java.lang.Object>> 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 interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Specified by:
entrySetin classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
checkMutable
private void checkMutable()
- Throws:
java.lang.UnsupportedOperationException- ifmakeImmutable()has has been called.
-
equals
public boolean equals(java.lang.Object o)
- Specified by:
equalsin interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
equalsin classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfacejava.util.Map<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>- Overrides:
hashCodein classjava.util.AbstractMap<K extends FieldSet.FieldDescriptorLite<K>,java.lang.Object>
-
-