Class LockedAbstractCache<K,​V>

java.lang.Object
io.github.xanthic.cache.core.LockedAbstractCache<K,​V>
Type Parameters:
K - The type of keys that form the cache
V - The type of values contained in the cache
All Implemented Interfaces:
io.github.xanthic.cache.api.Cache<K,​V>

public abstract class LockedAbstractCache<K,​V>
extends java.lang.Object
implements io.github.xanthic.cache.api.Cache<K,​V>
Safely implements Cache methods using ReadWriteLock.

This abstraction allows for underlying cache providers to implement the Cache interface in a thread-safe manner, even if the underlying cache does not already implement the more "exotic" write methods required by the Cache contract.

As this abstraction has a performance hit, it should be avoided if the backing cache provider already provides an implementation for these methods.

Does not support null values.

  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected java.util.concurrent.locks.ReadWriteLock lock  
  • Constructor Summary

    Constructors 
    Constructor Description
    LockedAbstractCache()  
  • Method Summary

    Modifier and Type Method Description
    void clear()  
    protected abstract void clearUnlocked()  
    V compute​(K key, @NotNull java.util.function.BiFunction<? super K,​? super V,​? extends V> computeFunc)  
    V computeIfAbsent​(K key, @NotNull java.util.function.Function<K,​V> computeFunc)  
    V computeIfPresent​(K key, @NotNull java.util.function.BiFunction<? super K,​? super V,​? extends V> computeFunc)  
    V get​(K key)  
    protected abstract V getUnlocked​(K key)  
    V merge​(K key, V value, @NotNull java.util.function.BiFunction<V,​V,​V> mergeFunc)  
    V put​(K key, V value)  
    void putAll​(@NotNull java.util.Map<? extends K,​? extends V> map)  
    V putIfAbsent​(K key, V value)  
    protected abstract void putUnlocked​(K key, V value)  
    protected <T> T read​(java.util.function.Supplier<T> reader)  
    V remove​(K key)  
    protected abstract void removeUnlocked​(K key)  
    boolean replace​(K key, V value)  
    boolean replace​(K key, V oldValue, V newValue)  
    long size()  
    protected abstract long sizeUnlocked()  
    protected <T> T write​(java.util.function.Supplier<T> writer)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • lock

      protected final java.util.concurrent.locks.ReadWriteLock lock
  • Constructor Details

  • Method Details

    • get

      public V get​(@NotNull K key)
      Specified by:
      get in interface io.github.xanthic.cache.api.Cache<K,​V>
    • put

      public V put​(@NotNull K key, @NotNull V value)
      Specified by:
      put in interface io.github.xanthic.cache.api.Cache<K,​V>
    • remove

      public V remove​(@NotNull K key)
      Specified by:
      remove in interface io.github.xanthic.cache.api.Cache<K,​V>
    • clear

      public void clear()
      Specified by:
      clear in interface io.github.xanthic.cache.api.Cache<K,​V>
    • size

      public long size()
      Specified by:
      size in interface io.github.xanthic.cache.api.Cache<K,​V>
    • compute

      public V compute​(@NotNull K key, @NotNull @NotNull java.util.function.BiFunction<? super K,​? super V,​? extends V> computeFunc)
      Specified by:
      compute in interface io.github.xanthic.cache.api.Cache<K,​V>
    • computeIfAbsent

      public V computeIfAbsent​(@NotNull K key, @NotNull @NotNull java.util.function.Function<K,​V> computeFunc)
      Specified by:
      computeIfAbsent in interface io.github.xanthic.cache.api.Cache<K,​V>
    • computeIfPresent

      public V computeIfPresent​(@NotNull K key, @NotNull @NotNull java.util.function.BiFunction<? super K,​? super V,​? extends V> computeFunc)
      Specified by:
      computeIfPresent in interface io.github.xanthic.cache.api.Cache<K,​V>
    • putIfAbsent

      public V putIfAbsent​(@NotNull K key, @NotNull V value)
      Specified by:
      putIfAbsent in interface io.github.xanthic.cache.api.Cache<K,​V>
    • merge

      public V merge​(@NotNull K key, @NotNull V value, @NotNull @NotNull java.util.function.BiFunction<V,​V,​V> mergeFunc)
      Specified by:
      merge in interface io.github.xanthic.cache.api.Cache<K,​V>
    • replace

      public boolean replace​(@NotNull K key, @NotNull V value)
      Specified by:
      replace in interface io.github.xanthic.cache.api.Cache<K,​V>
    • replace

      public boolean replace​(@NotNull K key, @NotNull V oldValue, @NotNull V newValue)
      Specified by:
      replace in interface io.github.xanthic.cache.api.Cache<K,​V>
    • putAll

      public void putAll​(@NotNull @NotNull java.util.Map<? extends K,​? extends V> map)
      Specified by:
      putAll in interface io.github.xanthic.cache.api.Cache<K,​V>
    • getUnlocked

      @Nullable protected abstract V getUnlocked​(@NotNull K key)
    • putUnlocked

      protected abstract void putUnlocked​(@NotNull K key, @NotNull V value)
    • removeUnlocked

      protected abstract void removeUnlocked​(@NotNull K key)
    • clearUnlocked

      protected abstract void clearUnlocked()
    • sizeUnlocked

      protected abstract long sizeUnlocked()
    • read

      protected <T> T read​(java.util.function.Supplier<T> reader)
    • write

      protected <T> T write​(java.util.function.Supplier<T> writer)