Class ForgetfulMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
org.ojalgo.type.ForgetfulMap<K,V>
All Implemented Interfaces:
Map<K,V>

public final class ForgetfulMap<K,V> extends AbstractMap<K,V>
A Map that 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 Map interface, 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.

  • Field Details

  • Constructor Details

  • Method Details

    • 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()
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class AbstractMap<K,V>
    • 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 a cleanUp() first.
    • get

      public V get(K key, Function<? super K,? extends V> mappingFunction)
      Returns the value associated with the key in this cache, obtaining that value from the mappingFunction if 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(Object key)
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
    • getAllPresent

      public Map<K,V> getAllPresent(Iterable<? extends K> keys)
      Returns a map of the values associated with the keys in this cache. The returned map will only contain entries which are already present in the cache.

      Note that duplicate elements in keys, as determined by Object.equals(java.lang.Object), will be ignored.

    • getIfPresent

      public V getIfPresent(K key)
      Returns the value associated with the key in this cache, or null if there is no cached value for the key.
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class AbstractMap<K,V>
    • invalidate

      public void invalidate(K key)
      Discards any cached value for the key. 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(Iterable<? extends K> keys)
      Discards any cached values for the keys. 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()
      Specified by:
      isEmpty in interface Map<K,V>
      Overrides:
      isEmpty in class AbstractMap<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
      Overrides:
      keySet in class AbstractMap<K,V>
    • newValueCache

      public ForgetfulMap.ValueCache<K,V> newValueCache(K key, Function<? super K,? extends V> valueSupplier)
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
    • putAll

      public void putAll(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 calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class AbstractMap<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
    • remove

      public boolean remove(Object key, Object value)
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<K,V>
    • isExpired

      private boolean isExpired(long now, ForgetfulMap.CachedValue<V> cached)
    • unwrap

      private Map.Entry<K,V> unwrap(Map.Entry<K,ForgetfulMap.CachedValue<V>> entry)