Package com.google.common.collect
Class MapMakerInternalMap<K,V,E extends MapMakerInternalMap.InternalEntry<K,V,E>,S extends MapMakerInternalMap.Segment<K,V,E,S>>
java.lang.Object
java.util.AbstractMap<K,V>
com.google.common.collect.MapMakerInternalMap<K,V,E,S>
- Type Parameters:
K
- the type of the keys in the mapV
- the type of the values in the mapE
- the type of theMapMakerInternalMap.InternalEntry
entry implementation used internallyS
- the type of theMapMakerInternalMap.Segment
entry implementation used internally
- All Implemented Interfaces:
Serializable
,ConcurrentMap<K,
,V> Map<K,
V>
class MapMakerInternalMap<K,V,E extends MapMakerInternalMap.InternalEntry<K,V,E>,S extends MapMakerInternalMap.Segment<K,V,E,S>>
extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable
The concurrent hash map implementation built by
MapMaker
.
This implementation is heavily derived from revision 1.96 of ConcurrentHashMap.java.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
The actual object that gets serialized.(package private) static class
Base class forMapMakerInternalMap.InternalEntry
implementations for strong keys.(package private) static class
Base class forMapMakerInternalMap.InternalEntry
implementations for weak keys.(package private) static final class
(package private) static final class
A dummy implementation ofMapMakerInternalMap.InternalEntry
, solely for use in the type signature ofUNSET_WEAK_VALUE_REFERENCE
below.(package private) final class
(package private) final class
(package private) class
(package private) static interface
An entry in a hash table of aMapMakerInternalMap.Segment
.(package private) static interface
MapMakerInternalMap.InternalEntryHelper<K,
V, E extends MapMakerInternalMap.InternalEntry<K, V, E>, S extends MapMakerInternalMap.Segment<K, V, E, S>> A helper object for operating onMapMakerInternalMap.InternalEntry
instances in a type-safe and efficient manner.(package private) final class
(package private) final class
(package private) static class
MapMakerInternalMap.Segment<K,
V, E extends MapMakerInternalMap.InternalEntry<K, V, E>, S extends MapMakerInternalMap.Segment<K, V, E, S>> Segments are specialized versions of hash tables.private static final class
The actual object that gets serialized.(package private) static enum
(package private) static class
Concrete implementation ofMapMakerInternalMap.InternalEntry
for strong keys andMapMaker.Dummy
values.(package private) static final class
Concrete implementation ofMapMakerInternalMap.Segment
for strong keys andMapMaker.Dummy
values.(package private) static class
Concrete implementation ofMapMakerInternalMap.InternalEntry
for strong keys and strong values.(package private) static final class
Concrete implementation ofMapMakerInternalMap.Segment
for strong keys and strong values.(package private) static class
Concrete implementation ofMapMakerInternalMap.InternalEntry
for strong keys and weak values.(package private) static final class
Concrete implementation ofMapMakerInternalMap.Segment
for strong keys and weak values.(package private) static interface
Marker interface forMapMakerInternalMap.InternalEntry
implementations for strong values.(package private) final class
(package private) final class
(package private) static class
Concrete implementation ofMapMakerInternalMap.InternalEntry
for weak keys andMapMaker.Dummy
values.(package private) static final class
Concrete implementation ofMapMakerInternalMap.Segment
for weak keys andMapMaker.Dummy
values.(package private) static class
Concrete implementation ofMapMakerInternalMap.InternalEntry
for weak keys and strong values.(package private) static final class
Concrete implementation ofMapMakerInternalMap.Segment
for weak keys and strong values.(package private) static class
Concrete implementation ofMapMakerInternalMap.InternalEntry
for weak keys and weak values.(package private) static final class
Concrete implementation ofMapMakerInternalMap.Segment
for weak keys and weak values.(package private) static interface
Marker interface forMapMakerInternalMap.InternalEntry
implementations for weak values.(package private) static interface
A weakly referenced value that also has a reference to its containing entry.(package private) static final class
Concrete implementation ofMapMakerInternalMap.WeakValueReference
.(package private) final class
Custom Entry class used by EntryIterator.next(), that relays setValue changes to the underlying map.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final int
The concurrency level.(package private) static final int
Number of (unsynchronized) retries in the containsValue method.(package private) static final int
Maximum number of entries to be drained in a single cleanup run.(package private) static final int
Number of cache access operations that can be buffered per segment before the cache's recency ordering information is updated.(package private) final MapMakerInternalMap.InternalEntryHelper
<K, V, E, S> Strategy for handling entries and segments in a type-safe and efficient manner.(package private) final Equivalence
<Object> Strategy for comparing keys.(package private) static final int
The maximum number of segments to allow; used to bound constructor arguments.(package private) static final int
The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments.(package private) final int
Mask value for indexing into segments.(package private) final MapMakerInternalMap.Segment<K,
V, E, S>[] The segments, each of which is a specialized hash table.(package private) final int
Shift value for indexing within segments.private static final long
(package private) static final MapMakerInternalMap.WeakValueReference
<Object, Object, MapMakerInternalMap.DummyInternalEntry> A singletonMapMakerInternalMap.WeakValueReference
used to denote an unset value in an entry with weak values.(package private) Collection
<V> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
MapMakerInternalMap
(MapMaker builder, MapMakerInternalMap.InternalEntryHelper<K, V, E, S> entryHelper) Creates a new, empty map with the specified strategy, initial capacity and concurrency level. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) (package private) E
This method is a convenience for testing.(package private) static <K,
V> MapMakerInternalMap <K, V, ? extends MapMakerInternalMap.InternalEntry<K, V, ?>, ?> Returns a freshMapMakerInternalMap
as specified by the givenbuilder
.(package private) MapMakerInternalMap.Segment
<K, V, E, S> createSegment
(int initialCapacity) (package private) static <K> MapMakerInternalMap
<K, MapMaker.Dummy, ? extends MapMakerInternalMap.InternalEntry<K, MapMaker.Dummy, ?>, ?> createWithDummyValues
(MapMaker builder) Returns a freshMapMakerInternalMap
withMapMaker.Dummy
values but otherwise as specified by the givenbuilder
.entrySet()
(package private) E
Returns the internal entry for the specified key.(package private) V
getLiveValue
(E entry) Gets the value from an entry.(package private) int
boolean
isEmpty()
(package private) boolean
isLiveForTesting
(MapMakerInternalMap.InternalEntry<K, V, ?> entry) This method is a convenience for testing.keySet()
(package private) MapMakerInternalMap.Strength
(package private) final MapMakerInternalMap.Segment<K,
V, E, S>[] newSegmentArray
(int ssize) void
putIfAbsent
(K key, V value) private void
(package private) void
reclaimKey
(E entry) (package private) void
reclaimValue
(MapMakerInternalMap.WeakValueReference<K, V, E> valueReference) (package private) static int
rehash
(int h) Applies a supplemental hash function to a given hash code, which defends against poor quality hash functions.boolean
boolean
(package private) MapMakerInternalMap.Segment
<K, V, E, S> segmentFor
(int hash) Returns the segment that should be used for a key with the given hash.int
size()
(package private) static <K,
V, E extends MapMakerInternalMap.InternalEntry<K, V, E>>
MapMakerInternalMap.WeakValueReference<K, V, E> (package private) Equivalence
<Object> values()
(package private) MapMakerInternalMap.Strength
(package private) Object
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
-
Field Details
-
MAXIMUM_CAPACITY
static final int MAXIMUM_CAPACITYThe maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments. MUST be a power of two no greater than1<<30
to ensure that entries are indexable using ints.- See Also:
-
MAX_SEGMENTS
static final int MAX_SEGMENTSThe maximum number of segments to allow; used to bound constructor arguments.- See Also:
-
CONTAINS_VALUE_RETRIES
static final int CONTAINS_VALUE_RETRIESNumber of (unsynchronized) retries in the containsValue method.- See Also:
-
DRAIN_THRESHOLD
static final int DRAIN_THRESHOLDNumber of cache access operations that can be buffered per segment before the cache's recency ordering information is updated. This is used to avoid lock contention by recording a memento of reads and delaying a lock acquisition until the threshold is crossed or a mutation occurs.This must be a (2^n)-1 as it is used as a mask.
- See Also:
-
DRAIN_MAX
static final int DRAIN_MAXMaximum number of entries to be drained in a single cleanup run. This applies independently to the cleanup queue and both reference queues.- See Also:
-
segmentMask
final transient int segmentMaskMask value for indexing into segments. The upper bits of a key's hash code are used to choose the segment. -
segmentShift
final transient int segmentShiftShift value for indexing within segments. Helps prevent entries that end up in the same segment from also ending up in the same bucket. -
segments
final transient MapMakerInternalMap.Segment<K,V, segmentsE extends MapMakerInternalMap.InternalEntry<K, V, E>, S extends MapMakerInternalMap.Segment<K, V, E, S>>[] The segments, each of which is a specialized hash table. -
concurrencyLevel
final int concurrencyLevelThe concurrency level. -
keyEquivalence
Strategy for comparing keys. -
entryHelper
final transient MapMakerInternalMap.InternalEntryHelper<K,V, entryHelperE extends MapMakerInternalMap.InternalEntry<K, V, E>, S extends MapMakerInternalMap.Segment<K, V, E, S>> Strategy for handling entries and segments in a type-safe and efficient manner. -
UNSET_WEAK_VALUE_REFERENCE
static final MapMakerInternalMap.WeakValueReference<Object,Object, UNSET_WEAK_VALUE_REFERENCEMapMakerInternalMap.DummyInternalEntry> A singletonMapMakerInternalMap.WeakValueReference
used to denote an unset value in an entry with weak values. -
keySet
-
values
-
entrySet
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
Method Details
-
create
static <K,V> MapMakerInternalMap<K,V, create? extends MapMakerInternalMap.InternalEntry<K, V, ?>, ?> (MapMaker builder) Returns a freshMapMakerInternalMap
as specified by the givenbuilder
. -
createWithDummyValues
static <K> MapMakerInternalMap<K,MapMaker.Dummy, createWithDummyValues? extends MapMakerInternalMap.InternalEntry<K, MapMaker.Dummy, ?>, ?> (MapMaker builder) Returns a freshMapMakerInternalMap
withMapMaker.Dummy
values but otherwise as specified by the givenbuilder
. The returnedMapMakerInternalMap
will be optimized to saved memory. SinceMapMaker.Dummy
is a singleton, we don't need to store any values at all. Because of this optimization,build.getValueStrength()
must beMapMakerInternalMap.Strength.STRONG
.This method is intended to only be used by the internal implementation of
Interners
, since a map of dummy values is the exact use case there. -
unsetWeakValueReference
static <K,V, MapMakerInternalMap.WeakValueReference<K,E extends MapMakerInternalMap.InternalEntry<K, V, E>> V, unsetWeakValueReference()E> -
rehash
static int rehash(int h) Applies a supplemental hash function to a given hash code, which defends against poor quality hash functions. This is critical when the concurrent hash map uses power-of-two length hash tables, that otherwise encounter collisions for hash codes that do not differ in lower or upper bits.- Parameters:
h
- hash code
-
copyEntry
This method is a convenience for testing. Code should callMapMakerInternalMap.Segment.copyEntry(E, E)
directly. -
hash
-
reclaimValue
-
reclaimKey
-
isLiveForTesting
This method is a convenience for testing. Code should callMapMakerInternalMap.Segment.getLiveValue(E)
instead. -
segmentFor
Returns the segment that should be used for a key with the given hash.- Parameters:
hash
- the hash code for the key- Returns:
- the segment
-
createSegment
-
getLiveValue
Gets the value from an entry. Returnsnull
if the entry is invalid, partially-collected or computing. -
newSegmentArray
-
keyStrength
MapMakerInternalMap.Strength keyStrength() -
valueStrength
MapMakerInternalMap.Strength valueStrength() -
valueEquivalence
Equivalence<Object> valueEquivalence() -
isEmpty
public boolean isEmpty() -
size
public int size() -
get
-
getEntry
Returns the internal entry for the specified key. The entry may be computing or partially collected. Does not impact recency ordering. -
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Overrides:
containsKey
in classAbstractMap<K,
V>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V> - Overrides:
containsValue
in classAbstractMap<K,
V>
-
put
-
putIfAbsent
- Specified by:
putIfAbsent
in interfaceConcurrentMap<K,
V> - Specified by:
putIfAbsent
in interfaceMap<K,
V>
-
putAll
-
remove
-
remove
-
replace
-
replace
-
clear
public void clear() -
keySet
-
values
-
entrySet
-
writeReplace
Object writeReplace() -
readObject
- Throws:
InvalidObjectException
-