Class FunctionList<S,E>
- java.lang.Object
-
- ca.odell.glazedlists.AbstractEventList<E>
-
- ca.odell.glazedlists.TransformedList<S,E>
-
- ca.odell.glazedlists.FunctionList<S,E>
-
- All Implemented Interfaces:
ListEventListener<S>,EventList<E>,java.lang.Iterable<E>,java.util.Collection<E>,java.util.EventListener,java.util.List<E>,java.util.RandomAccess
public final class FunctionList<S,E> extends TransformedList<S,E> implements java.util.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 aFunctionList.Functionthat must be supplied at the time of construction but can be changed afterward usingsetForwardFunction(ca.odell.glazedlists.FunctionList.Function<S, E>). ThisFunctionList.Functionis called the forward function because it creates elements in thisFunctionListfrom elements that have been added or mutated within the source list. The forward function may be an implementation of eitherFunctionList.FunctionorFunctionList.AdvancedFunction.An optional reverse
If the reverseFunctionList.Functionwhich 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:FunctionList.Functionis not supplied then callers of those methods will receive anIllegalStateExceptionexplaining that those operations are not available without the reverseFunctionList.Function.If specified, the reverse
FunctionList.Functionshould 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.Functionto decideif and how to preserve the relationship of their identities after their transformation. Warning: This class is thread ready but not thread safe. See
EventListfor 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
Nested Classes Modifier and Type Class Description static interfaceFunctionList.AdvancedFunction<A,B>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 interfaceFunctionList.Function<A,B>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
source
-
Fields inherited from class ca.odell.glazedlists.AbstractEventList
publisher, readWriteLock, updates
-
-
Constructor Summary
Constructors Constructor Description FunctionList(EventList<S> source, FunctionList.Function<S,E> forward)Construct aFunctionListwhich stores the result of transforming each source element using the given forwardFunctionList.Function.FunctionList(EventList<S> source, FunctionList.Function<S,E> forward, FunctionList.Function<E,S> reverse)Construct aFunctionListwhich stores the result of transforming each source element using the given forwardFunctionList.Function.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, E value)Inserts the specified element at the specified position in this list (optional operation).Eget(int index)Returns the element at the specified position in this list.FunctionList.Function<S,E>getForwardFunction()Returns theFunctionList.Functionwhich maps source elements to elements stored within thisFunctionList.FunctionList.Function<E,S>getReverseFunction()Returns theFunctionList.Functionwhich maps elements stored within thisFunctionListback to elements within the source list ornullif no suchFunctionList.Functionwas specified.protected booleanisWritable()Gets whether the sourceEventListis writable via this API.voidlistChanged(ListEvent<S> listChanges)When the underlying list changes, this notification allows the object to repaint itself or update itself as necessary.Eremove(int index)Removes the element at the specified position in this list (optional operation).Eset(int index, E value)Replaces the element at the specified position in this list with the specified element (optional operation).voidsetForwardFunction(FunctionList.Function<S,E> forward)Changes theFunctionList.Functionthat evaluates source elements to produce mapped elements.voidsetReverseFunction(FunctionList.Function<E,S> reverse)Changes theFunctionList.Functionthat evaluates FunctionList elements to produce the original source element with which it corresponds.-
Methods inherited from class ca.odell.glazedlists.TransformedList
addAll, clear, dispose, getSourceIndex, removeAll, retainAll, size
-
Methods inherited from class ca.odell.glazedlists.AbstractEventList
add, addAll, addListEventListener, contains, containsAll, equals, getPublisher, getReadWriteLock, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeListEventListener, subList, toArray, toArray, toString
-
-
-
-
Constructor Detail
-
FunctionList
public FunctionList(EventList<S> source, FunctionList.Function<S,E> forward)
Construct aFunctionListwhich stores the result of transforming each source element using the given forwardFunctionList.Function. No reverseFunctionList.Functionwill be specified implying thatAbstractEventList.add(Object),add(int, Object)andset(int, Object)will throwIllegalArgumentExceptionif they are called.- Parameters:
source- the EventList to decorate with a function transformationforward- the function to execute on each source element
-
FunctionList
public FunctionList(EventList<S> source, FunctionList.Function<S,E> forward, FunctionList.Function<E,S> reverse)
Construct aFunctionListwhich stores the result of transforming each source element using the given forwardFunctionList.Function. If the reverseFunctionList.Functionis not null,AbstractEventList.add(Object),add(int, Object)andset(int, Object)will execute as expected.Note: an
FunctionList.AdvancedFunctioncan be specified for the forwardFunctionList.Functionwhich 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 toList.set(int, E)).- Parameters:
source- the EventList to decorate with a function transformationforward- the function to execute on each source elementreverse- the function to map elements of FunctionList back to element values in the source list
-
-
Method Detail
-
setForwardFunction
public void setForwardFunction(FunctionList.Function<S,E> forward)
Changes theFunctionList.Functionthat evaluates source elements to produce mapped elements. Calling this method with a differentforwardFunction will cause all elements in this FunctionList to be reevaluated.Callers of this method typically also want to update the reverse function using
setReverseFunction(ca.odell.glazedlists.FunctionList.Function<E, S>)if one exists.
-
getForwardFunction
public FunctionList.Function<S,E> getForwardFunction()
Returns theFunctionList.Functionwhich maps source elements to elements stored within thisFunctionList. TheFunctionList.Functionis guaranteed to be non-null.
-
setReverseFunction
public void setReverseFunction(FunctionList.Function<E,S> reverse)
Changes theFunctionList.Functionthat evaluates FunctionList elements to produce the original source element with which it corresponds. The reverse Function will be used in all subsequent calls to: This method should typically be called at the same time the forward function is changed usingsetForwardFunction(ca.odell.glazedlists.FunctionList.Function<S, E>).
-
getReverseFunction
public FunctionList.Function<E,S> getReverseFunction()
Returns theFunctionList.Functionwhich maps elements stored within thisFunctionListback to elements within the source list ornullif no suchFunctionList.Functionwas specified.
-
isWritable
protected boolean isWritable()
Gets whether the sourceEventListis writable via this API.Extending classes must override this method in order to make themselves writable.
- Specified by:
isWritablein classTransformedList<S,E>
-
listChanged
public void listChanged(ListEvent<S> listChanges)
When the underlying list changes, this notification allows the object to repaint itself or update itself as necessary.It is mandatory that the calling thread has obtained the write lock on the source list. This is because the calling thread will have written to the source list to cause this event. This condition guarantees that no writes can occur while the listener is handling this event. It is an error to write to the source list while processing an event.
- Specified by:
listChangedin interfaceListEventListener<S>- Specified by:
listChangedin classTransformedList<S,E>- Parameters:
listChanges- aListEventdescribing the changes to the list
-
get
public E get(int index)
Returns the element at the specified position in this list.- Specified by:
getin interfacejava.util.List<S>- Overrides:
getin classTransformedList<S,E>- Parameters:
index- index of element to return.- Returns:
- the element at the specified position in this list.
-
remove
public E remove(int index)
Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.- Specified by:
removein interfacejava.util.List<S>- Overrides:
removein classTransformedList<S,E>- Parameters:
index- the index of the element to removed.- Returns:
- the element previously at the specified position.
-
set
public E set(int index, E value)
Replaces the element at the specified position in this list with the specified element (optional operation).- Specified by:
setin interfacejava.util.List<S>- Overrides:
setin classTransformedList<S,E>- Parameters:
index- index of element to replace.value- element to be stored at the specified position.- Returns:
- the element previously at the specified position.
-
add
public void add(int index, E value)Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).- Specified by:
addin interfacejava.util.List<S>- Overrides:
addin classTransformedList<S,E>- Parameters:
index- index at which the specified element is to be inserted.value- element to be inserted.
-
-