Package com.tangosol.util
Class AbstractKeyBasedMap<K,V>
- java.lang.Object
-
- com.tangosol.util.Base
-
- com.tangosol.util.AbstractKeyBasedMap<K,V>
-
- All Implemented Interfaces:
Map<K,V>
- Direct Known Subclasses:
AbstractKeySetBasedMap
,CacheStoreMap.ReadWriteMap
,KeyValueArrayMap
,OpenHashMap
,PartitionSplittingBackingMap
,PartitionSplittingBackingMap.MaskedPartitionMap
,ReadWriteBackingMap.EvictingBackupMap
,SimpleOverflowMap
,SimpleSerializationMap
public abstract class AbstractKeyBasedMap<K,V> extends Base implements Map<K,V>
AbstractKeyBasedMap is a base class for Map implementations. The primary difference between theAbstractMap
abstract class and this abstract class is this: that AbstractMap requires a sub-class to provide an Entry Set implementation, while AbstractKeyBasedMap requires a read-only sub-class to implement only get() and iterateKeys(), and a read-write sub-class to additionally implement only put() and remove().Read-only implementations must implement
iterateKeys()
andget(Object)
. Read/write implementations must additionally implementput(Object, Object)
andremove(Object)
. A number of the methods have implementations provided, but are extremely inefficient for Maps that contain large amounts of data, includingclear()
,containsKey(Object)
,size()
(and by extensionisEmpty()
). Furthermore, if any of a number of method implementations has any cost of returning an "old value", such as is done by theput(K, V)
andremove(Object)
methods, then theputAll(java.util.Map)
andremoveBlind(Object)
methods should also be implemented.- Author:
- 2005.07.13 cp
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractKeyBasedMap.DeferredCacheEvent<K,V>
A DeferredCacheEvent is aCacheEvent
object that defers the loading of theold value
.class
AbstractKeyBasedMap.EntrySet
A set of entries backed by this map.protected class
AbstractKeyBasedMap.KeySet
A set of keys backed by this map.protected class
AbstractKeyBasedMap.ValuesCollection
A Collection of values backed by this map.-
Nested classes/interfaces inherited from class com.tangosol.util.Base
Base.LoggingWriter
-
-
Constructor Summary
Constructors Constructor Description AbstractKeyBasedMap()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
clear()
Clear all key/value mappings.protected Object
clone()
Returns a shallow copy of this AbstractKeyBasedMap instance; the keySet, entrySet and values collections are not cloned or copied to (shared by) the clone.boolean
containsKey(Object oKey)
Returns true if this map contains a mapping for the specified key.boolean
containsValue(Object oValue)
Returns true if this Map maps one or more keys to the specified value.Set<Map.Entry<K,V>>
entrySet()
Returns a set view of the mappings contained in this map.boolean
equals(Object o)
Compares the specified object with this map for equality.abstract V
get(Object oKey)
Returns the value to which this map maps the specified key.Map<K,V>
getAll(Collection<? extends K> colKeys)
Get all the specified keys, if they are in the Map.int
hashCode()
Returns the hash code value for this Map.protected Set<Map.Entry<K,V>>
instantiateEntrySet()
Factory pattern: Create a Set that represents the entries in the Map.protected Set<K>
instantiateKeySet()
Factory pattern: Create a Set that represents the keys in the Mapprotected Collection<V>
instantiateValues()
Factory pattern: Instantiate the values Collection.boolean
isEmpty()
Returns true if this map contains no key-value mappings.protected abstract Iterator<K>
iterateKeys()
Create an iterator over the keys in this Map.Set<K>
keySet()
Returns a set view of the keys contained in this map.V
put(K oKey, V oValue)
Associates the specified value with the specified key in this map.void
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map.V
remove(Object oKey)
Removes the mapping for this key from this map if present.protected boolean
removeBlind(Object oKey)
Removes the mapping for this key from this map if present.int
size()
Returns the number of key-value mappings in this map.String
toString()
Returns a string representation of this Map.Collection<V>
values()
Returns a collection view of the values contained in this map.-
Methods inherited from class com.tangosol.util.Base
azzert, azzert, azzert, azzertFailed, breakLines, breakLines, capitalize, checkNotEmpty, checkNotNull, checkRange, computeSafeWaitTime, decimalValue, dup, dup, ensureBigDecimal, ensureClassLoader, ensureRuntimeException, ensureRuntimeException, equals, equalsDeep, err, err, err, err, err, escape, formatDateTime, getCallerStackFrame, getCommonMonitor, getCommonMonitor, getCommonMonitor, getContextClassLoader, getContextClassLoader, getDeepMessage, getErr, getLastSafeTimeMillis, getLog, getMaxDecDigits, getMaxHexDigits, getOriginalException, getOut, getProcessRandom, getRandom, getRandomBinary, getRandomBinary, getRandomString, getSafeTimeMillis, getStackFrame, getStackFrames, getStackTrace, getStackTrace, getStackTrace, getThreadFactory, getTimeZone, getUpTimeMillis, hashCode, hexValue, indentString, indentString, isDecimal, isHex, isLogEcho, isOctal, log, log, log, log, log, makeInteger, makeLong, makeThread, mergeArray, mergeBooleanArray, mergeByteArray, mergeCharArray, mergeDoubleArray, mergeFloatArray, mergeIntArray, mergeLongArray, mod, mod, octalValue, out, out, out, out, out, pad, parseBandwidth, parseBandwidth, parseDelimitedString, parseHex, parseHex, parseMemorySize, parseMemorySize, parsePercentage, parseTime, parseTime, parseTimeNanos, parseTimeNanos, printStackTrace, randomize, randomize, randomize, randomize, read, read, read, read, read, read, read, replace, setErr, setLog, setLogEcho, setOut, sleep, toBandwidthString, toBandwidthString, toCharEscape, toCrc, toCrc, toCrc, toCrc, toCrc, toDecString, toDelimitedString, toDelimitedString, toDelimitedString, toDelimitedString, toHex, toHex, toHexDump, toHexEscape, toHexEscape, toHexEscape, toHexEscape, toHexString, toMemorySizeString, toMemorySizeString, toQuotedCharEscape, toQuotedStringEscape, toSqlString, toString, toString, toStringEscape, toUnicodeEscape, trace, trace, trace, trace, trace, trace, trace, trace, trace, truncateString, truncateString, wait
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Method Detail
-
containsKey
public boolean containsKey(Object oKey)
Returns true if this map contains a mapping for the specified key.- Specified by:
containsKey
in interfaceMap<K,V>
- Returns:
- true if this map contains a mapping for the specified key, false otherwise.
-
containsValue
public boolean containsValue(Object oValue)
Returns true if this Map maps one or more keys to the specified value.- Specified by:
containsValue
in interfaceMap<K,V>
- Returns:
- true if this Map maps one or more keys to the specified value, false otherwise
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Returns a set view of the mappings contained in this map. Each element in the returned set is anMap.Entry
. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except by the iterator's own remove operation, or by the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It is not expected to support the add or addAll operations.
-
isEmpty
public boolean isEmpty()
Returns true if this map contains no key-value mappings.
-
keySet
public Set<K> keySet()
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll retainAll, and clear operations. It is not expected to support the add or addAll operations.
-
put
public V put(K oKey, V oValue)
Associates the specified value with the specified key in this map.
-
putAll
public void putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of callingput(K, V)
on this map once for each mapping in the passed map. The behavior of this operation is unspecified if the passed map is modified while the operation is in progress.
-
remove
public V remove(Object oKey)
Removes the mapping for this key from this map if present. Expensive: updates both the underlying cache and the local cache.- Specified by:
remove
in interfaceMap<K,V>
- Parameters:
oKey
- key whose mapping is to be removed from the map- Returns:
- previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.
-
size
public int size()
Returns the number of key-value mappings in this map.
-
values
public Collection<V> values()
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It is not expected to support the add or addAll operations.
-
getAll
public Map<K,V> getAll(Collection<? extends K> colKeys)
Get all the specified keys, if they are in the Map. For each key that is in the cache, that key and its corresponding value will be placed in the map that is returned by this method. The absence of a key in the returned map indicates that it was not in the cache, which may imply (for caches that can load behind the scenes) that the requested data could not be loaded.- Parameters:
colKeys
- a collection of keys that may be in the named cache- Returns:
- a Map of keys to values for the specified keys passed in colKeys
-
iterateKeys
protected abstract Iterator<K> iterateKeys()
Create an iterator over the keys in this Map. The Iterator must support remove() if the Map supports removal.- Returns:
- a new instance of an Iterator over the keys in this Map
-
removeBlind
protected boolean removeBlind(Object oKey)
Removes the mapping for this key from this map if present. This method exists to allow sub-classes to optimize remove functionality for situations in which the original value is not required.- Parameters:
oKey
- key whose mapping is to be removed from the map- Returns:
- true iff the Map changed as the result of this operation
-
equals
public boolean equals(Object o)
Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps t1 and t2 represent the same mappings if t1.keySet().equals(t2.keySet()) and for every key k in t1.keySet(), (t1.get(k)==null ? t2.get(k)==null : t1.get(k).equals(t2.get(k))) . This ensures that the equals method works properly across different implementations of the map interface.
-
hashCode
public int hashCode()
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 entrySet() view. This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any two maps t1 and t2, as required by the general contract of Object.hashCode.
-
toString
public String toString()
Returns a string representation of this Map. The string representation consists of a list of key-value mappings in the order returned by the Map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as by String.valueOf(Object).
-
clone
protected Object clone() throws CloneNotSupportedException
Returns a shallow copy of this AbstractKeyBasedMap instance; the keySet, entrySet and values collections are not cloned or copied to (shared by) the clone.- Overrides:
clone
in classObject
- Returns:
- a shallow copy of this map
- Throws:
CloneNotSupportedException
-
instantiateKeySet
protected Set<K> instantiateKeySet()
Factory pattern: Create a Set that represents the keys in the Map- Returns:
- a new instance of Set that represents the keys in the Map
-
instantiateEntrySet
protected Set<Map.Entry<K,V>> instantiateEntrySet()
Factory pattern: Create a Set that represents the entries in the Map.- Returns:
- a new instance of Set that represents the entries in the Map
-
instantiateValues
protected Collection<V> instantiateValues()
Factory pattern: Instantiate the values Collection.- Returns:
- a new instance of Collection that represents this Map's values
-
-