Class ObjectIdOwnerMap<V extends ObjectIdOwnerMap.Entry>
- Type Parameters:
V
- type of subclass of ObjectId that will be stored in the map.
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescription(package private) int
Number of low bits used to form the index intodirectory
.(package private) V[][]
Top level directory of the segments.private int
The map doubles in capacity whensize
reaches this target.private static final int
Size of the initial directory, will grow as necessary.private int
Low bit mask to index intodirectory
,2^bits-1
.private static final int
Number of bits in a segment's index.private static final int
(package private) int
Total number of objects in this map. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<Q extends V>
voidadd
(Q newValue) Store an object for future lookup.addIfAbsent
(Q newValue) Store an object for future lookup.void
clear()
Remove all entries from this map.private static final int
computeGrowAt
(int bits) boolean
contains
(AnyObjectId toFind) Returns true if the objectId is contained within the collection.private static final boolean
equals
(AnyObjectId firstObjectId, AnyObjectId secondObjectId) get
(AnyObjectId toFind) Lookup an existing mapping.private void
grow()
boolean
isEmpty()
Whether this map is emptyiterator()
private final V[]
int
size()
Get number of objects in this map.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
INITIAL_DIRECTORY
private static final int INITIAL_DIRECTORYSize of the initial directory, will grow as necessary.- See Also:
-
SEGMENT_BITS
private static final int SEGMENT_BITSNumber of bits in a segment's index. Segments are 2^11 in size.- See Also:
-
SEGMENT_SHIFT
private static final int SEGMENT_SHIFT- See Also:
-
directory
V extends ObjectIdOwnerMap.Entry[][] directoryTop level directory of the segments.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. -
size
int sizeTotal number of objects in this map. -
grow
private int growThe map doubles in capacity whensize
reaches this target. -
bits
int bitsNumber of low bits used to form the index intodirectory
. -
mask
private int maskLow bit mask to index intodirectory
,2^bits-1
.
-
-
Constructor Details
-
ObjectIdOwnerMap
public ObjectIdOwnerMap()Create an empty map.
-
-
Method Details
-
clear
public void clear()Remove all entries from this map. -
get
Lookup an existing mapping.- Parameters:
toFind
- the object identifier to find.- Returns:
- the instance mapped to toFind, or null if no mapping exists.
-
contains
Returns true if the objectId is contained within the collection.Returns true if this map contains the specified object.
- Specified by:
contains
in interfaceObjectIdSet
- Parameters:
toFind
- the objectId to find- Returns:
- whether the collection contains the objectId.
-
add
Store an object for future lookup.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 useaddIfAbsent(Entry)
.- Parameters:
newValue
- the object to store.
-
addIfAbsent
Store an object for future lookup.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;
- Parameters:
newValue
- the object to store.- Returns:
newValue
if stored, or the prior value already stored and that would have been returned had the caller usedget(newValue)
first.
-
size
public int size()Get number of objects in this map.- Returns:
- number of objects in this map.
-
isEmpty
public boolean isEmpty()Whether this map is empty- Returns:
- true if
size()
is 0.
-
iterator
- Specified by:
iterator
in interfaceIterable<V extends ObjectIdOwnerMap.Entry>
-
grow
private void grow() -
newSegment
-
computeGrowAt
private static final int computeGrowAt(int bits) -
equals
-