Class CacheEntry<T>
- java.lang.Object
-
- com.identityworksllc.iiq.common.cache.CacheEntry<T>
-
- Type Parameters:
T
- The type of the object to store
- All Implemented Interfaces:
Serializable
,Supplier<Optional<T>>
- Direct Known Subclasses:
VersionedCacheEntry
public class CacheEntry<T> extends Object implements Serializable, Supplier<Optional<T>>
Cache entry for use with the CacheMap class or other purposes.This class allows you to store a dated object of any type. The entry will be considered expired when the current date is after the expiration date.
Instances of this class are
Externalizable
if the contained type T is serializable.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description CacheEntry(CacheEntry<? extends T> other)
Copy constructorCacheEntry(T entryValue, long entryExpirationTimestampMillis)
Construct a new cache entry that expires at a specific Unix timestamp (in milliseconds)CacheEntry(T entryValue, long futureTimeAmount, TimeUnit timeUnit)
Construct a new cache entry that expires a specific amount of time in the futureCacheEntry(T entryValue, Date entryExpiration)
Construct a new cache entry that expires on a specific date
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AtomicStampedReference<T>
asStampedReference()
Returns anAtomicStampedReference
object with the ‘stamp’ set to the seconds (NOT milliseconds) timestamp of expiration.static <T> CacheEntry<T>
computeIfExpired(CacheEntry<T> entry, Supplier<CacheEntry<T>> valueSupplier)
Returns either the entry (if it is not null and not expired), or invokes the Supplier to generate a new entry if it is expired.boolean
equals(Object o)
Optional<T>
get()
If the entry is expired, returnsOptional.empty()
.long
getExpiration()
Returns the expiration instant, in Unix timestamp millisT
getValue()
Gets the value associated with this cache entryint
hashCode()
boolean
isExpired()
The entry is expired if the current time is after the expiration dateString
toString()
-
-
-
Constructor Detail
-
CacheEntry
public CacheEntry(CacheEntry<? extends T> other)
Copy constructor- Parameters:
other
- The other cache entry
-
CacheEntry
public CacheEntry(T entryValue, long futureTimeAmount, TimeUnit timeUnit)
Construct a new cache entry that expires a specific amount of time in the future- Parameters:
entryValue
- The entry valuefutureTimeAmount
- The amount of time in the future (from right now) to expire ittimeUnit
- The units in which the amount of time is specified
-
CacheEntry
public CacheEntry(T entryValue, Date entryExpiration)
Construct a new cache entry that expires on a specific date- Parameters:
entryValue
- The value of this cache entryentryExpiration
- The expiration date of this cache entry
-
CacheEntry
public CacheEntry(T entryValue, long entryExpirationTimestampMillis)
Construct a new cache entry that expires at a specific Unix timestamp (in milliseconds)- Parameters:
entryValue
- The value of this cache entryentryExpirationTimestampMillis
- The expiration date of this cache entry
-
-
Method Detail
-
computeIfExpired
public static <T> CacheEntry<T> computeIfExpired(CacheEntry<T> entry, Supplier<CacheEntry<T>> valueSupplier)
Returns either the entry (if it is not null and not expired), or invokes the Supplier to generate a new entry if it is expired.- Type Parameters:
T
- The type of thing that is cached- Parameters:
entry
- The existing entry (which can be null)valueSupplier
- A function to calculate a new value- Returns:
- The value supplier
-
asStampedReference
public AtomicStampedReference<T> asStampedReference()
Returns anAtomicStampedReference
object with the ‘stamp’ set to the seconds (NOT milliseconds) timestamp of expiration.- Returns:
- An AtomicStampedReference
-
equals
public boolean equals(Object o)
- Overrides:
equals
in classObject
- See Also:
Object.equals(Object)
-
get
public Optional<T> get()
If the entry is expired, returnsOptional.empty()
.If the entry is not expired, returns
Optional.ofNullable(Object)
.
-
getExpiration
public long getExpiration()
Returns the expiration instant, in Unix timestamp millis- Returns:
- The expiration timestamp
-
getValue
public T getValue()
Gets the value associated with this cache entry- Returns:
- The cached value
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classObject
- See Also:
Object.hashCode()
-
isExpired
public boolean isExpired()
The entry is expired if the current time is after the expiration date- Returns:
- True if expired
-
-