com.samskivert.util
Class ExpiringReference<T>

java.lang.Object
  extended by com.samskivert.util.ExpiringReference<T>

public class ExpiringReference<T>
extends Object

Provides a simple way of tracking a resource that should become stale after a certain time period. This is useful for caching data that was expensive to compute and should be cached for some time before being recreated.

Note: the data will not be unreferenced and thus garbage collectable until it has been requested at least once after it has expired. Thus expiring references must be combined with an LRUHashMap if memory conservation is also desired.


Field Summary
protected  long _expires
           
protected  T _value
           
 
Constructor Summary
ExpiringReference(T value, long expireMillis)
          Creates an reference to the specified value that will expire in the specified number of milliseconds.
 
Method Summary
static
<T> ExpiringReference<T>
create(T value, long expireMillis)
          Creates an expiring reference with the supplied value and expiration time.
static
<T> T
get(ExpiringReference<T> value)
          Gets the value from an expiring reference but returns null if the supplied reference reference is null.
 T getValue()
          Returns the value with which we were created or null if the value has expired.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_value

protected T _value

_expires

protected long _expires
Constructor Detail

ExpiringReference

public ExpiringReference(T value,
                         long expireMillis)
Creates an reference to the specified value that will expire in the specified number of milliseconds.

Method Detail

create

public static <T> ExpiringReference<T> create(T value,
                                              long expireMillis)
Creates an expiring reference with the supplied value and expiration time.


get

public static <T> T get(ExpiringReference<T> value)
Gets the value from an expiring reference but returns null if the supplied reference reference is null.


getValue

public T getValue()
Returns the value with which we were created or null if the value has expired.



Copyright © 2000-2008 Michael Bayne. All Rights Reserved.