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 intDEFAULT_MAX_ENTRIESpublic Contextcontextpublic intmaxEntries
-
Constructor Summary
Constructors Constructor Description CacheBase()
-
Method Summary
Modifier and Type Method Description ContextgetContext()Returns the Android application context. intgetMaxEntries()Returns the cache capacity specified via init. voidinit(@NonNull() Context context, int maxEntries)Initializes a cache with Android application context and maximum number of entries the cache can hold. voidinit(@NonNull() Context context)Initializes a cache with Android application context. Vput(@NonNull() K key, @NonNull() V value, double cost)Adds the value and the associated cost to the cache for the key. Vput(@NonNull() K key, @NonNull() V value)Adds or updates a value associated with a key. Vget(@NonNull() K key)Returns the value associated with a key. CacheEntrygetEntry(@NonNull() K key)Retrieves the value associated with a key, then returns a CacheEntryobject that embeds the key and value.doublegetCostOfEntries()Returns the aggregated value of the cost of the entries in the cache. intgetEntryCount()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- nullempty list.voidremove(@NonNull() K key)Removes a cache entry. voidremoveAll()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-nullkey of a cache entryvalue- anon-nullvalue to addcost- the cost associated with this entry.- Returns:
The previous value associated with the existed
key,nullif 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-nullkey of a cache entryvalue- anon-nullvalue to add- Returns:
The original value of an existing cache entry, or
nullif this is a new entry.
-
get
@Nullable() V get(@NonNull() K key)
Returns the value associated with a key.
- Parameters:
key- anon-nullkey of a cache entry- Returns:
The value associated with a key or
nullif the key is not found.
-
getEntry
@Nullable() CacheEntry getEntry(@NonNull() K key)
Retrieves the value associated with a key, then returns a
CacheEntryobject that embeds the key and value.- Parameters:
key- anon-nullkey of a cache entry- Returns:
A CacheEntry or
nullif 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-
nullempty 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-
nullempty list (use isEmpty to check).
-
remove
void remove(@NonNull() K key)
Removes a cache entry.
- Parameters:
key- anon-nullkey of the entry to remove
-
removeAll
void removeAll()
Removes all entries from the cache.
-
-
-
-