Package ca.odell.glazedlists.matchers
Class ThreadedMatcherEditor<E>
- java.lang.Object
-
- ca.odell.glazedlists.matchers.AbstractMatcherEditorListenerSupport<E>
-
- ca.odell.glazedlists.matchers.ThreadedMatcherEditor<E>
-
- All Implemented Interfaces:
MatcherEditor<E>
public class ThreadedMatcherEditor<E> extends AbstractMatcherEditorListenerSupport<E>
A MatcherEditor which decorates a source MatcherEditor with functionality. Specifically, this MatcherEditor is meant to act as a buffer for MatcherEvents to smooth out disparities between the rate at which MatcherEvents are produced by the source MatcherEditor and the rate at which they are consumed by registered MatcherEditorListeners.Internally, a
ThreadedMatcherEditorenqueues MatcherEvents as they they are received from the source MatcherEditor. The MatcherEvents on the queue are fired by another Thread as rapidly as the MatcherEditorListeners can consume them. Two methods exist on this class which enable subclasses to customize aspects of processing queued MatcherEvents:-
executeMatcherEventQueueRunnable(Runnable)is consulted when a Thread must be selected to execute the given Runnable which will drain the queue of MatcherEvents. Subclasses may override to customize which Thread is used. -
coalesceMatcherEvents(List)is used to compress many enqueued MatcherEvents into a single representative MatcherEvent. This implies a contract between all registered MatcherEditorListeners and thisThreadedMatcherEditorthat guarantees that processing the coalesced MatcherEvent is equivalent to processing all MatcherEvents sequentially.
MatcherEditor threadedMatcherEditor = new ThreadedMatcherEditor(new AnyMatcherEditor()); FilterList filterList = new FilterList(new BasicEventList(), threadedMatcherEditor);
- Author:
- James Lemieux
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface ca.odell.glazedlists.matchers.MatcherEditor
MatcherEditor.Event<E>, MatcherEditor.Listener<E>
-
-
Constructor Summary
Constructors Constructor Description ThreadedMatcherEditor(MatcherEditor<E> source)Creates a ThreadedMatcherEditor which wraps the givensource.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected MatcherEditor.Event<E>coalesceMatcherEvents(java.util.List<MatcherEditor.Event<E>> matcherEvents)This method implements the strategy for coalescing many queued MatcherEvents into a single representative MatcherEvent.protected voidexecuteMatcherEventQueueRunnable(java.lang.Runnable runnable)This method executes the givenrunnableon a Thread.Matcher<E>getMatcher()Returns the current Matcher specified by the sourceMatcherEditor.-
Methods inherited from class ca.odell.glazedlists.matchers.AbstractMatcherEditorListenerSupport
addMatcherEditorListener, createChangedEvent, createConstrainedEvent, createMatchAllEvent, createMatchNoneEvent, createRelaxedEvent, fireChangedMatcher, removeMatcherEditorListener
-
-
-
-
Constructor Detail
-
ThreadedMatcherEditor
public ThreadedMatcherEditor(MatcherEditor<E> source)
Creates a ThreadedMatcherEditor which wraps the givensource. MatcherEvents fired from thesourcewill be enqueued within this MatcherEditor until they are processed on an alternate Thread.- Parameters:
source- the MatcherEditor to wrap with buffering functionality- Throws:
java.lang.NullPointerException- ifsourceisnull
-
-
Method Detail
-
getMatcher
public Matcher<E> getMatcher()
Returns the current Matcher specified by the sourceMatcherEditor.- Returns:
- the current Matcher specified by the source
MatcherEditor
-
coalesceMatcherEvents
protected MatcherEditor.Event<E> coalesceMatcherEvents(java.util.List<MatcherEditor.Event<E>> matcherEvents)
This method implements the strategy for coalescing many queued MatcherEvents into a single representative MatcherEvent. Listeners which process the MatcherEvent returned from this method should match the state that would exist if each of thematcherEventswere fired sequentially. In general, any group ofmatcherEventscan be succesfully coalesced as a single MatcherEvent with a type ofchanged, however, this method's default implementation uses a few heuristics to do more intelligent coalescing in order to gain speed improvements:- if
matcherEventsends in a MatcherEvent which is aMatcherEditor.Event.MATCH_ALLorMatcherEditor.Event.MATCH_NONEtype, the last MatcherEvent is returned, regardless of previous MatcherEvents - if
matcherEventsonly contains a series of monotonically constraining MatcherEvents, the final MatcherEvent is returned - if
matcherEventsonly contains a series of monotonically relaxing MatcherEvents, the final MatcherEvent is returned - if
matcherEventscontains both constraining and relaxing MatcherEvents, the final MatcherEvent is returned with its type asMatcherEditor.Event.CHANGED
1, 2,and3above merely represent safe optimizations of the type of MatcherEvent that can be returned. It could also have been returned as a MatcherEvent with a type ofMatcherEditor.Event.CHANGEDand be assumed to work correctly, though potentially less efficiently, since it is a more generic type of change.Subclasses with the ability to fire precise MatcherEvents with fine grain types (i.e.
relaxedorconstrained) when coalescingmatcherEventsin situations not recounted above may do so by overiding this method.- Parameters:
matcherEvents- an array of MatcherEvents recorded in the order they were received from the source MatcherEditor- Returns:
- a single MatcherEvent which, when fired, will result in the
same state as if all
matcherEventshad been fired sequentially
- if
-
executeMatcherEventQueueRunnable
protected void executeMatcherEventQueueRunnable(java.lang.Runnable runnable)
This method executes the givenrunnableon a Thread. The particular Thread chosen to execute the Runnable is left as an implementation detail. By default, a new Thread namedMatcherQueueThreadis constructed to execute therunnableeach time this method is called. Subclasses may override this method to use any Thread selection strategy they wish.- Parameters:
runnable- a Runnable to execute on an alternate Thread
-
-