Refactor events so listener is defined as an Object for flexibility

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131126 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-08-31 21:09:49 +00:00
parent 43b14025a5
commit fc34848eb3
6 changed files with 37 additions and 105 deletions

View File

@ -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.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/ObservedCollection.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -61,7 +61,6 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import org.apache.commons.collections.event.ModificationHandler; import org.apache.commons.collections.event.ModificationHandler;
import org.apache.commons.collections.event.ModificationListener;
import org.apache.commons.collections.event.StandardModificationHandler; import org.apache.commons.collections.event.StandardModificationHandler;
import org.apache.commons.collections.event.StandardModificationListener; import org.apache.commons.collections.event.StandardModificationListener;
@ -82,7 +81,7 @@ import org.apache.commons.collections.event.StandardModificationListener;
* uses a technique other than listeners to communicate events. * uses a technique other than listeners to communicate events.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.2 $ $Date: 2003/08/31 17:24:46 $ * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -244,7 +243,7 @@ public class ObservedCollection extends AbstractCollectionDecorator {
* @return the listeners * @return the listeners
* @throws UnsupportedOperationException if the handler does not support listeners * @throws UnsupportedOperationException if the handler does not support listeners
*/ */
public ModificationListener[] getModificationListeners() { public Object[] getModificationListeners() {
return getHandler().getModificationListeners(); return getHandler().getModificationListeners();
} }
@ -253,12 +252,17 @@ public class ObservedCollection extends AbstractCollectionDecorator {
* This method simply delegates to the handler. * This method simply delegates to the handler.
* <p> * <p>
* No error occurs if the listener is <code>null</code>. * No error occurs if the listener is <code>null</code>.
* <p>
* 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 add, 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 ClassCastException if the listener is not of the correct type
* @throws UnsupportedOperationException if the handler does not support listeners * @throws UnsupportedOperationException if the handler does not support listeners
*/ */
public void addModificationListener(ModificationListener listener) { public void addModificationListener(Object listener) {
getHandler().addModificationListener(listener); getHandler().addModificationListener(listener);
} }
@ -274,7 +278,7 @@ public class ObservedCollection extends AbstractCollectionDecorator {
* @param listener the listener to remove, may be null (ignored) * @param listener the listener to remove, may be null (ignored)
* @throws UnsupportedOperationException if the handler does not support listeners * @throws UnsupportedOperationException if the handler does not support listeners
*/ */
public void removeModificationListener(ModificationListener listener) { public void removeModificationListener(Object listener) {
getHandler().removeModificationListener(listener); getHandler().removeModificationListener(listener);
} }

View File

@ -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.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/ModificationHandler.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -69,7 +69,7 @@ import org.apache.commons.collections.decorators.ObservedCollection;
* that forwards to single points. * that forwards to single points.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.2 $ $Date: 2003/08/31 17:25:49 $ * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -129,7 +129,7 @@ public abstract class ModificationHandler {
* @return the listeners * @return the listeners
* @throws UnsupportedOperationException if the handler does not support listeners * @throws UnsupportedOperationException if the handler does not support listeners
*/ */
public ModificationListener[] getModificationListeners() { public Object[] getModificationListeners() {
throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName()); throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName());
} }
@ -138,13 +138,18 @@ public abstract class ModificationHandler {
* <p> * <p>
* No error occurs if the listener is <code>null</code>. * No error occurs if the listener is <code>null</code>.
* <p> * <p>
* 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.
* <p>
* This implementation throws UnsupportedOperationException. * This implementation throws UnsupportedOperationException.
* *
* @param listener the listener to add, 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 ClassCastException if the listener is not of the correct type
* @throws UnsupportedOperationException if the handler does not support listeners * @throws UnsupportedOperationException if the handler does not support listeners
*/ */
public void addModificationListener(ModificationListener listener) { public void addModificationListener(Object listener) {
throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName()); throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName());
} }
@ -159,7 +164,7 @@ public abstract class ModificationHandler {
* @param listener the listener to remove, may be null (ignored) * @param listener the listener to remove, may be null (ignored)
* @throws UnsupportedOperationException if the handler does not support listeners * @throws UnsupportedOperationException if the handler does not support listeners
*/ */
public void removeModificationListener(ModificationListener listener) { public void removeModificationListener(Object listener) {
throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName()); throw new UnsupportedOperationException("Listeners not supported by " + getClass().getName());
} }

View File

@ -1,77 +0,0 @@
/*
* $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.2 2003/08/31 17:25:49 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.event;
import java.util.EventListener;
/**
* An empty listener designed to be subclassed.
* <p>
* 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.2 $ $Date: 2003/08/31 17:25:49 $
*
* @author Stephen Colebourne
*/
public interface ModificationListener extends EventListener {
// no methods - subinterfaces define them
}

View File

@ -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.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/StandardModificationHandler.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -80,7 +80,7 @@ package org.apache.commons.collections.event;
* </ul> * </ul>
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.2 $ $Date: 2003/08/31 17:25:49 $ * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -123,8 +123,8 @@ public class StandardModificationHandler extends ModificationHandler {
* *
* @return the listeners * @return the listeners
*/ */
public synchronized ModificationListener[] getModificationListeners() { public synchronized Object[] getModificationListeners() {
ModificationListener[] lnrs = new ModificationListener[holders.length]; Object[] lnrs = new Object[holders.length];
for (int i = 0; i < holders.length; i++) { for (int i = 0; i < holders.length; i++) {
lnrs[i] = holders[i].listener; lnrs[i] = holders[i].listener;
} }
@ -139,8 +139,8 @@ public class StandardModificationHandler extends ModificationHandler {
* @param listener the listener to add, may be null (ignored) * @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 StandardModificationListener
*/ */
public void addModificationListener(ModificationListener listener) { public void addModificationListener(Object listener) {
addModificationListener(listener, -1, -1); addModificationListener((StandardModificationListener) listener, -1, -1);
} }
/** /**
@ -151,14 +151,13 @@ public class StandardModificationHandler extends ModificationHandler {
* @param listener the listener to add, may be null (ignored) * @param listener the listener to add, may be null (ignored)
* @param preMask the mask for pre events (0 for none, -1 for all) * @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 postMask the mask for post events (0 for none, -1 for all)
* @throws ClassCastException if the listener is not a StandardModificationListener
*/ */
public synchronized void addModificationListener(ModificationListener listener, int preMask, int postMask) { public synchronized void addModificationListener(StandardModificationListener listener, int preMask, int postMask) {
if (listener != null) { if (listener != null) {
int oldSize = holders.length; int oldSize = holders.length;
Holder[] array = new Holder[oldSize + 1]; Holder[] array = new Holder[oldSize + 1];
System.arraycopy(holders, 0, array, 0, oldSize); System.arraycopy(holders, 0, array, 0, oldSize);
array[oldSize] = new Holder((StandardModificationListener) listener, preMask, postMask); array[oldSize] = new Holder(listener, preMask, postMask);
holders = array; holders = array;
calculateMasks(); calculateMasks();
} }
@ -173,7 +172,7 @@ public class StandardModificationHandler extends ModificationHandler {
* *
* @param listener the listener to remove, may be null (ignored) * @param listener the listener to remove, may be null (ignored)
*/ */
public synchronized void removeModificationListener(ModificationListener listener) { public synchronized void removeModificationListener(Object listener) {
if (listener != null) { if (listener != null) {
switch (holders.length) { switch (holders.length) {
case 0: case 0:

View File

@ -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.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/StandardModificationListener.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -57,6 +57,8 @@
*/ */
package org.apache.commons.collections.event; package org.apache.commons.collections.event;
import java.util.EventListener;
/** /**
* A listener that receives events from the <code>StandardModificationHandler</code>. * A listener that receives events from the <code>StandardModificationHandler</code>.
* <p> * <p>
@ -69,11 +71,11 @@ package org.apache.commons.collections.event;
* </ol> * </ol>
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.2 $ $Date: 2003/08/31 17:25:49 $ * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
public interface StandardModificationListener extends ModificationListener { public interface StandardModificationListener extends EventListener {
/** /**
* A collection modification is occurring. * A collection modification is occurring.

View File

@ -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.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/ObservedTestHelper.java,v 1.3 2003/08/31 21:09:49 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -64,7 +64,6 @@ import java.util.List;
import junit.framework.Assert; import junit.framework.Assert;
import org.apache.commons.collections.event.ModificationEventType; import org.apache.commons.collections.event.ModificationEventType;
import org.apache.commons.collections.event.ModificationListener;
import org.apache.commons.collections.event.StandardModificationEvent; import org.apache.commons.collections.event.StandardModificationEvent;
import org.apache.commons.collections.event.StandardModificationHandler; import org.apache.commons.collections.event.StandardModificationHandler;
import org.apache.commons.collections.event.StandardModificationListener; import org.apache.commons.collections.event.StandardModificationListener;
@ -74,7 +73,7 @@ import org.apache.commons.collections.event.StandardModificationListener;
* {@link ObservedCollection} implementations. * {@link ObservedCollection} implementations.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.2 $ $Date: 2003/08/31 17:28:42 $ * @version $Revision: 1.3 $ $Date: 2003/08/31 21:09:49 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -157,7 +156,7 @@ public class ObservedTestHelper {
Assert.assertEquals(0, coll.getModificationListeners().length); Assert.assertEquals(0, coll.getModificationListeners().length);
try { try {
coll.addModificationListener(new ModificationListener() {}); coll.addModificationListener(new Object());
Assert.fail(); Assert.fail();
} catch (ClassCastException ex) { } catch (ClassCastException ex) {
} }