Class CacheBase
-
- All Implemented Interfaces:
-
com.sap.cloud.mobile.foundation.cache.Cache
public abstract class CacheBase<K, V> implements Cache<K, V>
Abstract class for Cache implementations.
Provides default implementation of Cache: 1. Embeds and handles the Android Context and cache capacity (maximum number of entries) via init or init. 2. Forces concrete Cache implementation to enforce operation state via checkConfiguration in cache read-write access methods. 3. Provides no-op(not supported) cache methods.
-
-
Field Summary
Fields Modifier and Type Field Description public final static int
DEFAULT_MAX_ENTRIES
public Context
context
public int
maxEntries
-
Constructor Summary
Constructors Constructor Description CacheBase()
-
Method Summary
Modifier and Type Method Description Context
getContext()
Returns the Android application context. int
getMaxEntries()
Returns the cache capacity specified via init. void
init(@NonNull() Context context, int maxEntries)
Initializes a cache with Android application context and maximum number of entries the cache can hold. void
init(@NonNull() Context context)
Initializes a cache with Android application context. V
put(@NonNull() K key, @NonNull() V value, double cost)
Adds the value and the associated cost to the cache for the key. V
put(@NonNull() K key, @NonNull() V value)
Adds or updates a value associated with a key. V
get(@NonNull() K key)
Returns the value associated with a key. CacheEntry
getEntry(@NonNull() K key)
Retrieves the value associated with a key, then returns a CacheEntry
object that embeds the key and value.double
getCostOfEntries()
Returns the aggregated value of the cost of the entries in the cache. int
getEntryCount()
Returns the number of entries (key-value pairs) in the cache. List<K>
keys()
Returns a list of keys of all entries in the cache or a non- null
empty list.void
remove(@NonNull() K key)
Removes a cache entry. void
removeAll()
Removes all entries from the cache. -
-
Method Detail
-
getContext
@NonNull() Context getContext()
Returns the Android application context.
- Returns:
The associated Android application context.
-
getMaxEntries
int getMaxEntries()
Returns the cache capacity specified via init.
- Returns:
The maximum number of entries the cache can hold.
-
init
void init(@NonNull() Context context, int maxEntries)
Initializes a cache with Android application context and maximum number of entries the cache can hold.
- Parameters:
context
- Android application contextmaxEntries
- maximum number of entries the cache can hold.
-
init
void init(@NonNull() Context context)
Initializes a cache with Android application context.
- Parameters:
context
- Android application context
-
put
@Nullable() V put(@NonNull() K key, @NonNull() V value, double cost)
Adds the value and the associated cost to the cache for the key. The behavior when the aggregate of costs exceeds the maximum cost during the execution of this method is implementation dependent.
- Parameters:
key
- anon-null
key of a cache entryvalue
- anon-null
value to addcost
- the cost associated with this entry.- Returns:
The previous value associated with the existed
key
,null
if the entry did exist.
-
put
@Nullable() V put(@NonNull() K key, @NonNull() V value)
Adds or updates a value associated with a key.
- Parameters:
key
- anon-null
key of a cache entryvalue
- anon-null
value to add- Returns:
The original value of an existing cache entry, or
null
if this is a new entry.
-
get
@Nullable() V get(@NonNull() K key)
Returns the value associated with a key.
- Parameters:
key
- anon-null
key of a cache entry- Returns:
The value associated with a key or
null
if the key is not found.
-
getEntry
@Nullable() CacheEntry getEntry(@NonNull() K key)
Retrieves the value associated with a key, then returns a
CacheEntry
object that embeds the key and value.- Parameters:
key
- anon-null
key of a cache entry- Returns:
A CacheEntry or
null
if the key is not found.
-
getCostOfEntries
double getCostOfEntries()
Returns the aggregated value of the cost of the entries in the cache. The concrete behavior is implementation dependent.
- Returns:
The aggregated value of the cost of the entries in the cache.
-
getEntryCount
int getEntryCount()
Returns the number of entries (key-value pairs) in the cache.
- Returns:
The number of entries.
-
keys
@NonNull() List<K> keys()
Returns a list of keys of all entries in the cache or a non-
null
empty list. The order of the entries in the list is implementation dependent.- Returns:
A list of keys of all entries in the cache or a non-
null
empty list (use isEmpty to check).
-
remove
void remove(@NonNull() K key)
Removes a cache entry.
- Parameters:
key
- anon-null
key of the entry to remove
-
removeAll
void removeAll()
Removes all entries from the cache.
-
-
-
-