org.apache.commons.events.observable.standard
Class StandardModificationHandler

java.lang.Object
  extended by org.apache.commons.events.observable.ModificationHandler
      extended by org.apache.commons.events.observable.standard.StandardModificationHandler

public class StandardModificationHandler
extends ModificationHandler

The standard implementation of a ModificationHandler that sends standard JavaBean style events to listeners.

The information gathered by this implementation is all that is available as parameters or return values. In addition, the size method is used on the collection. All objects used are the real objects from the method calls, not clones.

Each listener can be filtered. There are separate filters for pre and post modification events.

Since:
Commons Events 1.0
Version:
$Revision: 155443 $ $Date: 2005-02-26 13:19:51 +0000 (Sat, 26 Feb 2005) $
Author:
Stephen Colebourne

Nested Class Summary
protected static class StandardModificationHandler.PostHolder
           
protected static class StandardModificationHandler.PreHolder
           
 
Nested classes/interfaces inherited from class org.apache.commons.events.observable.ModificationHandler
ModificationHandler.SetViewHandler, ModificationHandler.SubListHandler
 
Field Summary
protected static StandardModificationHandler.PostHolder[] EMPTY_POST_HOLDERS
          A reusable empty holders array.
protected static StandardModificationHandler.PreHolder[] EMPTY_PRE_HOLDERS
          A reusable empty holders array.
static ModificationHandlerFactory FACTORY
          The singleton factory
protected  StandardModificationHandler.PostHolder[] postHolder
          The event listeners.
protected  int postMask
          The event mask as to which event types to send on post events.
protected  StandardModificationHandler.PreHolder[] preHolder
          The event listeners.
protected  int preMask
          The event mask as to which event types to send on pre events.
protected  int preSize
          Temporary store for the size.
 
Constructor Summary
StandardModificationHandler()
          Constructor the creates the handler but leaves it invalid.
StandardModificationHandler(StandardPreModificationListener pre, int preMask, StandardPostModificationListener post, int postMask)
          Constructor the creates the handler but leaves it invalid.
 
Method Summary
 void addPostModificationListener(Object listener)
          Adds a listener to the handler for post modification events.
 void addPostModificationListener(StandardPostModificationListener listener, int mask)
          Adds a post listener to the list held in the handler.
 void addPreModificationListener(Object listener)
          Adds a listener to the handler for pre modification events.
 void addPreModificationListener(StandardPreModificationListener listener, int mask)
          Adds a pre listener to the list held in the handler.
protected  void calculatePostMask()
          Calculate the combined masks.
protected  void calculatePreMask()
          Calculate the combined masks.
protected  void firePostEvent(int type, int index, Object object, int repeat, Object previous, ObservableCollection view, int viewOffset)
          Sends the post event to the listeners.
protected  boolean firePreEvent(int type, int index, Object object, int repeat, Object previous, ObservableCollection view, int viewOffset)
          Sends the pre event to the listeners.
 Object[] getPostModificationListeners()
          Gets an array of all the post listeners active in the handler.
 Object[] getPreModificationListeners()
          Gets an array of all the pre listeners active in the handler.
protected  void postClear()
          Send an event after clear() is called.
protected  void postEvent(boolean modified, int type, int index, Object object, int repeat, Object previous, ObservableCollection view, int viewOffset)
          Handles the post event.
protected  boolean preEvent(int type, int index, Object object, int repeat, Object previous, ObservableCollection view, int viewOffset)
          Handles the pre event.
 void removePostModificationListener(Object listener)
          Removes a post listener to the list held in the handler.
 void removePreModificationListener(Object listener)
          Removes a pre listener to the list held in the handler.
 void setPostModificationListenerMask(StandardPostModificationListener listener, int mask)
          Sets the masks of a listener.
 void setPreModificationListenerMask(StandardPreModificationListener listener, int mask)
          Sets the masks of a listener.
 
Methods inherited from class org.apache.commons.events.observable.ModificationHandler
createHeadSetHandler, createSubListHandler, createSubSetHandler, createTailSetHandler, getBaseCollection, getObservedCollection, getRootHandler, getViewOffset, postAdd, postAddAll, postAddAllIndexed, postAddIndexed, postAddIterated, postAddNCopies, postRemove, postRemoveAll, postRemoveIndexed, postRemoveIterated, postRemoveNCopies, postRemoveNext, postRetainAll, postSetIndexed, postSetIterated, preAdd, preAddAll, preAddAllIndexed, preAddIndexed, preAddIterated, preAddNCopies, preClear, preRemove, preRemoveAll, preRemoveIndexed, preRemoveIterated, preRemoveNCopies, preRemoveNext, preRetainAll, preSetIndexed, preSetIterated, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FACTORY

public static final ModificationHandlerFactory FACTORY
The singleton factory


EMPTY_PRE_HOLDERS

protected static final StandardModificationHandler.PreHolder[] EMPTY_PRE_HOLDERS
A reusable empty holders array.


EMPTY_POST_HOLDERS

protected static final StandardModificationHandler.PostHolder[] EMPTY_POST_HOLDERS
A reusable empty holders array.


preMask

protected int preMask
The event mask as to which event types to send on pre events.


postMask

protected int postMask
The event mask as to which event types to send on post events.


preHolder

protected StandardModificationHandler.PreHolder[] preHolder
The event listeners.


postHolder

protected StandardModificationHandler.PostHolder[] postHolder
The event listeners.


preSize

protected int preSize
Temporary store for the size. This makes the class thread-unsafe, but you should sync collections anyway.

Constructor Detail

StandardModificationHandler

public StandardModificationHandler()
Constructor the creates the handler but leaves it invalid.

The handler can only be used after it has been properly initialized. This is normally done automatically by ObservableCollection.decorate(Collection, Object).


StandardModificationHandler

public StandardModificationHandler(StandardPreModificationListener pre,
                                   int preMask,
                                   StandardPostModificationListener post,
                                   int postMask)
Constructor the creates the handler but leaves it invalid.

The handler can only be used after it has been properly initialized. This is normally done automatically by ObservableCollection.decorate(Collection, Object).

Parameters:
pre - the pre listener
preMask - the mask for the pre listener
post - the post listener
postMask - the mask for the post listener
Method Detail

getPreModificationListeners

public Object[] getPreModificationListeners()
Gets an array of all the pre listeners active in the handler.

All listeners will be instances of StandardPreModificationListener.

Overrides:
getPreModificationListeners in class ModificationHandler
Returns:
the listeners

addPreModificationListener

public void addPreModificationListener(Object listener)
Adds a listener to the handler for pre modification events.

No error occurs if the listener is null.

Overrides:
addPreModificationListener in class ModificationHandler
Parameters:
listener - the listener to add, may be null (ignored)
Throws:
ClassCastException - if the listener is not a StandardPreModificationListener

addPreModificationListener

public void addPreModificationListener(StandardPreModificationListener listener,
                                       int mask)
Adds a pre listener to the list held in the handler.

No error occurs if the listener is null.

Parameters:
listener - the listener to add, may be null (ignored)
mask - the mask for events (0 for none, -1 for all)

removePreModificationListener

public void removePreModificationListener(Object listener)
Removes a pre listener to the list held in the handler.

No error occurs if the listener is not in the list or the type of the listener is incorrect. The listener is matched using ==.

Overrides:
removePreModificationListener in class ModificationHandler
Parameters:
listener - the listener to remove, may be null (ignored)

setPreModificationListenerMask

public void setPreModificationListenerMask(StandardPreModificationListener listener,
                                           int mask)
Sets the masks of a listener.

No error occurs if the listener is not in the list. The listener is matched using ==.

Parameters:
listener - the listener to change, may be null
mask - the new mask (0 for none, -1 for all)

calculatePreMask

protected void calculatePreMask()
Calculate the combined masks.


getPostModificationListeners

public Object[] getPostModificationListeners()
Gets an array of all the post listeners active in the handler.

All listeners will be instances of StandardModificationListener.

Overrides:
getPostModificationListeners in class ModificationHandler
Returns:
the listeners

addPostModificationListener

public void addPostModificationListener(Object listener)
Adds a listener to the handler for post modification events.

No error occurs if the listener is null.

Overrides:
addPostModificationListener in class ModificationHandler
Parameters:
listener - the listener to add, may be null (ignored)
Throws:
ClassCastException - if the listener is not a StandardPreModificationListener

addPostModificationListener

public void addPostModificationListener(StandardPostModificationListener listener,
                                        int mask)
Adds a post listener to the list held in the handler.

No error occurs if the listener is null.

Parameters:
listener - the listener to add, may be null (ignored)
mask - the mask for events (0 for none, -1 for all)

removePostModificationListener

public void removePostModificationListener(Object listener)
Removes a post listener to the list held in the handler.

No error occurs if the listener is not in the list or the type of the listener is incorrect. The listener is matched using ==.

Overrides:
removePostModificationListener in class ModificationHandler
Parameters:
listener - the listener to remove, may be null (ignored)

setPostModificationListenerMask

public void setPostModificationListenerMask(StandardPostModificationListener listener,
                                            int mask)
Sets the masks of a listener.

No error occurs if the listener is not in the list. The listener is matched using ==.

Parameters:
listener - the listener to change, may be null
mask - the new mask (0 for none, -1 for all)

calculatePostMask

protected void calculatePostMask()
Calculate the combined masks.


preEvent

protected boolean preEvent(int type,
                           int index,
                           Object object,
                           int repeat,
                           Object previous,
                           ObservableCollection view,
                           int viewOffset)
Handles the pre event.

Overrides:
preEvent in class ModificationHandler
Parameters:
type - the event type to send
index - the index where the change starts, the method param or derived
object - the object that will be added/removed/set, the method param or derived
repeat - the number of repeats of the add/remove, the method param or derived
previous - the previous value that will be removed/replaced, must exist in coll
view - the view collection that the change was actioned on, null if no view
viewOffset - the offset of the subList view, -1 if unknown
Returns:
true to call the decorated collection

firePreEvent

protected boolean firePreEvent(int type,
                               int index,
                               Object object,
                               int repeat,
                               Object previous,
                               ObservableCollection view,
                               int viewOffset)
Sends the pre event to the listeners.

Parameters:
type - the event type to send
index - the index where the change starts, the method param or derived
object - the object that will be added/removed/set, the method param or derived
repeat - the number of repeats of the add/remove, the method param or derived
previous - the previous value that will be removed/replaced, must exist in coll
view - the view collection that the change was actioned on, null if no view
viewOffset - the offset of the subList view, -1 if unknown
Returns:
true to call the decorated collection

postEvent

protected void postEvent(boolean modified,
                         int type,
                         int index,
                         Object object,
                         int repeat,
                         Object previous,
                         ObservableCollection view,
                         int viewOffset)
Handles the post event.

Overrides:
postEvent in class ModificationHandler
Parameters:
modified - true if the method succeeded in changing the collection
type - the event type to send
index - the index where the change starts, the method param or derived
object - the object that was added/removed/set, the method param or derived
repeat - the number of repeats of the add/remove, the method param or derived
previous - the previous value that was removed/replace, must have existed in coll
view - the view collection that the change was actioned on, null if no view
viewOffset - the offset of the subList view, -1 if unknown

firePostEvent

protected void firePostEvent(int type,
                             int index,
                             Object object,
                             int repeat,
                             Object previous,
                             ObservableCollection view,
                             int viewOffset)
Sends the post event to the listeners.

Parameters:
type - the event type to send
index - the index where the change starts, the method param or derived
object - the object that was added/removed/set, the method param or derived
repeat - the number of repeats of the add/remove, the method param or derived
previous - the previous value that was removed/replace, must have existed in coll
view - the view collection that the change was actioned on, null if no view
viewOffset - the offset of the subList view, -1 if unknown

postClear

protected void postClear()
Send an event after clear() is called.

Override to only send event if something actually cleared.

Overrides:
postClear in class ModificationHandler


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.