org.omnaest.utils.structure.element.cached
Class CachedElement<T>

java.lang.Object
  extended by org.omnaest.utils.structure.element.cached.CachedElement<T>
Type Parameters:
T -
Direct Known Subclasses:
CachedElementTimed, SoftCachedElement, ThreadLocalCachedElement, WeakCachedElement

public class CachedElement<T>
extends Object

A CachedElement provides an abstract cache mechanism around a given value. The value is initially resolved once from a given CachedElement.ValueResolver.

Author:
Omnaest

Nested Class Summary
protected static interface CachedElement.CachedValue<T>
          Used to store and retrieve the CachedElement.CachedValue
static interface CachedElement.ValueResolver<T>
           
static class CachedElement.ValueResolverSimple<T>
          Simple CachedElement.ValueResolver which returns the object initially given to the constructor.
 
Field Summary
protected  CachedElement.CachedValue<T> cachedValue
           
protected  CachedElement.ValueResolver<T> valueResolver
           
 
Constructor Summary
CachedElement(CachedElement.ValueResolver<T> valueResolver)
           
CachedElement(T value)
           
 
Method Summary
 CachedElement<T> clearCache()
          Clears the cached value, so the next call to getValue() will resolve the value once again from the underlying CachedElement.ValueResolver.
 T getValue()
          Returns the value from the cache or resolves it from the underlying CachedElement.ValueResolver if it is null.
 T getValueFromCacheOnly()
          Returns the cached value and does not resolve the value if the cached value is null.
 CachedElement.ValueResolver<T> getValueResolver()
           
 boolean hasValueResolved()
          Returns true, if the cache has resolved a value from the underlying CachedElement.ValueResolver instance.
protected  CachedElement.CachedValue<T> newCachedValue()
          Creates a new CachedElement.CachedValue instance.
 void setValueResolver(CachedElement.ValueResolver<T> valueResolver)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cachedValue

protected CachedElement.CachedValue<T> cachedValue

valueResolver

protected CachedElement.ValueResolver<T> valueResolver
Constructor Detail

CachedElement

public CachedElement(CachedElement.ValueResolver<T> valueResolver)
Parameters:
valueResolver -
See Also:
CachedElement, CachedElement.ValueResolver

CachedElement

public CachedElement(T value)
Parameters:
value -
See Also:
CachedElement, CachedElement.ValueResolver, CachedElement.ValueResolverSimple
Method Detail

getValue

public T getValue()
Returns the value from the cache or resolves it from the underlying CachedElement.ValueResolver if it is null.

See Also:
getValueFromCacheOnly()

getValueFromCacheOnly

public T getValueFromCacheOnly()
Returns the cached value and does not resolve the value if the cached value is null.
Use getValue() for normal purposes.

Returns:
See Also:
getValue()

hasValueResolved

public boolean hasValueResolved()
Returns true, if the cache has resolved a value from the underlying CachedElement.ValueResolver instance.

Returns:

setValueResolver

public void setValueResolver(CachedElement.ValueResolver<T> valueResolver)

getValueResolver

public CachedElement.ValueResolver<T> getValueResolver()

clearCache

public CachedElement<T> clearCache()
Clears the cached value, so the next call to getValue() will resolve the value once again from the underlying CachedElement.ValueResolver.

Returns:
this

newCachedValue

protected CachedElement.CachedValue<T> newCachedValue()
Creates a new CachedElement.CachedValue instance. Override this to alternate the behavior of the CachedElement

Returns:


Copyright © 2013. All Rights Reserved.