- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- org.ojalgo.type.ForgetfulMap<K,V>
-
- All Implemented Interfaces:
java.util.Map<K,V>
public final class ForgetfulMap<K,V> extends java.util.AbstractMap<K,V>AMapthat can forget entries after a specified period of time – a cache in other words. The entries can either expire after a certain amount of time has passed since they were created/written or since they were last accessed.In addition to implementing the
Mapinterface, also looked at the Cache interface of the Caffeine library, and mimicked some of its methods.Furthermore, this class provides a re-implementation of
TypeCache.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classForgetfulMap.Builderprivate static classForgetfulMap.CachedValue<V>Helper class to store value and timestampsstatic interfaceForgetfulMap.ValueCache<V>The specifications ofTypeCacheto allow for another implementation.private static classForgetfulMap.ValueCacheImpl<K,V>A re-implementation ofTypeCachebacked by aForgetfulMap.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.concurrent.ScheduledExecutorServiceCLEANERprivate longmyAccessLimitprivate java.util.function.Consumer<V>myDisposerprivate java.util.Map<K,ForgetfulMap.CachedValue<V>>myStorageprivate longmyWriteLimit
-
Constructor Summary
Constructors Constructor Description ForgetfulMap(ForgetfulMap.Builder builder)ForgetfulMap(ForgetfulMap.Builder builder, java.util.function.Consumer<V> disposer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcleanUp()Removes all entries that have expired.voidclear()booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)java.util.Set<java.util.Map.Entry<K,V>>entrySet()booleanequals(java.lang.Object obj)longestimatedSize()Returns the approximate number of entries in this cache.Vget(java.lang.Object key)Vget(K key, java.util.function.Function<? super K,? extends V> mappingFunction)Returns the value associated with thekeyin this cache, obtaining that value from themappingFunctionif necessary.java.util.Map<K,V>getAllPresent(java.lang.Iterable<? extends K> keys)Returns a map of the values associated with thekeysin this cache.VgetIfPresent(K key)Returns the value associated with thekeyin this cache, ornullif there is no cached value for thekey.inthashCode()voidinvalidate(K key)Discards any cached value for thekey.voidinvalidateAll()Discards all entries in the cache.voidinvalidateAll(java.lang.Iterable<? extends K> keys)Discards any cached values for thekeys.booleanisEmpty()private booleanisExpired(long now, ForgetfulMap.CachedValue<V> cached)java.util.Set<K>keySet()static ForgetfulMap.BuildernewBuilder()ForgetfulMap.ValueCache<V>newValueCache(K key, java.util.function.Function<? super K,? extends V> valueSupplier)Vput(K key, V value)voidputAll(java.util.Map<? extends K,? extends V> map)Copies all of the mappings from the specified map to the cache.Vremove(java.lang.Object key)booleanremove(java.lang.Object key, java.lang.Object value)intsize()private java.util.Map.Entry<K,V>unwrap(java.util.Map.Entry<K,ForgetfulMap.CachedValue<V>> entry)
-
-
-
Field Detail
-
CLEANER
private static final java.util.concurrent.ScheduledExecutorService CLEANER
-
myAccessLimit
private final long myAccessLimit
-
myDisposer
private final java.util.function.Consumer<V> myDisposer
-
myStorage
private final java.util.Map<K,ForgetfulMap.CachedValue<V>> myStorage
-
myWriteLimit
private final long myWriteLimit
-
-
Constructor Detail
-
ForgetfulMap
ForgetfulMap(ForgetfulMap.Builder builder)
-
ForgetfulMap
ForgetfulMap(ForgetfulMap.Builder builder, java.util.function.Consumer<V> disposer)
-
-
Method Detail
-
newBuilder
public static ForgetfulMap.Builder newBuilder()
-
cleanUp
public void cleanUp()
Removes all entries that have expired. This method is automatically called at regular intervals, but can also be called manually to remove entries that have expired since the last cleanup.
-
clear
public void clear()
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
equals
public boolean equals(java.lang.Object obj)
-
estimatedSize
public long estimatedSize()
Returns the approximate number of entries in this cache. The value returned is an estimate; the actual count may differ if there are concurrent insertions or removals, or if some entries are pending removal due to expiration or weak/soft reference collection. In the case of stale entries this inaccuracy can be mitigated by performing acleanUp()first.
-
get
public V get(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
Returns the value associated with thekeyin this cache, obtaining that value from themappingFunctionif necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache and return" pattern.If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this cache unless
null. The entire method invocation is performed atomically, so the function is applied at most once per key. Some attempted update operations on this cache by other threads may be blocked while the computation is in progress, so the computation should be short and simple, and must not attempt to update any other mappings of this cache.
-
get
public V get(java.lang.Object key)
-
getAllPresent
public java.util.Map<K,V> getAllPresent(java.lang.Iterable<? extends K> keys)
Returns a map of the values associated with thekeysin this cache. The returned map will only contain entries which are already present in the cache.Note that duplicate elements in
keys, as determined byObject.equals(java.lang.Object), will be ignored.
-
getIfPresent
public V getIfPresent(K key)
Returns the value associated with thekeyin this cache, ornullif there is no cached value for thekey.
-
hashCode
public int hashCode()
-
invalidate
public void invalidate(K key)
Discards any cached value for thekey. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present.
-
invalidateAll
public void invalidateAll()
Discards all entries in the cache. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present.
-
invalidateAll
public void invalidateAll(java.lang.Iterable<? extends K> keys)
Discards any cached values for thekeys. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present.
-
isEmpty
public boolean isEmpty()
-
keySet
public java.util.Set<K> keySet()
-
newValueCache
public ForgetfulMap.ValueCache<V> newValueCache(K key, java.util.function.Function<? super K,? extends V> valueSupplier)
-
putAll
public void putAll(java.util.Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to the cache. The effect of this call is equivalent to that of callingput(k, v)on this map once for each mapping from keykto valuevin the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
-
remove
public V remove(java.lang.Object key)
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
-
size
public int size()
-
isExpired
private boolean isExpired(long now, ForgetfulMap.CachedValue<V> cached)
-
unwrap
private java.util.Map.Entry<K,V> unwrap(java.util.Map.Entry<K,ForgetfulMap.CachedValue<V>> entry)
-
-