k mappings in an array for a configurable value of k, allowing direct
access to the corresponding Entrys without the need to create an Iterator. The remaining
entries are stored in an overflow map. Iteration over the entries in the map should be done as
follows:
for (int i = 0; i < fieldMap.getNumArrayEntries(); i++) {
process(fieldMap.getArrayEntryAt(i));
}
for (Map.Entry<K, V> entry : fieldMap.getOverflowEntries()) {
process(entry);
}
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.
The tradeoff for this memory efficiency is that the worst case running time of the put() operation is O(k + lg n), which happens when entries are added in descending
order. k should be chosen such that it covers enough common cases without adversely
affecting larger maps. In practice, the worst case scenario does not happen for extensions
because extension fields are serialized and deserialized in order of ascending tag number, but
the worst case scenario can happen for DynamicMessages.
The running time for all other operations is similar to that of TreeMap.
Instances are not thread-safe until makeImmutable() is called, after which any
modifying operation will result in an UnsupportedOperationException.
| Modifier and Type | Class and Description |
|---|---|
| private static class | SmallSortedMap.EmptySet
Helper class that holds immutable instances of an Iterable/Iterator that we return when the overflow entries is empty. |
| private class | SmallSortedMap.Entry
Entry implementation that implements Comparable in order to support binary search within the entry array. |
| private class | SmallSortedMap.EntryIterator
Iterator implementation that switches from the entry array to the overflow entries appropriately. |
| private class | SmallSortedMap.EntrySet
Stateless view of the entries in the field map. |
| Modifier and Type | Field and Description |
|---|---|
| private List | |
| private boolean | |
| private volatile SmallSortedMap | |
| private final int | |
| private Map |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private int | Returns: The returned integer position follows the same semantics as the value returned byjava..The key to find in the entry array. key)
|
| private void | |
| public void | clear()
Overrides java. Implements java. |
| public boolean | containsKey(Object
key whose presence in this map is to be tested o)Overrides java. Implements java. ClassCastException if o is not an object of type K.
|
| private void | |
| public Set | entrySet()
Implements abstract java. Implements java. keySet() and values(), the entry
set is created the first time this method is called, and returned in response to all subsequent
calls.
|
| public boolean | equals(Object
object to be compared for equality with this map o)Overrides java. Implements java. |
| public V | get(Object
the key whose associated value is to be returned o)Overrides java. Implements java. ClassCastException if o is not an object of type K.
|
| public Map. | |
| public int | |
| public int | |
| public Iterable | |
| private SortedMap | Returns: aSortedMap to which overflow entries mappings can be added or removed.
|
| public int | hashCode()
Overrides java. Implements java. |
| public boolean | |
| public void | |
| pack-priv static <FieldDescriptorType extends FieldSet. | newFieldMap(int
The size of the entry array containing the lexicographically smallest
mappings. arraySize)Creates a new instance for mapping FieldDescriptors to their values. |
| pack-priv static <K extends Comparable | newInstanceForTest(int
The size of the entry array containing the lexicographically smallest
mappings. arraySize)Creates a new instance for testing. |
| public V | put(K
key with which the specified value is to be associated key, V value to be associated with the specified key value)Overrides java. Implements java. |
| public V | remove(Object
key whose mapping is to be removed from the map o)Overrides java. Implements java. ClassCastException if o is not an object of type K.
|
| private V | |
| public int | size()
Overrides java. Implements java. |
| entryList | back to summary |
|---|---|
| private List<SmallSortedMap<K, V>. | |
| isImmutable | back to summary |
|---|---|
| private boolean isImmutable | |
| lazyEntrySet | back to summary |
|---|---|
| private volatile SmallSortedMap<K, V>. | |
| maxArraySize | back to summary |
|---|---|
| private final int maxArraySize | |
| overflowEntries | back to summary |
|---|---|
| private Map<K, V> overflowEntries | |
| SmallSortedMap | back to summary |
|---|---|
| private SmallSortedMap(int arraySize) | |
| binarySearchInArray | back to summary |
|---|---|
private int binarySearchInArray(K key)
| |
| checkMutable | back to summary |
|---|---|
private void checkMutable()
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides java. Implements java. Doc from java. Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.
| |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(Object o) Overrides java. Implements java. Doc from java. The implementation throws a Returns
| |
| ensureEntryArrayMutable | back to summary |
|---|---|
| private void ensureEntryArrayMutable() Lazily creates the entry list. Any code that adds to the list must first call this method. | |
| entrySet | back to summary |
|---|---|
| public Set Implements abstract java. Implements java. Doc from java. Similar to the AbstractMap implementation of Returns a | |
| equals | back to summary |
|---|---|
| public boolean equals(Object o) Overrides java. Implements java. Doc from java. Compares the specified object with this map for equality. Returns
| |
| get | back to summary |
|---|---|
| public V get(Object o) Overrides java. Implements java. Doc from java. The implementation throws a Returns the value to which the specified key is mapped,
or More formally, if this map contains a mapping from a key
If this map permits null values, then a return value of
| |
| getArrayEntryAt | back to summary |
|---|---|
| public Map. | |
| getNumArrayEntries | back to summary |
|---|---|
public int getNumArrayEntries()
| |
| getNumOverflowEntries | back to summary |
|---|---|
public int getNumOverflowEntries()
| |
| getOverflowEntries | back to summary |
|---|---|
| public Iterable | |
| getOverflowEntriesMutable | back to summary |
|---|---|
private SortedMap
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Implements java. Doc from java. Returns the hash code value for this map. The hash code of a map is
defined to be the sum of the hash codes of each entry in the map's
| |
| isImmutable | back to summary |
|---|---|
public boolean isImmutable()
| |
| makeImmutable | back to summary |
|---|---|
| public void makeImmutable() Make this map immutable from this point forward. | |
| newFieldMap | back to summary |
|---|---|
| pack-priv static <FieldDescriptorType extends FieldSet. Creates a new instance for mapping FieldDescriptors to their values. The
| |
| newInstanceForTest | back to summary |
|---|---|
| pack-priv static <K extends Comparable Creates a new instance for testing.
| |
| put | back to summary |
|---|---|
| public V put(K key, V value) Overrides java. Implements java. Doc from java. Associates the specified value with the specified key in this map
(optional operation). If the map previously contained a mapping for
the key, the old value is replaced by the specified value. (A map
| |
| remove | back to summary |
|---|---|
| public V remove(Object o) Overrides java. Implements java. Doc from java. The implementation throws a Removes the mapping for a key from this map if it is present
(optional operation). More formally, if this map contains a mapping
from key Returns the value to which this map previously associated the key,
or If this map permits null values, then a return value of
The map will not contain a mapping for the specified key once the call returns.
| |
| removeArrayEntryAt | back to summary |
|---|---|
| private V removeArrayEntryAt(int index) | |
| size | back to summary |
|---|---|
| public int size() Overrides java. Implements java. Doc from java. Returns the number of key-value mappings in this map. If the
map contains more than
| |
| Modifier and Type | Field and Description |
|---|---|
| private static final Iterable | |
| private static final Iterator |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv static <T> Iterable |
| ITERABLE | back to summary |
|---|---|
| private static final Iterable<Object> ITERABLE | |
| ITERATOR | back to summary |
|---|---|
| private static final Iterator<Object> ITERATOR | |
| EmptySet | back to summary |
|---|---|
| private EmptySet() | |
| iterable | back to summary |
|---|---|
pack-priv static <T> Iterable
| |
setValue().
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public int | compareTo(SmallSortedMap<K, V>.
the object to be compared. otherImplements java. |
| public boolean | equals(Object
object to be compared for equality with this map entry o)Overrides java. Implements java. |
| private boolean | |
| public K | |
| public V | |
| public int | hashCode()
Overrides java. Implements java. |
| public V | setValue(V
new value to be stored in this entry newValue)Implements java. |
| public String |
| Entry | back to summary |
|---|---|
| pack-priv Entry(Map. | |
| Entry | back to summary |
|---|---|
| pack-priv Entry(K key, V value) | |
| compareTo | back to summary |
|---|---|
| public int compareTo(SmallSortedMap<K, V>. Implements java. Doc from java. Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object o) Overrides java. Implements java. Doc from java. Compares the specified object with this entry for equality.
Returns
(e1.getKey()==null ?
e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
(e1.getValue()==null ?
e2.getValue()==null : e1.getValue().equals(e2.getValue()))
This ensures that the equals method works properly across
different implementations of the Map.Entry interface.
| |
| equals | back to summary |
|---|---|
| private boolean equals(Object o1, Object o2) equals() that handles null values. | |
| getKey | back to summary |
|---|---|
| public K getKey() Implements java. Doc from java. Returns the key corresponding to this entry.
| |
| getValue | back to summary |
|---|---|
| public V getValue() Implements java. Doc from java. Returns the value corresponding to this entry. If the mapping
has been removed from the backing map (by the iterator's
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Implements java. Doc from java. Returns the hash code value for this map entry. The hash code
of a map entry
(e.getKey()==null ? 0 : e.getKey().hashCode()) ^
(e.getValue()==null ? 0 : e.getValue().hashCode())
This ensures that e1.equals(e2) implies that
e1.hashCode()==e2.hashCode() for any two Entries
e1 and e2, as required by the general
contract of Object.hashCode.
| |
| setValue | back to summary |
|---|---|
| public V setValue(V newValue) Implements java. Doc from java. Replaces the value corresponding to this entry with the specified
value (optional operation). (Writes through to the map.) The
behavior of this call is undefined if the mapping has already been
removed from the map (by the iterator's
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| private Iterator | |
| private boolean | |
| private int |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private Iterator | getOverflowIterator()
It is important to create the overflow iterator only after the array entries have been iterated over because the overflow entry set changes when the client calls remove() on the array entries, which invalidates any existing iterators. |
| public boolean | |
| public Map. | |
| public void | remove()
Overrides default java. |
| lazyOverflowIterator | back to summary |
|---|---|
| private Iterator<Map. | |
| nextCalledBeforeRemove | back to summary |
|---|---|
| private boolean nextCalledBeforeRemove | |
| pos | back to summary |
|---|---|
| private int pos | |
| EntryIterator | back to summary |
|---|---|
| private EntryIterator() | |
| getOverflowIterator | back to summary |
|---|---|
| private Iterator It is important to create the overflow iterator only after the array entries have been iterated over because the overflow entry set changes when the client calls remove() on the array entries, which invalidates any existing iterators. | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns
| |
| next | back to summary |
|---|---|
| public Map. Implements java. Doc from java. Returns the next element in the iteration. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | add(Map.
element to be added to this set entryOverrides java. Implements java. |
| public void | clear()
Overrides java. Implements java. |
| public boolean | contains(Object
element whose presence in this set is to be tested o)Overrides java. Implements java. ClassCastException if o is not of the expected type.
|
| public Iterator | iterator()
Implements abstract java. Implements java. |
| public boolean | remove(Object
object to be removed from this set, if present o)Overrides java. Implements java. ClassCastException if o is not of the expected type.
|
| public int | size()
Implements abstract java. Implements java. |
| EntrySet | back to summary |
|---|---|
| private EntrySet() | |
| add | back to summary |
|---|---|
| public boolean add(Map. Overrides java. Implements java. Doc from java. Adds the specified element to this set if it is not already present
(optional operation). More formally, adds the specified element
The stipulation above does not imply that sets must accept all
elements; sets may refuse to add any particular element, including
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides java. Implements java. Doc from java. Removes all of the elements from this set (optional operation). The set will be empty after this call returns.
| |
| contains | back to summary |
|---|---|
| public boolean contains(Object o) Overrides java. Implements java. Doc from java. Throws a Returns | |
| iterator | back to summary |
|---|---|
| public Iterator Implements abstract java. Implements java. Doc from java. Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee). | |
| remove | back to summary |
|---|---|
| public boolean remove(Object o) Overrides java. Implements java. Doc from java. Throws a Removes the specified element from this set if it is present
(optional operation). More formally, removes an element | |
| size | back to summary |
|---|---|
| public int size() Implements abstract java. Implements java. Doc from java. Returns the number of elements in this set (its cardinality). If this
set contains more than
| |