V
- type of subclass of ObjectId that will be stored in the map.public class ObjectIdOwnerMap<V extends ObjectIdOwnerMap.Entry> extends java.lang.Object implements java.lang.Iterable<V>, ObjectIdSet
ObjectId
subclasses in
only one map.
To use this map type, applications must have their entry value type extend
from ObjectIdOwnerMap.Entry
, which itself
extends from ObjectId.
Object instances may only be stored in ONE ObjectIdOwnerMap. This restriction exists because the map stores internal map state within each object instance. If an instance is be placed in another ObjectIdOwnerMap it could corrupt one or both map's internal state.
If an object instance must be in more than one map, applications may use
ObjectIdOwnerMap for one of the maps, and
ObjectIdSubclassMap
for the other map(s). It is
encouraged to use ObjectIdOwnerMap for the map that is accessed most often,
as this implementation runs faster than the more general ObjectIdSubclassMap
implementation.
Modifier and Type | Class and Description |
---|---|
static class |
ObjectIdOwnerMap.Entry
Type of entry stored in the
ObjectIdOwnerMap . |
Modifier and Type | Field and Description |
---|---|
(package private) int |
bits
Number of low bits used to form the index into
directory . |
(package private) V[][] |
directory
Top level directory of the segments.
|
private int |
grow
The map doubles in capacity when
size reaches this target. |
private static int |
INITIAL_DIRECTORY
Size of the initial directory, will grow as necessary.
|
private int |
mask
Low bit mask to index into
directory , 2^bits-1 . |
private static int |
SEGMENT_BITS
Number of bits in a segment's index.
|
private static int |
SEGMENT_SHIFT |
(package private) int |
size
Total number of objects in this map.
|
Constructor and Description |
---|
ObjectIdOwnerMap()
Create an empty map.
|
Modifier and Type | Method and Description |
---|---|
<Q extends V> |
add(Q newValue)
Store an object for future lookup.
|
<Q extends V> |
addIfAbsent(Q newValue)
Store an object for future lookup.
|
void |
clear()
Remove all entries from this map.
|
private static int |
computeGrowAt(int bits) |
boolean |
contains(AnyObjectId toFind)
Returns true if the objectId is contained within the collection.
|
private static boolean |
equals(AnyObjectId firstObjectId,
AnyObjectId secondObjectId) |
V |
get(AnyObjectId toFind)
Lookup an existing mapping.
|
private void |
grow() |
boolean |
isEmpty()
Whether this map is empty
|
java.util.Iterator<V> |
iterator() |
private V[] |
newSegment() |
int |
size()
Get number of objects in this map.
|
private static final int INITIAL_DIRECTORY
private static final int SEGMENT_BITS
private static final int SEGMENT_SHIFT
V extends ObjectIdOwnerMap.Entry[][] directory
The low bits
of the SHA-1 are used to select the segment from
this directory. Each segment is constant sized at 2^SEGMENT_BITS.
int size
private int grow
size
reaches this target.int bits
directory
.private int mask
directory
, 2^bits-1
.public void clear()
public V get(AnyObjectId toFind)
toFind
- the object identifier to find.public boolean contains(AnyObjectId toFind)
Returns true if this map contains the specified object.
contains
in interface ObjectIdSet
toFind
- the objectId to findpublic <Q extends V> void add(Q newValue)
An existing mapping for must not be in this map. Callers must
first call get(AnyObjectId)
to verify there is no current
mapping prior to adding a new mapping, or use addIfAbsent(Entry)
.
newValue
- the object to store.public <Q extends V> V addIfAbsent(Q newValue)
Stores newValue
, but only if there is not already an object for
the same object name. Callers can tell if the value is new by checking
the return value with reference equality:
V obj = ...; boolean wasNew = map.addIfAbsent(obj) == obj;
newValue
- the object to store.newValue
if stored, or the prior value already stored and
that would have been returned had the caller used
get(newValue)
first.public int size()
public boolean isEmpty()
size()
is 0.public java.util.Iterator<V> iterator()
iterator
in interface java.lang.Iterable<V extends ObjectIdOwnerMap.Entry>
private void grow()
private final V[] newSegment()
private static final int computeGrowAt(int bits)
private static final boolean equals(AnyObjectId firstObjectId, AnyObjectId secondObjectId)