Class Int2ObjectCache<V>
- Type Parameters:
V- type of values stored in theMap
The eviction strategy is to remove the oldest in a set if the key is not found, or if found then that item. The newly inserted item becomes the youngest in the set. Sets are evicted on a first in, first out, manner unless replacing a matching key.
A good set size would be in the range of 2 to 16 so that the references/keys can fit in a cache-line (assuming references are 32-bit references and 64-byte cache lines, YMMV). A linear search within a cache line is much less costly than a cache-miss to another line.
Null values are not supported by this cache.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classIterator over entries which supports access to unboxed keys viaInt2ObjectCache.EntryIterator.getIntKey().final classSet of entries which supports cached iterator to avoid allocation.final classIterator over keys which supports access to unboxed keys viaInt2ObjectCache.KeyIterator.nextInt().final classA key set implementation which supports cached iterator to avoid allocation.final classCollection of values which supports cached iterator to avoid allocation.final classAn iterator over values. -
Constructor Summary
ConstructorsConstructorDescriptionInt2ObjectCache(int numSets, int setSize, Consumer<V> evictionConsumer) Constructs cache with provided configuration. -
Method Summary
Modifier and TypeMethodDescriptionlongThe number of times a cache hit has occurred on theget(int)method.longThe number of times a cache miss has occurred on theget(int)method.longThe number of items that have been put in the cache.intcapacity()Get the total capacity for the map to which the load factor will be a fraction of.voidclear()Clear down all items in the cache.compute(int key, IntObjectToObjectFunction<? super V, ? extends V> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping).computeIfAbsent(int key, IntFunction<? extends V> mappingFunction) Get a value for a given key, or if it does ot exist then default the value via aIntFunctionand put it in the cache.computeIfAbsent(Integer key, Function<? super Integer, ? extends V> mappingFunction) computeIfPresent(int key, IntObjectToObjectFunction<? super V, ? extends V> remappingFunction) If the value for the specified key is present attempts to compute a new mapping given the key and its current mapped value.computeIfPresent(Integer key, BiFunction<? super Integer, ? super V, ? extends V> remappingFunction) booleancontainsKey(int key) Overloaded version ofMap.containsKey(Object)that takes a primitive int key.booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleanvoidforEach(BiConsumer<? super Integer, ? super V> action) voidforEachInt(IntObjConsumer<? super V> consumer) Implementation of theforEach(BiConsumer)that avoids boxing of keys.get(int key) Overloaded version ofMap.get(Object)that takes a primitive int key.getOrDefault(int key, V defaultValue) Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.inthashCode()booleanisEmpty()keySet()If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.Overloaded version ofMap.put(Object, Object)that takes a primitive int key.voidvoidputAll(Int2ObjectCache<? extends V> map) Put all values from the given map into this one without allocation.putIfAbsent(int key, V value) If the specified key is not already associated with a value (or is mapped tonull) associates it with the given value and returnsnull, else returns the current value.putIfAbsent(Integer key, V value) remove(int key) Overloaded version ofMap.remove(Object)that takes a primitive int key.booleanRemoves the entry for the specified key only if it is currently mapped to the specified value.booleanReplaces the entry for the specified key only if it is currently mapped to some value.booleanReplaces the entry for the specified key only if currently mapped to the specified value.booleanvoidreplaceAll(BiFunction<? super Integer, ? super V, ? extends V> function) voidreplaceAllInt(IntObjectToObjectFunction<? super V, ? extends V> function) Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.voidReset the cache statistics counters to zero.intsize()toString()values()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
getOrDefault
-
Constructor Details
-
Int2ObjectCache
Constructs cache with provided configuration.- Parameters:
numSets- number of sets, must be power or two.setSize- size of a single set, must be power or two.evictionConsumer- consumer to be notified when entry is being evicted from the cache.
-
-
Method Details
-
cacheHits
public long cacheHits()The number of times a cache hit has occurred on theget(int)method.- Returns:
- the number of times a cache hit has occurred on the
get(int)method.
-
cacheMisses
public long cacheMisses()The number of times a cache miss has occurred on theget(int)method.- Returns:
- the number of times a cache miss has occurred on the
get(int)method.
-
cachePuts
public long cachePuts()The number of items that have been put in the cache.- Returns:
- number of items that have been put in the cache.
-
resetCounters
public void resetCounters()Reset the cache statistics counters to zero. -
capacity
public int capacity()Get the total capacity for the map to which the load factor will be a fraction of.- Returns:
- the total capacity for the map.
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKeyin interfaceMap<Integer,V>
-
containsKey
public boolean containsKey(int key) Overloaded version ofMap.containsKey(Object)that takes a primitive int key.- Parameters:
key- for indexing theMap- Returns:
- true if the key is found otherwise false.
-
containsValue
- Specified by:
containsValuein interfaceMap<Integer,V>
-
get
-
get
Overloaded version ofMap.get(Object)that takes a primitive int key.- Parameters:
key- for indexing theMap- Returns:
- the value if found otherwise null
-
getOrDefault
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.- Parameters:
key- whose associated value is to be returned.defaultValue- the default mapping of the key.- Returns:
- the value to which the specified key is mapped, or
defaultValueif this map contains no mapping for the key.
-
forEach
-
forEachInt
Implementation of theforEach(BiConsumer)that avoids boxing of keys.- Parameters:
consumer- to be called for each key/value pair.
-
computeIfAbsent
- Specified by:
computeIfAbsentin interfaceMap<Integer,V>
-
computeIfAbsent
Get a value for a given key, or if it does ot exist then default the value via aIntFunctionand put it in the cache.Primitive specialized version of
Map.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>).- Parameters:
key- to search on.mappingFunction- to provide a value if the get returns null.- Returns:
- the value if found otherwise the default.
-
computeIfPresent
public V computeIfPresent(Integer key, BiFunction<? super Integer, ? super V, ? extends V> remappingFunction) - Specified by:
computeIfPresentin interfaceMap<Integer,V>
-
computeIfPresent
public V computeIfPresent(int key, IntObjectToObjectFunction<? super V, ? extends V> remappingFunction) If the value for the specified key is present attempts to compute a new mapping given the key and its current mapped value.Primitive specialized version of
Map.computeIfPresent(Object, BiFunction).- Parameters:
key- with which the specified value is to be associated.remappingFunction- the function to compute a value.- Returns:
- the new value associated with the specified key, or null if none.
-
compute
-
compute
Attempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping).Primitive specialized version of
Map.compute(Object, BiFunction).- Parameters:
key- with which the specified value is to be associated.remappingFunction- the function to compute a value.- Returns:
- the new value associated with the specified key, or null if none.
-
merge
-
merge
If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result isnull.Primitive specialized version of
Map.merge(Object, Object, BiFunction).- Parameters:
key- with which the resulting value is to be associated.value- the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key.remappingFunction- the function to recompute a value if present.- Returns:
- the new value associated with the specified key, or null if no value is associated with the key.
-
putIfAbsent
- Specified by:
putIfAbsentin interfaceMap<Integer,V>
-
putIfAbsent
If the specified key is not already associated with a value (or is mapped tonull) associates it with the given value and returnsnull, else returns the current value.Primitive specialized version of
Map.putIfAbsent(Object, Object).- Parameters:
key- with which the specified value is to be associated.value- to be associated with the specified key.- Returns:
- the previous value associated with the specified key, or
nullif there was no mapping for the key.
-
put
-
put
Overloaded version ofMap.put(Object, Object)that takes a primitive int key. -
remove
-
remove
Removes the entry for the specified key only if it is currently mapped to the specified value.Primitive specialized version of
Map.remove(Object, Object).- Parameters:
key- key with which the specified value is associated.value- expected to be associated with the specified key.- Returns:
trueif the value was removed.
-
remove
-
remove
Overloaded version ofMap.remove(Object)that takes a primitive int key.- Parameters:
key- for indexing theMap- Returns:
- the value if found otherwise null
-
replace
-
replace
Replaces the entry for the specified key only if currently mapped to the specified value.Primitive specialized version of
Map.replace(Object, Object, Object).- Parameters:
key- with which the specified value is associated.oldValue- expected to be associated with the specified key.newValue- to be associated with the specified key.- Returns:
trueif the value was replaced.
-
replace
-
replace
Replaces the entry for the specified key only if it is currently mapped to some value.Primitive specialized version of
Map.replace(Object, Object).- Parameters:
key- with which the specified value is associated.value- to be associated with the specified key.- Returns:
- the previous value associated with the specified key.
-
replaceAll
- Specified by:
replaceAllin interfaceMap<Integer,V>
-
replaceAllInt
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.Primitive specialized version of
Map.replaceAll(BiFunction).NB: Renamed from forEach to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.
- Parameters:
function- the function to apply to each entry.
-
clear
public void clear()Clear down all items in the cache.If an exception occurs during the eviction function callback then clear may need to be called again to complete.
If an exception occurs the cache should only be used when
size()reports zero. -
putAll
-
putAll
Put all values from the given map into this one without allocation.- Parameters:
map- whose value are to be added.
-
keySet
-
values
-
entrySet
-
toString
-
equals
-
hashCode
public int hashCode()
-