Add ObservableSortedBag

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131200 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-09-28 21:50:37 +00:00
parent 8b6040783b
commit 52f05aba78
4 changed files with 303 additions and 14 deletions

View File

@ -0,0 +1,179 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/observed/Attic/ObservableSortedBag.java,v 1.1 2003/09/28 21:50:37 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.observed;
import java.util.Comparator;
import org.apache.commons.collections.SortedBag;
/**
* Decorates a <code>SortedBag</code> implementation to observe modifications.
* <p>
* Each modifying method call made on this <code>SortedBag</code> is forwarded to a
* {@link ModificationHandler}.
* The handler manages the event, notifying listeners and optionally vetoing changes.
* The default handler is {@link StandardModificationHandler}.
* See this class for details of configuration available.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/28 21:50:37 $
*
* @author Stephen Colebourne
*/
public class ObservableSortedBag extends ObservableBag implements SortedBag {
// Factories
//-----------------------------------------------------------------------
/**
* Factory method to create an observable bag.
* <p>
* A {@link StandardModificationHandler} will be created.
* This can be accessed by {@link #getHandler()} to add listeners.
*
* @param bag the bag to decorate, must not be null
* @return the observed bag
* @throws IllegalArgumentException if the collection is null
*/
public static ObservableSortedBag decorate(final SortedBag bag) {
return new ObservableSortedBag(bag, null);
}
/**
* Factory method to create an observable bag using a listener or a handler.
* <p>
* A lot of functionality is available through this method.
* If you don't need the extra functionality, simply implement the
* {@link org.apache.commons.collections.observed.standard.StandardModificationListener}
* interface and pass it in as the second parameter.
* <p>
* Internally, an <code>ObservableSortedBag</code> relies on a {@link ModificationHandler}.
* The handler receives all the events and processes them, typically by
* calling listeners. Different handler implementations can be plugged in
* to provide a flexible event system.
* <p>
* The handler implementation is determined by the listener parameter via
* the registered factories. The listener may be a manually configured
* <code>ModificationHandler</code> instance.
* <p>
* The listener is defined as an Object for maximum flexibility.
* It does not have to be a listener in the classic JavaBean sense.
* It is entirely up to the factory and handler as to how the parameter
* is interpretted. An IllegalArgumentException is thrown if no suitable
* handler can be found for this listener.
* <p>
* A <code>null</code> listener will create a {@link StandardModificationHandler}.
*
* @param bag the bag to decorate, must not be null
* @param listener bag listener, may be null
* @return the observed bag
* @throws IllegalArgumentException if the bag is null
* @throws IllegalArgumentException if there is no valid handler for the listener
*/
public static ObservableSortedBag decorate(
final SortedBag bag,
final Object listener) {
if (bag == null) {
throw new IllegalArgumentException("SortedBag must not be null");
}
return new ObservableSortedBag(bag, listener);
}
// Constructors
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies) and takes a handler.
* <p>
* The handler implementation is determined by the listener parameter via
* the registered factories. The listener may be a manually configured
* <code>ModificationHandler</code> instance.
*
* @param bag the bag to decorate, must not be null
* @param listener the listener, may be null
* @throws IllegalArgumentException if the bag is null
*/
protected ObservableSortedBag(
final SortedBag bag,
final Object listener) {
super(bag, listener);
}
/**
* Typecast the collection to a SortedBag.
*
* @return the wrapped collection as a SortedBag
*/
private SortedBag getSortedBag() {
return (SortedBag) getCollection();
}
// SortedBag API
//-----------------------------------------------------------------------
public Comparator comparator() {
return getSortedBag().comparator();
}
public Object first() {
return getSortedBag().first();
}
public Object last() {
return getSortedBag().last();
}
}

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/observed/Attic/ObservedTestHelper.java,v 1.11 2003/09/27 12:06:36 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/observed/Attic/ObservedTestHelper.java,v 1.12 2003/09/28 21:50:37 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -79,7 +79,7 @@ import org.apache.commons.collections.observed.standard.StandardPreModificationL
* {@link ObservedCollection} implementations.
*
* @since Commons Collections 3.0
* @version $Revision: 1.11 $ $Date: 2003/09/27 12:06:36 $
* @version $Revision: 1.12 $ $Date: 2003/09/28 21:50:37 $
*
* @author Stephen Colebourne
*/
@ -208,16 +208,15 @@ public class ObservedTestHelper extends Assert {
doTestRemoveNCopies(factory);
}
// public static void bulkTestObservedSortedBag(ObservedFactory factory) {
// assertTrue(factory.createObservedCollection() instanceof ObservableSortedBag);
// assertTrue(factory.createObservedCollection(LISTENER) instanceof ObservableSortedBag);
// assertTrue(factory.createObservedCollection(new StandardModificationHandler()) instanceof ObservableSortedBag);
//
// bulkTestObservedCollection(factory);
// doTestAddNCopies(factory);
// doTestRemoveNCopies(factory);
// // TODO
// }
public static void bulkTestObservedSortedBag(ObservedFactory factory) {
assertTrue(factory.createObservedCollection() instanceof ObservableSortedBag);
assertTrue(factory.createObservedCollection(LISTENER) instanceof ObservableSortedBag);
assertTrue(factory.createObservedCollection(new StandardModificationHandler()) instanceof ObservableSortedBag);
bulkTestObservedCollection(factory);
doTestAddNCopies(factory);
doTestRemoveNCopies(factory);
}
public static void bulkTestObservedBuffer(ObservedFactory factory) {
assertTrue(factory.createObservedCollection() instanceof ObservableBuffer);

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/observed/Attic/TestAll.java,v 1.3 2003/09/21 20:01:53 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/observed/Attic/TestAll.java,v 1.4 2003/09/28 21:50:37 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
* Entry point for all collections observed tests.
*
* @since Commons Collections 3.0
* @version $Revision: 1.3 $ $Date: 2003/09/21 20:01:53 $
* @version $Revision: 1.4 $ $Date: 2003/09/28 21:50:37 $
*
* @author Stephen Colebourne
*/
@ -88,6 +88,7 @@ public class TestAll extends TestCase {
suite.addTest(TestObservableCollection.suite());
suite.addTest(TestObservableList.suite());
suite.addTest(TestObservableSet.suite());
suite.addTest(TestObservableSortedBag.suite());
suite.addTest(TestObservableSortedSet.suite());
return suite;

View File

@ -0,0 +1,110 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/observed/Attic/TestObservableSortedBag.java,v 1.1 2003/09/28 21:50:37 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.observed;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.TestSortedBag;
import org.apache.commons.collections.TreeBag;
/**
* Extension of {@link TestSortedBag} for exercising the
* {@link ObservedSortedBag} implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/28 21:50:37 $
*
* @author Stephen Colebourne
*/
public class TestObservableSortedBag extends TestSortedBag implements ObservedTestHelper.ObservedFactory {
public TestObservableSortedBag(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestObservableSortedBag.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestObservableSortedBag.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//-----------------------------------------------------------------------
public Bag makeBag() {
return ObservableSortedBag.decorate(new TreeBag(), ObservedTestHelper.LISTENER);
}
//-----------------------------------------------------------------------
public void testObservedSortedBag() {
ObservedTestHelper.bulkTestObservedSortedBag(this);
}
//-----------------------------------------------------------------------
public ObservableCollection createObservedCollection() {
return ObservableSortedBag.decorate(new TreeBag());
}
public ObservableCollection createObservedCollection(Object listener) {
return ObservableSortedBag.decorate(new TreeBag(), listener);
}
}