ca.odell.glazedlists

Class FunctionList<S,E>

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

public final class FunctionList<S,E>
extends TransformedList<S,E>
implements RandomAccess

This List is meant to simplify the task of transforming each element of a source list to an element stored at the same index in this FunctionList. The logic of precisely how to transform the source elements is contained within a FunctionList.Function that must be supplied at the time of construction but can be changed afterward using setForwardFunction(Function). This FunctionList.Function is called the forward function because it creates elements in this FunctionList from elements that have been added or mutated within the source list. The forward function may be an implementation of either FunctionList.Function or FunctionList.AdvancedFunction.

An optional reverse FunctionList.Function which is capable of mapping the elements of this FunctionList back to the corresponding source element may be supplied in order to use the following mutating methods:

If the reverse FunctionList.Function is not supplied then callers of those methods will receive an IllegalStateException explaining that those operations are not available without the reverse FunctionList.Function.

If specified, the reverse FunctionList.Function should do its best to maintain the invariant:

o.equals(reverseFunction.evaluate(forwardFunction.evaluate(o))) for any o that is non-null.

Note: if two source elements share the same identity (i.e. source.get(i) == source.get(j) when i != j), it is up to author of the FunctionList.Function to decide if and how to preserve the relationship of their identities after their transformation.

Warning: This class is thread ready but not thread safe. See EventList for an example of thread safe code.

EventList Overview
Writable:yes
Concurrency:thread ready, not thread safe
Performance:reads: O(1), writes O(1) amortized
Memory:
Unit Tests:FunctionList
Issues: 282

Nested Class Summary

static interface
FunctionList.AdvancedFunction
An AdvancedFunction is an extension of the simple Function interface which provides more hooks in the lifecycle of the transformation of a source element.
static interface
FunctionList.Function
A Function encapsulates the logic for transforming a list element into any kind of Object.

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

FunctionList(EventList source, Function forward)
Construct a FunctionList which stores the result of transforming each source element using the given forward FunctionList.Function.
FunctionList(EventList source, Function forward, Function reverse)
Construct a FunctionList which stores the result of transforming each source element using the given forward FunctionList.Function.

Method Summary

void
add(int index, E value)
E
get(int index)
Function
getForwardFunction()
Returns the FunctionList.Function which maps source elements to elements stored within this FunctionList.
Function
getReverseFunction()
Returns the FunctionList.Function which maps elements stored within this FunctionList back to elements within the source list or null if no such FunctionList.Function was specified.
protected boolean
isWritable()
void
listChanged(ListEvent listChanges)
E
remove(int index)
E
set(int index, E value)
void
setForwardFunction(Function forward)
Changes the FunctionList.Function that evaluates source elements to produce mapped elements.
void
setReverseFunction(Function reverse)
Changes the FunctionList.Function that evaluates FunctionList elements to produce the original source element with which it corresponds.

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

FunctionList

public FunctionList(EventList source,
                    Function forward)
Construct a FunctionList which stores the result of transforming each source element using the given forward FunctionList.Function. No reverse FunctionList.Function will be specified implying that add(Object), add(int, Object) and set(int, Object) will throw IllegalArgumentException if they are called.
Parameters:
source - the EventList to decorate with a function transformation
forward - the function to execute on each source element

FunctionList

public FunctionList(EventList source,
                    Function forward,
                    Function reverse)
Construct a FunctionList which stores the result of transforming each source element using the given forward FunctionList.Function. If the reverse FunctionList.Function is not null, add(Object), add(int, Object) and set(int, Object) will execute as expected.

Note: an FunctionList.AdvancedFunction can be specified for the forward FunctionList.Function which allows the implementor a chance to examine the prior value that was mapped to a source element when it must be remapped due to a modification (from a call to List.set).

Parameters:
source - the EventList to decorate with a function transformation
forward - the function to execute on each source element
reverse - the function to map elements of FunctionList back to element values in the source list

Method Details



getForwardFunction

public Function getForwardFunction()
Returns the FunctionList.Function which maps source elements to elements stored within this FunctionList. The FunctionList.Function is guaranteed to be non-null.

getReverseFunction

public Function getReverseFunction()
Returns the FunctionList.Function which maps elements stored within this FunctionList back to elements within the source list or null if no such FunctionList.Function was specified.





setForwardFunction

public void setForwardFunction(Function forward)
Changes the FunctionList.Function that evaluates source elements to produce mapped elements. Calling this method with a different forward Function will cause all elements in this FunctionList to be reevaluated.

Callers of this method typically also want to update the reverse function using setReverseFunction(Function) if one exists.


setReverseFunction

public void setReverseFunction(Function reverse)
Changes the FunctionList.Function that evaluates FunctionList elements to produce the original source element with which it corresponds. The reverse Function will be used in all subsequent calls to:
  • add(Object)
  • add(int, Object)
  • set(int, Object)
This method should typically be called at the same time the forward function is changed using setForwardFunction(Function).

Glazed Lists, Copyright © 2003 publicobject.com, O'Dell Engineering.
Documentation build by pbuilder at 2009-07-14 22:05