ca.odell.glazedlists.io

Class CachingList

Implemented Interfaces:
EventListener, EventList<E>, List, ListEventListener<E>

public class CachingList
extends TransformedList<S,E>

An EventList that caches elements from its source EventList. It is useful in cases when the get(int) method of an EventList is expensive. It can also be used when there are too many elements to keep in memory simultaneously. For caching to be effective, object access must be clustered.

This EventList caches the most recently requested n elements.

By overriding the preFetch(int) method, you can modify this CachingList to do predictive lookups for higher performance.

EventList Overview
Writable:yes
Concurrency:thread ready, not thread safe
Performance:reads: O(log N), writes O(log N)
Memory:O(N)
Unit Tests:N/A
Issues: 22 32 43 262
Author:
Kevin Maltby

Field Summary

Fields inherited from class ca.odell.glazedlists.TransformedList<S,E>

source

Fields inherited from class ca.odell.glazedlists.AbstractEventList<E>

publisher, readWriteLock, updates

Constructor Summary

CachingList(EventList<E> source, int maxSize)
Creates a CachingList that caches elements from the specified source EventList.

Method Summary

protected Object
fetch(int index, boolean recordHitsOrMisses)
Fetches a particular element.
Object
get(int index)
float
getCacheHitRatio()
Gets the ratio of cache hits to cache misses.
int
getCacheHits()
Gets the total number of times that this list has fetched its result from the cache rather than from the source list.
int
getCacheMisses()
Gets the total number of times that this list has fetched its result from the source list rather than the cache.
protected boolean
isWritable()
void
listChanged(ListEvent<E> listChanges)
protected void
preFetch(int index)
Pre-fetches a set of data given the index that was directly requested.
int
size()

Methods inherited from class ca.odell.glazedlists.TransformedList<S,E>

add, addAll, clear, dispose, get, getSourceIndex, isWritable, listChanged, remove, removeAll, retainAll, set, size

Methods inherited from class ca.odell.glazedlists.AbstractEventList<E>

T[] toArray, add, add, addAll, addAll, addListEventListener, clear, contains, containsAll, equals, get, getPublisher, getReadWriteLock, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeListEventListener, retainAll, set, size, subList, toArray, toString

Constructor Details

CachingList

public CachingList(EventList<E> source,
                   int maxSize)
Creates a CachingList that caches elements from the specified source EventList.
Parameters:
source - The source list to use to get values from
maxSize - The maximum size of the cache

Method Details

fetch

protected final Object fetch(int index,
                             boolean recordHitsOrMisses)
Fetches a particular element.

This might seem redundant with the existence of get(int). However, the goals of the methods are different. This method exists to be called by get(int) or preFetch(int). This distinction allows users overriding this class a means of entry retrieval which does not implicitly execute a pre-fetch. This is particularly key for users overriding preFetch(int)

Parameters:
index - The index of the value to retrieve
recordHitsOrMisses - Whether to increment the hit/miss counters (this should always be false when called from preFetch(int)).
Returns:
The value associated with the given index, or null if the index is not found.

get

public final Object get(int index)
Overrides:
get in interface TransformedList<S,E>

getCacheHitRatio

public final float getCacheHitRatio()
Gets the ratio of cache hits to cache misses. This is a number between 0 and 1, where 0 means the cache is unused and 1 means the cache was used exclusively.

getCacheHits

public final int getCacheHits()
Gets the total number of times that this list has fetched its result from the cache rather than from the source list.
Returns:
The number of times that this cache provided the result

getCacheMisses

public final int getCacheMisses()
Gets the total number of times that this list has fetched its result from the source list rather than the cache.
Returns:
The number of times that this cache couldn't provide the result

isWritable

protected boolean isWritable()
Overrides:
isWritable in interface TransformedList<S,E>

listChanged

public final void listChanged(ListEvent<E> listChanges)

preFetch

protected void preFetch(int index)
Pre-fetches a set of data given the index that was directly requested.

Each application that wishes to take advantage of pre-fetching should implement this method in a way which best fits their particular use cases. As such, no default pre-fetch behaviour could really be defined, and thus this method is empty by default.

Because pre-fetching can modify the cache, child classes of CachingList should use careful consideration of locking when implementing this method.

Parameters:
index - The index that was requested from the cache

size

public final int size()
Overrides:
size in interface TransformedList<S,E>

Glazed Lists, Copyright © 2003-2006 publicobject.com, O'Dell Engineering.
Documentation build by buildd at 2008-01-04 14:38