Class Cache4<K1,K2,K3,K4,V>
java.lang.Object
org.apache.juneau.commons.collections.Cache4<K1,K2,K3,K4,V>
- Type Parameters:
K1- The first key type.K2- The second key type.K3- The third key type.K4- The fourth key type.V- The value type.
Simple in-memory cache for storing and retrieving objects by four-part keys.
Overview:
This class uses ConcurrentHashMap internally to provide a thread-safe caching layer with automatic
value computation, cache eviction, and statistics tracking for four-part composite keys. It's designed for
caching expensive-to-compute or frequently-accessed objects indexed by four keys.
Features:
- Thread-safe concurrent access without external synchronization
- Four-part composite key support
- Automatic cache eviction when maximum size is reached
- Lazy computation via
Function4supplier pattern - Default supplier support for simplified access
- Built-in hit/miss statistics tracking
- Optional logging of cache statistics on JVM shutdown
- Can be disabled entirely via builder or system property
See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from the cache.booleancontainsKey(K1 key1, K2 key2, K3 key3, K4 key4) Returnstrue if the cache contains a mapping for the specified four-part key.booleancontainsValue(V value) Returnstrue if the cache contains one or more entries with the specified value.static <K1,K2, K3, K4, V>
Cache4.Builder<K1,K2, K3, K4, V> create()Creates a newCache4.Builderfor constructing a cache with explicit type parameters.Retrieves a cached value by four-part key using the default supplier.Retrieves a cached value by four-part key, computing it if necessary using the provided supplier.intReturns the total number of cache hits since this cache was created.booleanisEmpty()Returnstrue if the cache contains no entries.static <K1,K2, K3, K4, V>
Cache4.Builder<K1,K2, K3, K4, V> Creates a newCache4.Builderfor constructing a cache.Associates the specified value with the specified four-part key.Removes the entry for the specified four-part key from the cache.intsize()Returns the number of entries in the cache.
-
Constructor Details
-
Cache4
Constructor.- Parameters:
builder- The builder containing configuration settings.
-
-
Method Details
-
create
Creates a newCache4.Builderfor constructing a cache with explicit type parameters.This variant allows you to specify the cache's generic types explicitly without passing the class objects, which is useful when working with complex parameterized types.
- Type Parameters:
K1- The first key type.K2- The second key type.K3- The third key type.K4- The fourth key type.V- The value type.- Returns:
- A new builder for configuring the cache.
-
of
public static <K1,K2, Cache4.Builder<K1,K3, K4, V> K2, ofK3, K4, V> (Class<K1> key1, Class<K2> key2, Class<K3> key3, Class<K4> key4, Class<V> type) Creates a newCache4.Builderfor constructing a cache.- Type Parameters:
K1- The first key type.K2- The second key type.K3- The third key type.K4- The fourth key type.V- The value type.- Parameters:
key1- The first key type class (used for type safety).key2- The second key type class (used for type safety).key3- The third key type class (used for type safety).key4- The fourth key type class (used for type safety).type- The value type class.- Returns:
- A new builder for configuring the cache.
-
clear
Removes all entries from the cache. -
containsKey
Returnstrue if the cache contains a mapping for the specified four-part key.- Parameters:
key1- The first key.key2- The second key.key3- The third key.key4- The fourth key.- Returns:
true if the cache contains the four-part key.
-
containsValue
Returnstrue if the cache contains one or more entries with the specified value.- Parameters:
value- The value to check.- Returns:
true if the cache contains the value.
-
get
Retrieves a cached value by four-part key using the default supplier.- Parameters:
key1- First key component. Can benull .key2- Second key component. Can benull .key3- Third key component. Can benull .key4- Fourth key component. Can benull .- Returns:
- The cached or computed value. May be
null if the supplier returnsnull . - Throws:
NullPointerException- if no default supplier was configured.
-
get
Retrieves a cached value by four-part key, computing it if necessary using the provided supplier.- Parameters:
key1- First key component. Can benull .key2- Second key component. Can benull .key3- Third key component. Can benull .key4- Fourth key component. Can benull .supplier- The supplier to compute the value if it's not in the cache. Must not benull .- Returns:
- The cached or computed value. May be
null if the supplier returnsnull .
-
getCacheHits
Returns the total number of cache hits since this cache was created.- Returns:
- The total number of cache hits since creation.
-
isEmpty
Returnstrue if the cache contains no entries.- Returns:
true if the cache is empty.
-
put
Associates the specified value with the specified four-part key.- Parameters:
key1- The first key.key2- The second key.key3- The third key.key4- The fourth key.value- The value to associate with the four-part key.- Returns:
- The previous value associated with the four-part key, or
null if there was no mapping.
-
remove
Removes the entry for the specified four-part key from the cache.- Parameters:
key1- The first key.key2- The second key.key3- The third key.key4- The fourth key.- Returns:
- The previous value associated with the four-part key, or
null if there was no mapping.
-
size
Returns the number of entries in the cache.- Returns:
- The number of cached entries.
-