JTemporal Home
Tutorial

net.sf.jtemporal
Class CachedTimeSeries<V>

java.lang.Object
  extended by net.sf.jtemporal.CachedTimeSeries<V>
Type Parameters:
V - the type of the value
All Implemented Interfaces:
ReadableTemporalAttribute<V>, TimeSeries<V>

public class CachedTimeSeries<V>
extends java.lang.Object
implements TimeSeries<V>

Caches an external TimeSeries (for ex. to hand code a database access). Each time this collection is queried, if the value for the given instant is unknown, the MissingInstantHandler is invoked. Then, the handler has the choice whether to retrieve just the missing value, or other values, or other objects as well to reduce call granularity (prefetch).
Internally a LightLRUCache is used to cache reads. Writes are not cached, if you want to cache them, you should do it at the ChangeHandler level.
If you already have a persistence framework, you will eventually use this class on top of that, in order to control and customize prefetching.
This class is NOT thread-safe.

Version:
$Id: CachedTimeSeries.java,v 1.13 2007/09/14 20:03:10 tabeck Exp $
See Also:
LightLRUCache

Nested Class Summary
static interface CachedTimeSeries.ChangeHandler<V>
          Applies the changes on the underlying data source.
static interface CachedTimeSeries.MissingElementHandler<V>
          Reacts on missing elements.
 
Constructor Summary
CachedTimeSeries(int size, CachedTimeSeries.MissingElementHandler<V> reader)
          Creates a READ-ONLY CachedTimeSeries.
CachedTimeSeries(int size, CachedTimeSeries.MissingElementHandler<V> reader, CachedTimeSeries.ChangeHandler<V> writer)
          Creates a normal instance of CachedTimeSeries
 
Method Summary
 void clearCache()
          Clears the underlying cache.
 boolean containsInstant(Instant i)
          Returns true if this TimeSeries contains a mapping for the specified Instant.
 V get(Instant i)
          Gets the value currently associated to the i instant.
 V put(Instant i, V value)
          Creates a new entry in this timeseries.
 void putInCache(Instant i, V value)
          Writes in the cache only, without triggering the CachedTimeSeries.ChangeHandler.
 V remove(Instant i)
          Removes an entry from this timeseries.
 void remove(Period p)
          Remove a whole period from the timeseries (optional implementation).
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CachedTimeSeries

public CachedTimeSeries(int size,
                        CachedTimeSeries.MissingElementHandler<V> reader)
Creates a READ-ONLY CachedTimeSeries.

Parameters:
size - the size of the cache (number of entries)
reader - handles physical read requests

CachedTimeSeries

public CachedTimeSeries(int size,
                        CachedTimeSeries.MissingElementHandler<V> reader,
                        CachedTimeSeries.ChangeHandler<V> writer)
Creates a normal instance of CachedTimeSeries

Parameters:
size - size the size of the cache (number of entries)
reader - handles physical read requests
writer - handles write requests
Method Detail

put

public V put(Instant i,
             V value)
Description copied from interface: TimeSeries
Creates a new entry in this timeseries.

Specified by:
put in interface TimeSeries<V>
Parameters:
i - the instant whom the value must be associated to. Must be not null.
value - the value. Must be not null.
Returns:
the value previously associated to the i instant, or null.

putInCache

public void putInCache(Instant i,
                       V value)
Writes in the cache only, without triggering the CachedTimeSeries.ChangeHandler.
Usually this method is only called by the CachedTimeSeries.MissingElementHandler implementation.
+ * If you want to cache the information that no value is available for the given instant, then you must put a value=null.

Parameters:
i - the key of the entry
value - the value to be associated to the given instant, or null, if no value exists at the given instant.

get

public V get(Instant i)
Description copied from interface: TimeSeries
Gets the value currently associated to the i instant.

Specified by:
get in interface ReadableTemporalAttribute<V>
Specified by:
get in interface TimeSeries<V>
Parameters:
i - instant to query
Returns:
the value currently associated to the i instant, or null.

remove

public V remove(Instant i)
Description copied from interface: TimeSeries
Removes an entry from this timeseries.

Specified by:
remove in interface TimeSeries<V>
Parameters:
i - the instant to be removed. Must be not null.
Returns:
the value that was previously associated to the i instant

remove

public void remove(Period p)
Description copied from interface: TimeSeries
Remove a whole period from the timeseries (optional implementation).

Specified by:
remove in interface TimeSeries<V>
Parameters:
p - the period enclosing the entries to be removed.

clearCache

public void clearCache()
Clears the underlying cache. Useful when you know that the underlying data has been changed by somebody else, and your cached imformation is not valid anymore.


containsInstant

public boolean containsInstant(Instant i)
Description copied from interface: TimeSeries
Returns true if this TimeSeries contains a mapping for the specified Instant.

Specified by:
containsInstant in interface TimeSeries<V>
Parameters:
i - instant whose presence in this TimeSeries is to be tested.
Returns:
true if this TimeSeries contains a mapping for the specified instant.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

JTemporal Home
Tutorial