From ca7f9fb3e3c1732836c62a75504fb8803d74d135 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 31 Aug 2003 22:44:54 +0000 Subject: [PATCH] Refactor events so pre and post more separated git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131127 13f79535-47bb-0310-9956-ffa450edef68 --- .../decorators/ObservedCollection.java | 152 +++++---- .../collections/decorators/ObservedList.java | 126 +++---- .../collections/decorators/ObservedSet.java | 128 +++---- .../event/ModificationHandler.java | 67 +++- .../event/ModificationListener.java | 77 +++++ .../event/StandardModificationHandler.java | 311 +++++++++++++----- .../event/StandardModificationListener.java | 44 +-- .../StandardPostModificationListener.java | 82 +++++ ...a => StandardPreModificationListener.java} | 31 +- .../decorators/ObservedTestHelper.java | 128 +++++-- .../decorators/TestObservedCollection.java | 96 +++--- .../decorators/TestObservedList.java | 96 +++--- .../decorators/TestObservedSet.java | 96 +++--- 13 files changed, 938 insertions(+), 496 deletions(-) create mode 100644 src/java/org/apache/commons/collections/event/ModificationListener.java create mode 100644 src/java/org/apache/commons/collections/event/StandardPostModificationListener.java rename src/java/org/apache/commons/collections/event/{StandardModificationAdaptor.java => StandardPreModificationListener.java} (80%) diff --git a/src/java/org/apache/commons/collections/decorators/ObservedCollection.java b/src/java/org/apache/commons/collections/decorators/ObservedCollection.java index 92c740f35..fcea6c933 100644 --- a/src/java/org/apache/commons/collections/decorators/ObservedCollection.java +++ b/src/java/org/apache/commons/collections/decorators/ObservedCollection.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/ObservedCollection.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/ObservedCollection.java,v 1.4 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -63,6 +63,8 @@ import java.util.Iterator; import org.apache.commons.collections.event.ModificationHandler; import org.apache.commons.collections.event.StandardModificationHandler; import org.apache.commons.collections.event.StandardModificationListener; +import org.apache.commons.collections.event.StandardPostModificationListener; +import org.apache.commons.collections.event.StandardPreModificationListener; /** * ObservedCollection decorates a Collection @@ -81,7 +83,7 @@ import org.apache.commons.collections.event.StandardModificationListener; * uses a technique other than listeners to communicate events. * * @since Commons Collections 3.0 - * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $ + * @version $Revision: 1.4 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ @@ -108,7 +110,7 @@ public class ObservedCollection extends AbstractCollectionDecorator { /** * Factory method to create an observable collection and register one - * listener to receive all events. + * listener to receive events before the change is made. *

* A {@link StandardModificationHandler} will be created. * The listener will be added to the handler. @@ -120,62 +122,74 @@ public class ObservedCollection extends AbstractCollectionDecorator { */ public static ObservedCollection decorate( final Collection coll, - final StandardModificationListener listener) { + final StandardPreModificationListener listener) { - return decorate(coll, listener, -1, -1); - } - - /** - * Factory method to create an observable collection and register one - * listener to receive all post events. - *

- * A {@link StandardModificationHandler} will be created. - * The listener will be added to the handler. - * - * @param coll the collection to decorate, must not be null - * @param listener collection listener, must not be null - * @return the observed collection - * @throws IllegalArgumentException if the collection or listener is null - */ - public static ObservedCollection decoratePostEventsOnly( - final Collection coll, - final StandardModificationListener listener) { - - return decorate(coll, listener, 0, -1); - } - - /** - * Factory method to create an observable collection and - * register one listener using event masks. - *

- * A {@link StandardModificationHandler} will be created. - * The listener will be added to the handler. - * The masks are defined in - * {@link org.apache.commons.collections.event.ModificationEventType ModificationEventType}. - * - * @param coll the collection to decorate, must not be null - * @param listener collection listener, must not be null - * @param preEventMask mask for pre events (0 for none, -1 for all) - * @param postEventMask mask for post events (0 for none, -1 for all) - * @return the observed collection - * @throws IllegalArgumentException if the collection or listener is null - */ - public static ObservedCollection decorate( - final Collection coll, - final StandardModificationListener listener, - final int preEventMask, - final int postEventMask) { - if (coll == null) { throw new IllegalArgumentException("Collection must not be null"); } if (listener == null) { throw new IllegalArgumentException("Listener must not be null"); } - StandardModificationHandler handler = new StandardModificationHandler(); - ObservedCollection oc = new ObservedCollection(coll, handler); - handler.addModificationListener(listener, preEventMask, postEventMask); - return oc; + StandardModificationHandler handler = new StandardModificationHandler( + listener, -1, null, 0 + ); + return new ObservedCollection(coll, handler); + } + + /** + * Factory method to create an observable collection and register one + * listener to receive events after the change is made. + *

+ * A {@link StandardModificationHandler} will be created. + * The listener will be added to the handler. + * + * @param coll the collection to decorate, must not be null + * @param listener collection listener, must not be null + * @return the observed collection + * @throws IllegalArgumentException if the collection or listener is null + */ + public static ObservedCollection decorate( + final Collection coll, + final StandardPostModificationListener listener) { + + if (coll == null) { + throw new IllegalArgumentException("Collection must not be null"); + } + if (listener == null) { + throw new IllegalArgumentException("Listener must not be null"); + } + StandardModificationHandler handler = new StandardModificationHandler( + null, 0, listener, -1 + ); + return new ObservedCollection(coll, handler); + } + + /** + * Factory method to create an observable collection and register one + * listener to receive events both before and after the change is made. + *

+ * A {@link StandardModificationHandler} will be created. + * The listener will be added to the handler. + * + * @param coll the collection to decorate, must not be null + * @param listener collection listener, must not be null + * @return the observed collection + * @throws IllegalArgumentException if the collection or listener is null + */ + public static ObservedCollection decorate( + final Collection coll, + final StandardModificationListener listener) { + + if (coll == null) { + throw new IllegalArgumentException("Collection must not be null"); + } + if (listener == null) { + throw new IllegalArgumentException("Listener must not be null"); + } + StandardModificationHandler handler = new StandardModificationHandler( + listener, -1, listener, -1 + ); + return new ObservedCollection(coll, handler); } /** @@ -237,18 +251,7 @@ public class ObservedCollection extends AbstractCollectionDecorator { // Listener convenience methods //---------------------------------------------------------------------- /** - * Gets an array of all the listeners active in the handler. - * This method simply delegates to the handler. - * - * @return the listeners - * @throws UnsupportedOperationException if the handler does not support listeners - */ - public Object[] getModificationListeners() { - return getHandler().getModificationListeners(); - } - - /** - * Adds a listener to the list held in the handler. + * Adds a listener to the handler to receive pre modification events. * This method simply delegates to the handler. *

* No error occurs if the listener is null. @@ -262,24 +265,27 @@ public class ObservedCollection extends AbstractCollectionDecorator { * @throws ClassCastException if the listener is not of the correct type * @throws UnsupportedOperationException if the handler does not support listeners */ - public void addModificationListener(Object listener) { - getHandler().addModificationListener(listener); + public void addPreModificationListener(Object listener) { + getHandler().addPreModificationListener(listener); } /** - * Removes a listener to the list held in the handler. + * Adds a listener to the handler to receive post modification events. * This method simply delegates to the handler. *

- * No error occurs if the listener is not in the list or the type - * of the listener is incorrect. + * No error occurs if the listener is null. *

- * This implementation throws UnsupportedOperationException. + * The listener does not necessarily have to be a listener in the classic + * JavaBean sense. It is entirely up to the handler as to how it interprets + * the listener parameter. A ClassCastException is thrown if the handler + * cannot interpret the parameter. * - * @param listener the listener to remove, may be null (ignored) + * @param listener the listener to add, may be null (ignored) + * @throws ClassCastException if the listener is not of the correct type * @throws UnsupportedOperationException if the handler does not support listeners */ - public void removeModificationListener(Object listener) { - getHandler().removeModificationListener(listener); + public void addPostModificationListener(Object listener) { + getHandler().addPostModificationListener(listener); } // Collection diff --git a/src/java/org/apache/commons/collections/decorators/ObservedList.java b/src/java/org/apache/commons/collections/decorators/ObservedList.java index fb7d77be1..ef3356dff 100644 --- a/src/java/org/apache/commons/collections/decorators/ObservedList.java +++ b/src/java/org/apache/commons/collections/decorators/ObservedList.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/ObservedList.java,v 1.2 2003/08/31 17:24:46 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/ObservedList.java,v 1.3 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -64,6 +64,8 @@ import java.util.ListIterator; import org.apache.commons.collections.event.ModificationHandler; import org.apache.commons.collections.event.StandardModificationHandler; import org.apache.commons.collections.event.StandardModificationListener; +import org.apache.commons.collections.event.StandardPostModificationListener; +import org.apache.commons.collections.event.StandardPreModificationListener; /** * ObservedList decorates a List @@ -82,7 +84,7 @@ import org.apache.commons.collections.event.StandardModificationListener; * uses a technique other than listeners to communicate events. * * @since Commons Collections 3.0 - * @version $Revision: 1.2 $ $Date: 2003/08/31 17:24:46 $ + * @version $Revision: 1.3 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ @@ -106,74 +108,86 @@ public class ObservedList extends ObservedCollection implements List { /** * Factory method to create an observable list and register one - * listener to receive all events. + * listener to receive events before the change is made. *

* A {@link StandardModificationHandler} will be created. * The listener will be added to the handler. * - * @param list the list to decorate, must not be null - * @param listener the listener, must not be null + * @param coll the list to decorate, must not be null + * @param listener list listener, must not be null * @return the observed list * @throws IllegalArgumentException if the list or listener is null */ public static ObservedList decorate( - final List list, - final StandardModificationListener listener) { + final List coll, + final StandardPreModificationListener listener) { - return decorate(list, listener, -1, -1); - } - - /** - * Factory method to create an observable list and register one - * listener to receive all post events. - *

- * A {@link StandardModificationHandler} will be created. - * The listener will be added to the handler. - * - * @param list the list to decorate, must not be null - * @param listener the listener, must not be null - * @return the observed list - * @throws IllegalArgumentException if the list or listener is null - */ - public static ObservedList decoratePostEventsOnly( - final List list, - final StandardModificationListener listener) { - - return decorate(list, listener, 0, -1); - } - - /** - * Factory method to create an observable list and - * register one listener using event masks. - *

- * A {@link StandardModificationHandler} will be created. - * The listener will be added to the handler. - * The masks are defined in - * {@link org.apache.commons.collections.event.ModificationEventType ModificationEventType}. - * - * @param list the list to decorate, must not be null - * @param listener the listener, must not be null - * @param preEventMask mask for pre events (0 for none, -1 for all) - * @param postEventMask mask for post events (0 for none, -1 for all) - * @return the observed list - * @throws IllegalArgumentException if the list or listener is null - */ - public static ObservedList decorate( - final List list, - final StandardModificationListener listener, - final int preEventMask, - final int postEventMask) { - - if (list == null) { + if (coll == null) { throw new IllegalArgumentException("List must not be null"); } if (listener == null) { throw new IllegalArgumentException("Listener must not be null"); } - StandardModificationHandler handler = new StandardModificationHandler(); - ObservedList oc = new ObservedList(list, handler); - handler.addModificationListener(listener, preEventMask, postEventMask); - return oc; + StandardModificationHandler handler = new StandardModificationHandler( + listener, -1, null, 0 + ); + return new ObservedList(coll, handler); + } + + /** + * Factory method to create an observable list and register one + * listener to receive events after the change is made. + *

+ * A {@link StandardModificationHandler} will be created. + * The listener will be added to the handler. + * + * @param coll the list to decorate, must not be null + * @param listener list listener, must not be null + * @return the observed list + * @throws IllegalArgumentException if the list or listener is null + */ + public static ObservedList decorate( + final List coll, + final StandardPostModificationListener listener) { + + if (coll == null) { + throw new IllegalArgumentException("List must not be null"); + } + if (listener == null) { + throw new IllegalArgumentException("Listener must not be null"); + } + StandardModificationHandler handler = new StandardModificationHandler( + null, 0, listener, -1 + ); + return new ObservedList(coll, handler); + } + + /** + * Factory method to create an observable list and register one + * listener to receive events both before and after the change is made. + *

+ * A {@link StandardModificationHandler} will be created. + * The listener will be added to the handler. + * + * @param coll the list to decorate, must not be null + * @param listener list listener, must not be null + * @return the observed list + * @throws IllegalArgumentException if the list or listener is null + */ + public static ObservedList decorate( + final List coll, + final StandardModificationListener listener) { + + if (coll == null) { + throw new IllegalArgumentException("List must not be null"); + } + if (listener == null) { + throw new IllegalArgumentException("Listener must not be null"); + } + StandardModificationHandler handler = new StandardModificationHandler( + listener, -1, listener, -1 + ); + return new ObservedList(coll, handler); } /** diff --git a/src/java/org/apache/commons/collections/decorators/ObservedSet.java b/src/java/org/apache/commons/collections/decorators/ObservedSet.java index 64ec35762..f63121822 100644 --- a/src/java/org/apache/commons/collections/decorators/ObservedSet.java +++ b/src/java/org/apache/commons/collections/decorators/ObservedSet.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/ObservedSet.java,v 1.2 2003/08/31 17:24:46 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/ObservedSet.java,v 1.3 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -62,6 +62,8 @@ import java.util.Set; import org.apache.commons.collections.event.ModificationHandler; import org.apache.commons.collections.event.StandardModificationHandler; import org.apache.commons.collections.event.StandardModificationListener; +import org.apache.commons.collections.event.StandardPostModificationListener; +import org.apache.commons.collections.event.StandardPreModificationListener; /** * ObservedSet decorates a Set @@ -80,7 +82,7 @@ import org.apache.commons.collections.event.StandardModificationListener; * uses a technique other than listeners to communicate events. * * @since Commons Collections 3.0 - * @version $Revision: 1.2 $ $Date: 2003/08/31 17:24:46 $ + * @version $Revision: 1.3 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ @@ -104,74 +106,86 @@ public class ObservedSet extends ObservedCollection implements Set { /** * Factory method to create an observable set and register one - * listener to receive all events. + * listener to receive events before the change is made. *

* A {@link StandardModificationHandler} will be created. * The listener will be added to the handler. * - * @param set the set to decorate, must not be null - * @param listener the listener, must not be null + * @param coll the set to decorate, must not be null + * @param listener set listener, must not be null * @return the observed set * @throws IllegalArgumentException if the set or listener is null */ public static ObservedSet decorate( - final Set set, - final StandardModificationListener listener) { + final Set coll, + final StandardPreModificationListener listener) { - return decorate(set, listener, -1, -1); - } - - /** - * Factory method to create an observable set and register one - * listener to receive all post events. - *

- * A {@link StandardModificationHandler} will be created. - * The listener will be added to the handler. - * - * @param set the set to decorate, must not be null - * @param listener the listener, must not be null - * @return the observed set - * @throws IllegalArgumentException if the set or listener is null - */ - public static ObservedSet decoratePostEventsOnly( - final Set set, - final StandardModificationListener listener) { - - return decorate(set, listener, 0, -1); - } - - /** - * Factory method to create an observable set and - * register one listener using event masks. - *

- * A {@link StandardModificationHandler} will be created. - * The listener will be added to the handler. - * The masks are defined in - * {@link org.apache.commons.collections.event.ModificationEventType ModificationEventType}. - * - * @param set the set to decorate, must not be null - * @param listener the listener, must not be null - * @param preEventMask mask for pre events (0 for none, -1 for all) - * @param postEventMask mask for post events (0 for none, -1 for all) - * @return the observed set - * @throws IllegalArgumentException if the set or listener is null - */ - public static ObservedSet decorate( - final Set set, - final StandardModificationListener listener, - final int preEventMask, - final int postEventMask) { - - if (set == null) { + if (coll == null) { throw new IllegalArgumentException("Set must not be null"); } if (listener == null) { throw new IllegalArgumentException("Listener must not be null"); } - StandardModificationHandler handler = new StandardModificationHandler(); - ObservedSet oc = new ObservedSet(set, handler); - handler.addModificationListener(listener, preEventMask, postEventMask); - return oc; + StandardModificationHandler handler = new StandardModificationHandler( + listener, -1, null, 0 + ); + return new ObservedSet(coll, handler); + } + + /** + * Factory method to create an observable set and register one + * listener to receive events after the change is made. + *

+ * A {@link StandardModificationHandler} will be created. + * The listener will be added to the handler. + * + * @param coll the set to decorate, must not be null + * @param listener set listener, must not be null + * @return the observed set + * @throws IllegalArgumentException if the set or listener is null + */ + public static ObservedSet decorate( + final Set coll, + final StandardPostModificationListener listener) { + + if (coll == null) { + throw new IllegalArgumentException("Set must not be null"); + } + if (listener == null) { + throw new IllegalArgumentException("Listener must not be null"); + } + StandardModificationHandler handler = new StandardModificationHandler( + null, 0, listener, -1 + ); + return new ObservedSet(coll, handler); + } + + /** + * Factory method to create an observable set and register one + * listener to receive events both before and after the change is made. + *

+ * A {@link StandardModificationHandler} will be created. + * The listener will be added to the handler. + * + * @param coll the set to decorate, must not be null + * @param listener set listener, must not be null + * @return the observed set + * @throws IllegalArgumentException if the set or listener is null + */ + public static ObservedSet decorate( + final Set coll, + final StandardModificationListener listener) { + + if (coll == null) { + throw new IllegalArgumentException("Set must not be null"); + } + if (listener == null) { + throw new IllegalArgumentException("Listener must not be null"); + } + StandardModificationHandler handler = new StandardModificationHandler( + listener, -1, listener, -1 + ); + return new ObservedSet(coll, handler); } /** @@ -209,7 +223,7 @@ public class ObservedSet extends ObservedCollection implements Set { * * @param set the set to decorate, must not be null * @param handler the observing handler, may be null - * @throws IllegalArgumentException if the collection is null + * @throws IllegalArgumentException if the set is null */ protected ObservedSet( final Set set, diff --git a/src/java/org/apache/commons/collections/event/ModificationHandler.java b/src/java/org/apache/commons/collections/event/ModificationHandler.java index ba9620d0b..8efb66efd 100644 --- a/src/java/org/apache/commons/collections/event/ModificationHandler.java +++ b/src/java/org/apache/commons/collections/event/ModificationHandler.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/ModificationHandler.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/ModificationHandler.java,v 1.4 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -69,7 +69,7 @@ import org.apache.commons.collections.decorators.ObservedCollection; * that forwards to single points. * * @since Commons Collections 3.0 - * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $ + * @version $Revision: 1.4 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ @@ -119,22 +119,22 @@ public abstract class ModificationHandler { return collection; } - // Listeners + // PreListeners //---------------------------------------------------------------------- /** - * Gets an array of all the listeners active in the handler. + * Gets an array of all the pre listeners active in the handler. *

* This implementation throws UnsupportedOperationException. * * @return the listeners * @throws UnsupportedOperationException if the handler does not support listeners */ - public Object[] getModificationListeners() { + public Object[] getPreModificationListeners() { throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName()); } /** - * Adds a listener to the list held in the handler. + * Adds a pre listener to the list held in the handler. *

* No error occurs if the listener is null. *

@@ -149,12 +149,12 @@ public abstract class ModificationHandler { * @throws ClassCastException if the listener is not of the correct type * @throws UnsupportedOperationException if the handler does not support listeners */ - public void addModificationListener(Object listener) { + public void addPreModificationListener(Object listener) { throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName()); } /** - * Removes a listener to the list held in the handler. + * 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. @@ -164,7 +164,56 @@ public abstract class ModificationHandler { * @param listener the listener to remove, may be null (ignored) * @throws UnsupportedOperationException if the handler does not support listeners */ - public void removeModificationListener(Object listener) { + public void removePreModificationListener(Object listener) { + throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName()); + } + + // PostListeners + //---------------------------------------------------------------------- + /** + * Gets an array of all the post listeners active in the handler. + *

+ * This implementation throws UnsupportedOperationException. + * + * @return the listeners + * @throws UnsupportedOperationException if the handler does not support listeners + */ + public Object[] getPostModificationListeners() { + throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName()); + } + + /** + * Adds a post listener to the list held in the handler. + *

+ * No error occurs if the listener is null. + *

+ * The listener does not necessarily have to be a listener in the classic + * JavaBean sense. It is entirely up to the handler as to how it interprets + * the listener parameter. A ClassCastException is thrown if the handler + * cannot interpret the parameter. + *

+ * This implementation throws UnsupportedOperationException. + * + * @param listener the listener to add, may be null (ignored) + * @throws ClassCastException if the listener is not of the correct type + * @throws UnsupportedOperationException if the handler does not support listeners + */ + public void addPostModificationListener(Object listener) { + throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName()); + } + + /** + * 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. + *

+ * This implementation throws UnsupportedOperationException. + * + * @param listener the listener to remove, may be null (ignored) + * @throws UnsupportedOperationException if the handler does not support listeners + */ + public void removePostModificationListener(Object listener) { throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName()); } diff --git a/src/java/org/apache/commons/collections/event/ModificationListener.java b/src/java/org/apache/commons/collections/event/ModificationListener.java new file mode 100644 index 000000000..9b4fee3e3 --- /dev/null +++ b/src/java/org/apache/commons/collections/event/ModificationListener.java @@ -0,0 +1,77 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/ModificationListener.java,v 1.4 2003/08/31 22:44:54 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.event; + +import java.util.EventListener; + +/** + * An empty listener designed to be subclassed. + *

+ * This interface exists to mark independent subclasses as fulfilling the + * role of an event listener for collection modification events. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.4 $ $Date: 2003/08/31 22:44:54 $ + * + * @author Stephen Colebourne + */ +public interface ModificationListener extends EventListener { + + // no methods - subinterfaces define them + +} diff --git a/src/java/org/apache/commons/collections/event/StandardModificationHandler.java b/src/java/org/apache/commons/collections/event/StandardModificationHandler.java index bbc9b6e30..c79df453f 100644 --- a/src/java/org/apache/commons/collections/event/StandardModificationHandler.java +++ b/src/java/org/apache/commons/collections/event/StandardModificationHandler.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/StandardModificationHandler.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/StandardModificationHandler.java,v 1.4 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -66,35 +66,30 @@ package org.apache.commons.collections.event; * 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 events - * (modificationOccurring) and post events (modificationOccurred). - *

- * This implementation is the standard one. Most listeners will probably be - * content with the events generated from here. However, if you need something - * extra then this class can be subclassed or replaced as required. For example: - *

+ * Each listener can be filtered. There are separate filters for pre and post + * modification events. * * @since Commons Collections 3.0 - * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $ + * @version $Revision: 1.4 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ public class StandardModificationHandler extends ModificationHandler { /** A reusable empty holders array. */ - protected static final Holder[] EMPTY_HOLDERS = new Holder[0]; + protected static final PreHolder[] EMPTY_PRE_HOLDERS = new PreHolder[0]; + /** A reusable empty holders array. */ + protected static final PostHolder[] EMPTY_POST_HOLDERS = new PostHolder[0]; /** The event mask as to which event types to send on pre events. */ protected int preMask = ModificationEventType.GROUP_NONE; /** The event mask as to which event types to send on post events. */ protected int postMask = ModificationEventType.GROUP_NONE; + /** The event listeners. */ - protected Holder[] holders = EMPTY_HOLDERS; + protected PreHolder[] preHolder = EMPTY_PRE_HOLDERS; + /** The event listeners. */ + protected PostHolder[] postHolder = EMPTY_POST_HOLDERS; /** * Temporary store for the size. * This makes the class thread-unsafe, but you should sync collections anyway. @@ -114,57 +109,84 @@ public class StandardModificationHandler extends ModificationHandler { super(); } - // Listeners + /** + * Constructor the creates the handler but leaves it invalid. + *

+ * The handler can only be used after {@link #init(ObservedCollection)} is + * called. This is normally done automatically by + * {@link ObservedCollection#decorate(Collection, ModificationHandler)}. + * + * @param pre the pre listener + * @param preMask the mask for the pre listener + * @param post the post listener + * @param postMask the mask for the post listener + */ + public StandardModificationHandler( + StandardPreModificationListener pre, int preMask, + StandardPostModificationListener post, int postMask) { + super(); + if (pre != null) { + preHolder = new PreHolder[1]; + preHolder[0] = new PreHolder(pre, preMask); + this.preMask = preMask; + } + if (post != null) { + postHolder = new PostHolder[1]; + postHolder[0] = new PostHolder(post, postMask); + this.postMask = postMask; + } + } + + // Pre Listeners //---------------------------------------------------------------------- /** - * Gets an array of all the listeners active in the handler. + * Gets an array of all the pre listeners active in the handler. *

- * All listeners will be instances of StandardModificationListener. + * All listeners will be instances of StandardPreModificationListener. * * @return the listeners */ - public synchronized Object[] getModificationListeners() { - Object[] lnrs = new Object[holders.length]; - for (int i = 0; i < holders.length; i++) { - lnrs[i] = holders[i].listener; + public synchronized Object[] getPreModificationListeners() { + Object[] lnrs = new Object[preHolder.length]; + for (int i = 0; i < preHolder.length; i++) { + lnrs[i] = preHolder[i].listener; } return lnrs; } /** - * Adds a listener to the list held in the handler. + * Adds a listener to the handler for pre modification events. *

* No error occurs if the listener is null. * * @param listener the listener to add, may be null (ignored) - * @throws ClassCastException if the listener is not a StandardModificationListener + * @throws ClassCastException if the listener is not a StandardPreModificationListener */ - public void addModificationListener(Object listener) { - addModificationListener((StandardModificationListener) listener, -1, -1); + public void addPreModificationListener(Object listener) { + addPreModificationListener((StandardPreModificationListener) listener, -1); } /** - * Adds a listener to the list held in the handler. + * Adds a pre listener to the list held in the handler. *

* No error occurs if the listener is null. * * @param listener the listener to add, may be null (ignored) - * @param preMask the mask for pre events (0 for none, -1 for all) - * @param postMask the mask for post events (0 for none, -1 for all) + * @param mask the mask for events (0 for none, -1 for all) */ - public synchronized void addModificationListener(StandardModificationListener listener, int preMask, int postMask) { + public synchronized void addPreModificationListener(StandardPreModificationListener listener, int mask) { if (listener != null) { - int oldSize = holders.length; - Holder[] array = new Holder[oldSize + 1]; - System.arraycopy(holders, 0, array, 0, oldSize); - array[oldSize] = new Holder(listener, preMask, postMask); - holders = array; - calculateMasks(); + int oldSize = preHolder.length; + PreHolder[] array = new PreHolder[oldSize + 1]; + System.arraycopy(preHolder, 0, array, 0, oldSize); + array[oldSize] = new PreHolder(listener, mask); + preHolder = array; + calculatePreMask(); } } /** - * Removes a listener to the list held in the handler. + * 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. @@ -172,33 +194,33 @@ public class StandardModificationHandler extends ModificationHandler { * * @param listener the listener to remove, may be null (ignored) */ - public synchronized void removeModificationListener(Object listener) { + public synchronized void removePreModificationListener(Object listener) { if (listener != null) { - switch (holders.length) { + switch (preHolder.length) { case 0: return; case 1: - if (holders[0].listener == listener) { - holders = EMPTY_HOLDERS; - calculateMasks(); + if (preHolder[0].listener == listener) { + preHolder = EMPTY_PRE_HOLDERS; + calculatePreMask(); } return; default: - Holder[] array = new Holder[holders.length - 1]; + PreHolder[] array = new PreHolder[preHolder.length - 1]; boolean match = false; - for (int i = 0; i < holders.length; i++) { + for (int i = 0; i < preHolder.length; i++) { if (match) { - array[i - 1] = holders[i]; - } else if (holders[i].listener == listener) { + array[i - 1] = preHolder[i]; + } else if (preHolder[i].listener == listener) { match = true; } else { - array[i] = holders[i]; + array[i] = preHolder[i]; } } - holders = array; - calculateMasks(); + preHolder = array; + calculatePreMask(); return; } } @@ -210,56 +232,183 @@ public class StandardModificationHandler extends ModificationHandler { * No error occurs if the listener is not in the list. * The listener is matched using ==. * + * @param listener the listener to change, may be null + * @param mask the new mask (0 for none, -1 for all) * @return a non-null array of listeners */ - public synchronized void setModificationListenerMasks(StandardModificationListener listener, int preMask, int postMask) { + public synchronized void setPreModificationListenerMask(StandardPreModificationListener listener, int mask) { if (listener != null) { - for (int i = 0; i < holders.length; i++) { - if (holders[i].listener == listener) { - holders[i].preMask = preMask; - holders[i].postMask = postMask; - calculateMasks(); + for (int i = 0; i < preHolder.length; i++) { + if (preHolder[i].listener == listener) { + preHolder[i].mask = mask; + calculatePreMask(); break; } } } } - // Holder for listener and masks - //----------------------------------------------------------------------- - protected static class Holder { - StandardModificationListener listener; - int preMask; - int postMask; + /** + * Calculate the combined masks. + */ + protected void calculatePreMask() { + preMask = ModificationEventType.GROUP_NONE; + for (int i = 0; i < preHolder.length; i++) { + preMask |= preHolder[i].mask; + } + } + + protected static class PreHolder { + final StandardPreModificationListener listener; + int mask; - Holder(StandardModificationListener listener, int preMask, int postMask) { + PreHolder(StandardPreModificationListener listener, int mask) { this.listener = listener; - this.preMask = preMask; - this.postMask = postMask; + this.mask = mask; } public String toString() { - return "[" + listener + "," - + ModificationEventType.toString(preMask) + "," - + ModificationEventType.toString(postMask) + "]"; + return "[" + listener + "," + ModificationEventType.toString(mask) + "]"; } } - // Masks - //----------------------------------------------------------------------- + // Post Listeners + //---------------------------------------------------------------------- + /** + * Gets an array of all the post listeners active in the handler. + *

+ * All listeners will be instances of StandardModificationListener. + * + * @return the listeners + */ + public synchronized Object[] getPostModificationListeners() { + Object[] lnrs = new Object[postHolder.length]; + for (int i = 0; i < postHolder.length; i++) { + lnrs[i] = postHolder[i].listener; + } + return lnrs; + } + + /** + * Adds a listener to the handler for post modification events. + *

+ * No error occurs if the listener is null. + * + * @param listener the listener to add, may be null (ignored) + * @throws ClassCastException if the listener is not a StandardPreModificationListener + */ + public void addPostModificationListener(Object listener) { + addPostModificationListener((StandardPostModificationListener) listener, -1); + } + + /** + * Adds a post listener to the list held in the handler. + *

+ * No error occurs if the listener is null. + * + * @param listener the listener to add, may be null (ignored) + * @param mask the mask for events (0 for none, -1 for all) + */ + public synchronized void addPostModificationListener(StandardPostModificationListener listener, int mask) { + if (listener != null) { + int oldSize = postHolder.length; + PostHolder[] array = new PostHolder[oldSize + 1]; + System.arraycopy(postHolder, 0, array, 0, oldSize); + array[oldSize] = new PostHolder(listener, mask); + postHolder = array; + calculatePostMask(); + } + } + + /** + * 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 ==. + * + * @param listener the listener to remove, may be null (ignored) + */ + public synchronized void removePostModificationListener(Object listener) { + if (listener != null) { + switch (postHolder.length) { + case 0: + return; + + case 1: + if (postHolder[0].listener == listener) { + postHolder = EMPTY_POST_HOLDERS; + calculatePostMask(); + } + return; + + default: + PostHolder[] array = new PostHolder[postHolder.length - 1]; + boolean match = false; + for (int i = 0; i < postHolder.length; i++) { + if (match) { + array[i - 1] = postHolder[i]; + } else if (postHolder[i].listener == listener) { + match = true; + } else { + array[i] = postHolder[i]; + } + } + postHolder = array; + calculatePostMask(); + return; + } + } + } + + /** + * Sets the masks of a listener. + *

+ * No error occurs if the listener is not in the list. + * The listener is matched using ==. + * + * @param listener the listener to change, may be null + * @param mask the new mask (0 for none, -1 for all) + * @return a non-null array of listeners + */ + public synchronized void setPostModificationListenerMask(StandardPostModificationListener listener, int mask) { + if (listener != null) { + for (int i = 0; i < postHolder.length; i++) { + if (postHolder[i].listener == listener) { + postHolder[i].mask = mask; + calculatePostMask(); + break; + } + } + } + } + /** * Calculate the combined masks. */ - protected void calculateMasks() { - preMask = ModificationEventType.GROUP_NONE; + protected void calculatePostMask() { postMask = ModificationEventType.GROUP_NONE; - for (int i = 0; i < holders.length; i++) { - preMask |= holders[i].preMask; - postMask |= holders[i].postMask; + for (int i = 0; i < postHolder.length; i++) { + postMask |= postHolder[i].mask; } } + protected static class PostHolder { + final StandardPostModificationListener listener; + int mask; + + PostHolder(StandardPostModificationListener listener, int mask) { + this.listener = listener; + this.mask = mask; + } + + public String toString() { + return "[" + listener + "," + ModificationEventType.toString(mask) + "]"; + } + + } + // Pre event sending //----------------------------------------------------------------------- /** @@ -289,9 +438,9 @@ public class StandardModificationHandler extends ModificationHandler { if ((preMask & type) > 0) { StandardModificationEvent event = null; synchronized (this) { - for (int i = 0; i < holders.length; i++) { - Holder holder = holders[i]; - if ((holder.preMask & type) > 0) { + for (int i = 0; i < preHolder.length; i++) { + PreHolder holder = preHolder[i]; + if ((holder.mask & type) > 0) { if (event == null) { event = new StandardModificationEvent( getCollection(), this, type, preSize, index, object, repeat, null); @@ -350,9 +499,9 @@ public class StandardModificationHandler extends ModificationHandler { if ((postMask & type) > 0) { StandardModificationEvent event = null; synchronized (this) { - for (int i = 0; i < holders.length; i++) { - Holder holder = holders[i]; - if ((holder.postMask & type) > 0) { + for (int i = 0; i < postHolder.length; i++) { + PostHolder holder = postHolder[i]; + if ((holder.mask & type) > 0) { if (event == null) { event = new StandardModificationEvent( getCollection(), this, type, preSize, index, object, repeat, result); diff --git a/src/java/org/apache/commons/collections/event/StandardModificationListener.java b/src/java/org/apache/commons/collections/event/StandardModificationListener.java index d35989a70..9fe46dadb 100644 --- a/src/java/org/apache/commons/collections/event/StandardModificationListener.java +++ b/src/java/org/apache/commons/collections/event/StandardModificationListener.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/StandardModificationListener.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/StandardModificationListener.java,v 1.4 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -57,48 +57,16 @@ */ package org.apache.commons.collections.event; -import java.util.EventListener; - /** - * A listener that receives events from the StandardModificationHandler. - *

- * This listener has two methods. - *

    - *
  1. modificationOccurring - called before the modification - * occurs and can veto the change. - *
  2. modificationOccurred - called after the change and is - * for information. - *
+ * A listener for the StandardModificationHandler that is called + * both before the collection is changed and after the change has occurred. * * @since Commons Collections 3.0 - * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $ + * @version $Revision: 1.4 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ -public interface StandardModificationListener extends EventListener { +public interface StandardModificationListener + extends StandardPreModificationListener, StandardPostModificationListener { - /** - * A collection modification is occurring. - *

- * To veto the change, throw ModicationVetoedException. - *

- * This method should be processed quickly, as with all event handling. - * It should also avoid modifying the event source (the collection). - * - * @param event the event detail - * @throws ModicationVetoedException to veto - */ - public void modificationOccurring(StandardModificationEvent event); - - /** - * A collection modification occurred. - *

- * This method should be processed quickly, as with all event handling. - * It should also avoid modifying the event source (the collection). - * Finally it should avoid throwing an exception. - * - * @param event the event detail - */ - public void modificationOccurred(StandardModificationEvent event); - } diff --git a/src/java/org/apache/commons/collections/event/StandardPostModificationListener.java b/src/java/org/apache/commons/collections/event/StandardPostModificationListener.java new file mode 100644 index 000000000..6e0040766 --- /dev/null +++ b/src/java/org/apache/commons/collections/event/StandardPostModificationListener.java @@ -0,0 +1,82 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/StandardPostModificationListener.java,v 1.1 2003/08/31 22:44:54 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.event; + +/** + * A listener for the StandardModificationHandler that is called + * when a collection has been changed. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/08/31 22:44:54 $ + * + * @author Stephen Colebourne + */ +public interface StandardPostModificationListener extends ModificationListener { + + /** + * A collection modification occurred. + *

+ * This method should be processed quickly, as with all event handling. + * It should also avoid modifying the event source (the collection). + * Finally it should avoid throwing an exception. + * + * @param event the event detail + */ + public void modificationOccurred(StandardModificationEvent event); + +} diff --git a/src/java/org/apache/commons/collections/event/StandardModificationAdaptor.java b/src/java/org/apache/commons/collections/event/StandardPreModificationListener.java similarity index 80% rename from src/java/org/apache/commons/collections/event/StandardModificationAdaptor.java rename to src/java/org/apache/commons/collections/event/StandardPreModificationListener.java index 3adc9f1a7..e8dc93bfd 100644 --- a/src/java/org/apache/commons/collections/event/StandardModificationAdaptor.java +++ b/src/java/org/apache/commons/collections/event/StandardPreModificationListener.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/StandardModificationAdaptor.java,v 1.2 2003/08/31 17:25:49 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/event/Attic/StandardPreModificationListener.java,v 1.1 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -58,36 +58,27 @@ package org.apache.commons.collections.event; /** - * An adaptor for StandardModificationListener that provides no-op - * implementations of both methods. + * A listener for the StandardModificationHandler that is called + * when a collection is about to be modified. * * @since Commons Collections 3.0 - * @version $Revision: 1.2 $ $Date: 2003/08/31 17:25:49 $ + * @version $Revision: 1.1 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ -public class StandardModificationAdaptor implements StandardModificationListener { +public interface StandardPreModificationListener extends ModificationListener { /** - * A collection modification is occurring and may be vetoed. + * A collection modification is occurring. *

- * This implementation does nothing. + * To veto the change, throw ModicationVetoedException. + *

+ * This method should be processed quickly, as with all event handling. + * It should also avoid modifying the event source (the collection). * * @param event the event detail * @throws ModicationVetoedException to veto */ - public void modificationOccurring(StandardModificationEvent event) { - // do nothing - } + public void modificationOccurring(StandardModificationEvent event); - /** - * A collection modification occurred. - *

- * This implementation does nothing. - * - * @param event the event detail - */ - public void modificationOccurred(StandardModificationEvent event) { - // do nothing - } } diff --git a/src/test/org/apache/commons/collections/decorators/ObservedTestHelper.java b/src/test/org/apache/commons/collections/decorators/ObservedTestHelper.java index 4a058c077..6fe16cc1d 100644 --- a/src/test/org/apache/commons/collections/decorators/ObservedTestHelper.java +++ b/src/test/org/apache/commons/collections/decorators/ObservedTestHelper.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/ObservedTestHelper.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/ObservedTestHelper.java,v 1.4 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -67,13 +67,15 @@ import org.apache.commons.collections.event.ModificationEventType; import org.apache.commons.collections.event.StandardModificationEvent; import org.apache.commons.collections.event.StandardModificationHandler; import org.apache.commons.collections.event.StandardModificationListener; +import org.apache.commons.collections.event.StandardPostModificationListener; +import org.apache.commons.collections.event.StandardPreModificationListener; /** * Helper for testing * {@link ObservedCollection} implementations. * * @since Commons Collections 3.0 - * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $ + * @version $Revision: 1.4 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ @@ -101,8 +103,26 @@ public class ObservedTestHelper { } } + public static class PreListener implements StandardPreModificationListener { + public StandardModificationEvent preEvent = null; + + public void modificationOccurring(StandardModificationEvent event) { + this.preEvent = event; + } + } + + public static class PostListener implements StandardPostModificationListener { + public StandardModificationEvent postEvent = null; + + public void modificationOccurred(StandardModificationEvent event) { + this.postEvent = event; + } + } + public static final Listener LISTENER = new Listener(); public static final Listener LISTENER2 = new Listener(); + public static final PreListener PRE_LISTENER = new PreListener(); + public static final PostListener POST_LISTENER = new PostListener(); public ObservedTestHelper() { super(); @@ -111,52 +131,100 @@ public class ObservedTestHelper { //----------------------------------------------------------------------- public static void doTestFactoryPlain(ObservedCollection coll) { Assert.assertEquals(StandardModificationHandler.class, coll.getHandler().getClass()); - Assert.assertEquals(0, coll.getModificationListeners().length); + Assert.assertEquals(0, coll.getHandler().getPreModificationListeners().length); + Assert.assertEquals(0, coll.getHandler().getPostModificationListeners().length); + } + + public static void doTestFactoryWithPreListener(ObservedCollection coll) { + Assert.assertEquals(StandardModificationHandler.class, coll.getHandler().getClass()); + Assert.assertEquals(1, coll.getHandler().getPreModificationListeners().length); + Assert.assertEquals(0, coll.getHandler().getPostModificationListeners().length); + Assert.assertSame(PRE_LISTENER, coll.getHandler().getPreModificationListeners()[0]); + + PRE_LISTENER.preEvent = null; + coll.add(SIX); + Assert.assertTrue(PRE_LISTENER.preEvent != null); + } + + public static void doTestFactoryWithPostListener(ObservedCollection coll) { + Assert.assertEquals(StandardModificationHandler.class, coll.getHandler().getClass()); + Assert.assertEquals(0, coll.getHandler().getPreModificationListeners().length); + Assert.assertEquals(1, coll.getHandler().getPostModificationListeners().length); + Assert.assertSame(POST_LISTENER, coll.getHandler().getPostModificationListeners()[0]); + + POST_LISTENER.postEvent = null; + coll.add(SIX); + Assert.assertTrue(POST_LISTENER.postEvent != null); } public static void doTestFactoryWithListener(ObservedCollection coll) { Assert.assertEquals(StandardModificationHandler.class, coll.getHandler().getClass()); - Assert.assertEquals(1, coll.getModificationListeners().length); - Assert.assertSame(LISTENER, coll.getModificationListeners()[0]); - } - - public static void doTestFactoryPostEvents(ObservedCollection coll) { - Assert.assertEquals(StandardModificationHandler.class, coll.getHandler().getClass()); - Assert.assertEquals(1, coll.getModificationListeners().length); - Assert.assertSame(LISTENER, coll.getModificationListeners()[0]); + Assert.assertEquals(1, coll.getHandler().getPreModificationListeners().length); + Assert.assertEquals(1, coll.getHandler().getPostModificationListeners().length); + Assert.assertSame(LISTENER, coll.getHandler().getPreModificationListeners()[0]); + Assert.assertSame(LISTENER, coll.getHandler().getPostModificationListeners()[0]); LISTENER.preEvent = null; LISTENER.postEvent = null; coll.add(SIX); - Assert.assertTrue(LISTENER.preEvent == null); + Assert.assertTrue(LISTENER.preEvent != null); Assert.assertTrue(LISTENER.postEvent != null); } //----------------------------------------------------------------------- - public static void doTestAddRemoveGetListeners(ObservedCollection coll) { - Assert.assertEquals(0, coll.getModificationListeners().length); - coll.addModificationListener(LISTENER); - Assert.assertEquals(1, coll.getModificationListeners().length); - Assert.assertSame(LISTENER, coll.getModificationListeners()[0]); + public static void doTestAddRemoveGetPreListeners(ObservedCollection coll) { + Assert.assertEquals(0, coll.getHandler().getPreModificationListeners().length); + coll.getHandler().addPreModificationListener(LISTENER); + Assert.assertEquals(1, coll.getHandler().getPreModificationListeners().length); + Assert.assertSame(LISTENER, coll.getHandler().getPreModificationListeners()[0]); - coll.addModificationListener(LISTENER2); - Assert.assertEquals(2, coll.getModificationListeners().length); - Assert.assertSame(LISTENER, coll.getModificationListeners()[0]); - Assert.assertSame(LISTENER2, coll.getModificationListeners()[1]); + coll.getHandler().addPreModificationListener(LISTENER2); + Assert.assertEquals(2, coll.getHandler().getPreModificationListeners().length); + Assert.assertSame(LISTENER, coll.getHandler().getPreModificationListeners()[0]); + Assert.assertSame(LISTENER2, coll.getHandler().getPreModificationListeners()[1]); - coll.removeModificationListener(LISTENER); - Assert.assertEquals(1, coll.getModificationListeners().length); - Assert.assertSame(LISTENER2, coll.getModificationListeners()[0]); + coll.getHandler().removePreModificationListener(LISTENER); + Assert.assertEquals(1, coll.getHandler().getPreModificationListeners().length); + Assert.assertSame(LISTENER2, coll.getHandler().getPreModificationListeners()[0]); - coll.removeModificationListener(LISTENER); // check no error if not present - Assert.assertEquals(1, coll.getModificationListeners().length); - Assert.assertSame(LISTENER2, coll.getModificationListeners()[0]); + coll.getHandler().removePreModificationListener(LISTENER); // check no error if not present + Assert.assertEquals(1, coll.getHandler().getPreModificationListeners().length); + Assert.assertSame(LISTENER2, coll.getHandler().getPreModificationListeners()[0]); - coll.removeModificationListener(LISTENER2); - Assert.assertEquals(0, coll.getModificationListeners().length); + coll.getHandler().removePreModificationListener(LISTENER2); + Assert.assertEquals(0, coll.getHandler().getPreModificationListeners().length); try { - coll.addModificationListener(new Object()); + coll.getHandler().addPreModificationListener(new Object()); + Assert.fail(); + } catch (ClassCastException ex) { + } + } + + public static void doTestAddRemoveGetPostListeners(ObservedCollection coll) { + Assert.assertEquals(0, coll.getHandler().getPostModificationListeners().length); + coll.getHandler().addPostModificationListener(LISTENER); + Assert.assertEquals(1, coll.getHandler().getPostModificationListeners().length); + Assert.assertSame(LISTENER, coll.getHandler().getPostModificationListeners()[0]); + + coll.getHandler().addPostModificationListener(LISTENER2); + Assert.assertEquals(2, coll.getHandler().getPostModificationListeners().length); + Assert.assertSame(LISTENER, coll.getHandler().getPostModificationListeners()[0]); + Assert.assertSame(LISTENER2, coll.getHandler().getPostModificationListeners()[1]); + + coll.getHandler().removePostModificationListener(LISTENER); + Assert.assertEquals(1, coll.getHandler().getPostModificationListeners().length); + Assert.assertSame(LISTENER2, coll.getHandler().getPostModificationListeners()[0]); + + coll.getHandler().removePostModificationListener(LISTENER); // check no error if not present + Assert.assertEquals(1, coll.getHandler().getPostModificationListeners().length); + Assert.assertSame(LISTENER2, coll.getHandler().getPostModificationListeners()[0]); + + coll.getHandler().removePostModificationListener(LISTENER2); + Assert.assertEquals(0, coll.getHandler().getPostModificationListeners().length); + + try { + coll.getHandler().addPostModificationListener(new Object()); Assert.fail(); } catch (ClassCastException ex) { } diff --git a/src/test/org/apache/commons/collections/decorators/TestObservedCollection.java b/src/test/org/apache/commons/collections/decorators/TestObservedCollection.java index da621ce0e..20d9a7a5a 100644 --- a/src/test/org/apache/commons/collections/decorators/TestObservedCollection.java +++ b/src/test/org/apache/commons/collections/decorators/TestObservedCollection.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestObservedCollection.java,v 1.2 2003/08/31 17:28:42 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestObservedCollection.java,v 1.3 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -66,7 +66,6 @@ import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.collections.TestCollection; -import org.apache.commons.collections.event.ModificationEventType; import org.apache.commons.collections.event.StandardModificationHandler; /** @@ -74,7 +73,7 @@ import org.apache.commons.collections.event.StandardModificationHandler; * {@link ObservedCollection} implementation. * * @since Commons Collections 3.0 - * @version $Revision: 1.2 $ $Date: 2003/08/31 17:28:42 $ + * @version $Revision: 1.3 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ @@ -84,6 +83,8 @@ public class TestObservedCollection extends TestCollection { private static Integer SEVEN = new Integer(7); private static Integer EIGHT = new Integer(8); private static final ObservedTestHelper.Listener LISTENER = ObservedTestHelper.LISTENER; + private static final ObservedTestHelper.PreListener PRE_LISTENER = ObservedTestHelper.PRE_LISTENER; + private static final ObservedTestHelper.PostListener POST_LISTENER = ObservedTestHelper.POST_LISTENER; public TestObservedCollection(String testName) { super(testName); @@ -127,11 +128,17 @@ public class TestObservedCollection extends TestCollection { coll = ObservedCollection.decorate(new ArrayList(), LISTENER); ObservedTestHelper.doTestFactoryWithListener(coll); - coll = ObservedCollection.decoratePostEventsOnly(new ArrayList(), LISTENER); - ObservedTestHelper.doTestFactoryPostEvents(coll); + coll = ObservedCollection.decorate(new ArrayList(), PRE_LISTENER); + ObservedTestHelper.doTestFactoryWithPreListener(coll); + + coll = ObservedCollection.decorate(new ArrayList(), POST_LISTENER); + ObservedTestHelper.doTestFactoryWithPostListener(coll); coll = ObservedCollection.decorate(new ArrayList()); - ObservedTestHelper.doTestAddRemoveGetListeners(coll); + ObservedTestHelper.doTestAddRemoveGetPreListeners(coll); + + coll = ObservedCollection.decorate(new ArrayList()); + ObservedTestHelper.doTestAddRemoveGetPostListeners(coll); coll = ObservedCollection.decorate(new ArrayList(), LISTENER); ObservedTestHelper.doTestAdd(coll); @@ -161,44 +168,45 @@ public class TestObservedCollection extends TestCollection { ObservedCollection coll = ObservedCollection.decorate(new ArrayList(), handler); assertSame(handler, coll.getHandler()); - assertEquals(0, coll.getModificationListeners().length); - } - - public void testFactoryWithMasks() { - ObservedCollection coll = ObservedCollection.decorate(new ArrayList(), LISTENER, -1, 0); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent != null); - assertTrue(LISTENER.postEvent == null); - - coll = ObservedCollection.decorate(new ArrayList(), LISTENER, 0, -1); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent == null); - assertTrue(LISTENER.postEvent != null); - - coll = ObservedCollection.decorate(new ArrayList(), LISTENER, -1, -1); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent != null); - assertTrue(LISTENER.postEvent != null); - - coll = ObservedCollection.decorate(new ArrayList(), LISTENER, 0, 0); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent == null); - assertTrue(LISTENER.postEvent == null); - - coll = ObservedCollection.decorate(new ArrayList(), LISTENER, ModificationEventType.ADD, ModificationEventType.ADD_ALL); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent != null); - assertTrue(LISTENER.postEvent == null); + assertEquals(0, coll.getHandler().getPreModificationListeners().length); + assertEquals(0, coll.getHandler().getPostModificationListeners().length); } +// public void testFactoryWithMasks() { +// ObservedCollection coll = ObservedCollection.decorate(new ArrayList(), LISTENER, -1, 0); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent != null); +// assertTrue(LISTENER.postEvent == null); +// +// coll = ObservedCollection.decorate(new ArrayList(), LISTENER, 0, -1); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent == null); +// assertTrue(LISTENER.postEvent != null); +// +// coll = ObservedCollection.decorate(new ArrayList(), LISTENER, -1, -1); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent != null); +// assertTrue(LISTENER.postEvent != null); +// +// coll = ObservedCollection.decorate(new ArrayList(), LISTENER, 0, 0); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent == null); +// assertTrue(LISTENER.postEvent == null); +// +// coll = ObservedCollection.decorate(new ArrayList(), LISTENER, ModificationEventType.ADD, ModificationEventType.ADD_ALL); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent != null); +// assertTrue(LISTENER.postEvent == null); +// } +// } diff --git a/src/test/org/apache/commons/collections/decorators/TestObservedList.java b/src/test/org/apache/commons/collections/decorators/TestObservedList.java index 3574ac6e0..23d00b2a5 100644 --- a/src/test/org/apache/commons/collections/decorators/TestObservedList.java +++ b/src/test/org/apache/commons/collections/decorators/TestObservedList.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestObservedList.java,v 1.2 2003/08/31 17:28:42 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestObservedList.java,v 1.3 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -65,7 +65,6 @@ import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.collections.TestList; -import org.apache.commons.collections.event.ModificationEventType; import org.apache.commons.collections.event.StandardModificationHandler; /** @@ -73,7 +72,7 @@ import org.apache.commons.collections.event.StandardModificationHandler; * {@link ObservedList} implementation. * * @since Commons Collections 3.0 - * @version $Revision: 1.2 $ $Date: 2003/08/31 17:28:42 $ + * @version $Revision: 1.3 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ @@ -83,6 +82,8 @@ public class TestObservedList extends TestList { private static Integer SEVEN = new Integer(7); private static Integer EIGHT = new Integer(8); private static final ObservedTestHelper.Listener LISTENER = ObservedTestHelper.LISTENER; + private static final ObservedTestHelper.PreListener PRE_LISTENER = ObservedTestHelper.PRE_LISTENER; + private static final ObservedTestHelper.PostListener POST_LISTENER = ObservedTestHelper.POST_LISTENER; public TestObservedList(String testName) { super(testName); @@ -116,11 +117,17 @@ public class TestObservedList extends TestList { coll = ObservedList.decorate(new ArrayList(), LISTENER); ObservedTestHelper.doTestFactoryWithListener(coll); - coll = ObservedList.decoratePostEventsOnly(new ArrayList(), LISTENER); - ObservedTestHelper.doTestFactoryPostEvents(coll); + coll = ObservedList.decorate(new ArrayList(), PRE_LISTENER); + ObservedTestHelper.doTestFactoryWithPreListener(coll); + + coll = ObservedList.decorate(new ArrayList(), POST_LISTENER); + ObservedTestHelper.doTestFactoryWithPostListener(coll); coll = ObservedList.decorate(new ArrayList()); - ObservedTestHelper.doTestAddRemoveGetListeners(coll); + ObservedTestHelper.doTestAddRemoveGetPreListeners(coll); + + coll = ObservedList.decorate(new ArrayList()); + ObservedTestHelper.doTestAddRemoveGetPostListeners(coll); coll = ObservedList.decorate(new ArrayList(), LISTENER); ObservedTestHelper.doTestAdd(coll); @@ -162,44 +169,45 @@ public class TestObservedList extends TestList { ObservedList coll = ObservedList.decorate(new ArrayList(), handler); assertSame(handler, coll.getHandler()); - assertEquals(0, coll.getModificationListeners().length); - } - - public void testFactoryWithMasks() { - ObservedList coll = ObservedList.decorate(new ArrayList(), LISTENER, -1, 0); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent != null); - assertTrue(LISTENER.postEvent == null); - - coll = ObservedList.decorate(new ArrayList(), LISTENER, 0, -1); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent == null); - assertTrue(LISTENER.postEvent != null); - - coll = ObservedList.decorate(new ArrayList(), LISTENER, -1, -1); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent != null); - assertTrue(LISTENER.postEvent != null); - - coll = ObservedList.decorate(new ArrayList(), LISTENER, 0, 0); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent == null); - assertTrue(LISTENER.postEvent == null); - - coll = ObservedList.decorate(new ArrayList(), LISTENER, ModificationEventType.ADD, ModificationEventType.ADD_ALL); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent != null); - assertTrue(LISTENER.postEvent == null); + assertEquals(0, coll.getHandler().getPreModificationListeners().length); + assertEquals(0, coll.getHandler().getPostModificationListeners().length); } +// public void testFactoryWithMasks() { +// ObservedList coll = ObservedList.decorate(new ArrayList(), LISTENER, -1, 0); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent != null); +// assertTrue(LISTENER.postEvent == null); +// +// coll = ObservedList.decorate(new ArrayList(), LISTENER, 0, -1); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent == null); +// assertTrue(LISTENER.postEvent != null); +// +// coll = ObservedList.decorate(new ArrayList(), LISTENER, -1, -1); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent != null); +// assertTrue(LISTENER.postEvent != null); +// +// coll = ObservedList.decorate(new ArrayList(), LISTENER, 0, 0); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent == null); +// assertTrue(LISTENER.postEvent == null); +// +// coll = ObservedList.decorate(new ArrayList(), LISTENER, ModificationEventType.ADD, ModificationEventType.ADD_ALL); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent != null); +// assertTrue(LISTENER.postEvent == null); +// } +// } diff --git a/src/test/org/apache/commons/collections/decorators/TestObservedSet.java b/src/test/org/apache/commons/collections/decorators/TestObservedSet.java index 4c7695463..e1cf6d9ae 100644 --- a/src/test/org/apache/commons/collections/decorators/TestObservedSet.java +++ b/src/test/org/apache/commons/collections/decorators/TestObservedSet.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestObservedSet.java,v 1.2 2003/08/31 17:28:42 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestObservedSet.java,v 1.3 2003/08/31 22:44:54 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -65,7 +65,6 @@ import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.collections.TestSet; -import org.apache.commons.collections.event.ModificationEventType; import org.apache.commons.collections.event.StandardModificationHandler; /** @@ -73,7 +72,7 @@ import org.apache.commons.collections.event.StandardModificationHandler; * {@link ObservedSet} implementation. * * @since Commons Collections 3.0 - * @version $Revision: 1.2 $ $Date: 2003/08/31 17:28:42 $ + * @version $Revision: 1.3 $ $Date: 2003/08/31 22:44:54 $ * * @author Stephen Colebourne */ @@ -83,6 +82,8 @@ public class TestObservedSet extends TestSet { private static Integer SEVEN = new Integer(7); private static Integer EIGHT = new Integer(8); private static final ObservedTestHelper.Listener LISTENER = ObservedTestHelper.LISTENER; + private static final ObservedTestHelper.PreListener PRE_LISTENER = ObservedTestHelper.PRE_LISTENER; + private static final ObservedTestHelper.PostListener POST_LISTENER = ObservedTestHelper.POST_LISTENER; public TestObservedSet(String testName) { super(testName); @@ -116,11 +117,17 @@ public class TestObservedSet extends TestSet { coll = ObservedSet.decorate(new HashSet(), LISTENER); ObservedTestHelper.doTestFactoryWithListener(coll); - coll = ObservedSet.decoratePostEventsOnly(new HashSet(), LISTENER); - ObservedTestHelper.doTestFactoryPostEvents(coll); + coll = ObservedSet.decorate(new HashSet(), PRE_LISTENER); + ObservedTestHelper.doTestFactoryWithPreListener(coll); + + coll = ObservedSet.decorate(new HashSet(), POST_LISTENER); + ObservedTestHelper.doTestFactoryWithPostListener(coll); coll = ObservedSet.decorate(new HashSet()); - ObservedTestHelper.doTestAddRemoveGetListeners(coll); + ObservedTestHelper.doTestAddRemoveGetPreListeners(coll); + + coll = ObservedSet.decorate(new HashSet()); + ObservedTestHelper.doTestAddRemoveGetPostListeners(coll); coll = ObservedSet.decorate(new HashSet(), LISTENER); ObservedTestHelper.doTestAdd(coll); @@ -150,44 +157,45 @@ public class TestObservedSet extends TestSet { ObservedSet coll = ObservedSet.decorate(new HashSet(), handler); assertSame(handler, coll.getHandler()); - assertEquals(0, coll.getModificationListeners().length); - } - - public void testFactoryWithMasks() { - ObservedSet coll = ObservedSet.decorate(new HashSet(), LISTENER, -1, 0); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent != null); - assertTrue(LISTENER.postEvent == null); - - coll = ObservedSet.decorate(new HashSet(), LISTENER, 0, -1); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent == null); - assertTrue(LISTENER.postEvent != null); - - coll = ObservedSet.decorate(new HashSet(), LISTENER, -1, -1); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent != null); - assertTrue(LISTENER.postEvent != null); - - coll = ObservedSet.decorate(new HashSet(), LISTENER, 0, 0); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent == null); - assertTrue(LISTENER.postEvent == null); - - coll = ObservedSet.decorate(new HashSet(), LISTENER, ModificationEventType.ADD, ModificationEventType.ADD_ALL); - LISTENER.preEvent = null; - LISTENER.postEvent = null; - coll.add(SIX); - assertTrue(LISTENER.preEvent != null); - assertTrue(LISTENER.postEvent == null); + assertEquals(0, coll.getHandler().getPreModificationListeners().length); + assertEquals(0, coll.getHandler().getPostModificationListeners().length); } +// public void testFactoryWithMasks() { +// ObservedSet coll = ObservedSet.decorate(new HashSet(), LISTENER, -1, 0); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent != null); +// assertTrue(LISTENER.postEvent == null); +// +// coll = ObservedSet.decorate(new HashSet(), LISTENER, 0, -1); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent == null); +// assertTrue(LISTENER.postEvent != null); +// +// coll = ObservedSet.decorate(new HashSet(), LISTENER, -1, -1); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent != null); +// assertTrue(LISTENER.postEvent != null); +// +// coll = ObservedSet.decorate(new HashSet(), LISTENER, 0, 0); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent == null); +// assertTrue(LISTENER.postEvent == null); +// +// coll = ObservedSet.decorate(new HashSet(), LISTENER, ModificationEventType.ADD, ModificationEventType.ADD_ALL); +// LISTENER.preEvent = null; +// LISTENER.postEvent = null; +// coll.add(SIX); +// assertTrue(LISTENER.preEvent != null); +// assertTrue(LISTENER.postEvent == null); +// } +// }