Package io.github.xanthic.cache.api
Interface Cache<K,V>
- Type Parameters:
K
- The type of keys that form the cacheV
- The type of values contained in the cache
public interface Cache<K,V>
The basic interface that various cache implementations must provide.
This interface can be thought as a superset of Map
;
less functionality is required here to achieve greater compatibility.
There cannot be duplicate keys, and null keys or values are not permitted.
Instances should be thread-safe, but no guarantee is made whether this is achieved through fine-grained locking or blunt synchronization.
Implementations ought to implement expiry and size-based eviction, as defined in ICacheSpec
.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all entries from the cache.Computes what value should be associated with the specified key, or null if the mapping should be removed.computeIfAbsent
(K key, @NotNull Function<K, V> computeFunc) Obtains the value currently associated with the specified key, or atomically stores the computed value if no prior mapping existed.computeIfPresent
(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> computeFunc) Computes a new value for a specific key, if a mapping already existed.default void
forEach
(@NotNull BiConsumer<? super K, ? super V> action) Performs the specified action upon all entries within the cache.Obtains the value associated with the specified key.default V
getOrDefault
(K key, V defaultValue) Obtains the value associated with the specified key, or the passed default value if no mapping existed.Associates the key with the specified value (or the result of the atomic merge function if a mapping already existed).Associates the specified key with the specified value, creating or replacing the mapping as needed.default void
Copies all of the mappings from the specified map to this cache.putIfAbsent
(K key, V value) Creates a mapping from the specified key to the specified value, if no mapping for the key already existed.Deletes any mapping that may exist for the specified key.boolean
Replaces the entry for the specified key only if it is currently mapped to some value.boolean
Replaces the value for the specified key only if it is currently mapped to the specified old value.long
size()
-
Method Details
-
get
Obtains the value associated with the specified key.- Parameters:
key
- the key whose mapped value should be queried- Returns:
- the value associated with the key in the cache, or null if no such mapping was found
- Throws:
NullPointerException
- if the specified key is null
-
put
Associates the specified key with the specified value, creating or replacing the mapping as needed.- Parameters:
key
- the key whose mapping should be created or updatedvalue
- the value to be associated with the specified key- Returns:
- the previous value associated with the key, or null if no prior mapping existed
- Throws:
NullPointerException
- if the specified key or value is null
-
remove
Deletes any mapping that may exist for the specified key.- Parameters:
key
- the key whose mapping should be deleted- Returns:
- the value in the removed mapping, or null if no mapping existed
- Throws:
NullPointerException
- if the specified key is null
-
clear
void clear()Removes all entries from the cache. -
size
long size()- Returns:
- the estimated number of entries contained in the cache
-
compute
@Nullable V compute(@NotNull K key, @NotNull @NotNull BiFunction<? super K, ? super V, ? extends V> computeFunc) Computes what value should be associated with the specified key, or null if the mapping should be removed.- Parameters:
key
- the key with which the specified value is to be associatedcomputeFunc
- the function to compute a value- Returns:
- the new value associated with the specified key, or null if none
- Throws:
NullPointerException
- if the specified key is null or the compute function is null
-
computeIfAbsent
Obtains the value currently associated with the specified key, or atomically stores the computed value if no prior mapping existed.- Parameters:
key
- the key whose mapping should be created or returnedcomputeFunc
- the value supplier for a given key, if no mapping already existed- Returns:
- the current (existing or computed) value associated with the key
- Throws:
NullPointerException
- if the specified key is null or the compute function is null
-
computeIfPresent
@Nullable V computeIfPresent(@NotNull K key, @NotNull @NotNull BiFunction<? super K, ? super V, ? extends V> computeFunc) Computes a new value for a specific key, if a mapping already existed.If the compute function yields null, the mapping should be removed.
- Parameters:
key
- the key whose mapping should be updatedcomputeFunc
- the function to compute the new value for an already existing mapping- Returns:
- the new value associated with the key, or null if none
- Throws:
NullPointerException
- if the specified key is null or the compute function is null
-
putIfAbsent
Creates a mapping from the specified key to the specified value, if no mapping for the key already existed.- Parameters:
key
- the key whose mapping should be created or returnedvalue
- the value that should be associated with the key if no prior mapping exists- Returns:
- the previous value associated with the key, or null if no mapping already existed
- Throws:
NullPointerException
- if the specified key or value is null
-
merge
Associates the key with the specified value (or the result of the atomic merge function if a mapping already existed).- Parameters:
key
- the key whose mapping should be created or updatedvalue
- the value to be associated with the key or merged with the existing mapped valuemergeFunc
- the function that takes the existing value and the new value to compute a merged value- Returns:
- the latest value associated with the specified key
- Throws:
NullPointerException
- if the specified key or the value or mergeFunc is null
-
replace
Replaces the entry for the specified key only if it is currently mapped to some value.- Parameters:
key
- the key whose mapped value should be updatedvalue
- the value to be associated with the specified key- Returns:
- whether a replacement occurred (a prior mapping must have existed to return true)
- Throws:
NullPointerException
- if the specified key or value is null
-
replace
Replaces the value for the specified key only if it is currently mapped to the specified old value.- Parameters:
key
- the key whose mapped value should be updatedoldValue
- the value expected to be already associated with the specified keynewValue
- the value to be newly associated with the specified key- Returns:
- whether a replacement occurred
- Throws:
NullPointerException
- if a specified key or newValue is null
-
getOrDefault
Obtains the value associated with the specified key, or the passed default value if no mapping existed.Note: Unlike
computeIfAbsent(Object, Function)
, this method does not write the default value to the cache.- Parameters:
key
- the key whose mapped value should be querieddefaultValue
- the default value to return if no mapping exists for the specified key- Returns:
- the value mapped to the specified key, if present; otherwise, the specified default value
- Throws:
NullPointerException
- if the specified key is null
-
putAll
Copies all of the mappings from the specified map to this cache.- Parameters:
map
- the map whose entries should be added to this cache- Throws:
NullPointerException
- if the map is null, or the specified map contains null keys or values
-
forEach
Performs the specified action upon all entries within the cache.- Parameters:
action
- the action to perform upon each entry- Throws:
NullPointerException
- if the specified action is nullUnsupportedOperationException
- if the underlying cache provider does not support iteration over entries
-