diff --git a/src/java/org/apache/commons/collections/decorators/SetList.java b/src/java/org/apache/commons/collections/decorators/SetList.java new file mode 100755 index 000000000..6c8d6fab9 --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/SetList.java @@ -0,0 +1,229 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/SetList.java,v 1.1 2003/10/02 22:34:44 matth Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-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.decorators; + +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/** + * SetList combines the List and Set interfaces + * in one implementation. + *

+ * The List interface makes certain assumptions/requirements. + * This implementation breaks these in certain ways, but this is merely the + * result of rejecting duplicates. + * Each violation is explained in the method, but it should not affect you. + *

+ * The {@link org.apache.commons.collections.decorators.OrderedSet OrderedSet} + * class provides an alternative approach, by wrapping an existing Set and + * retaining insertion order in the iterator. This class offers the List + * interface implementation as well. + *

+ * If the Set aspects are important to you (fast add/contains/remove) + * then OrderedSet is a better choice. + * This implementation is based solely on ArrayList, and so has + * slow add/contains/remove operations for all except small lists. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/10/02 22:34:44 $ + * + * @author Matthew Hawthorne + * @author Stephen Colebourne + */ +public class SetList extends AbstractList { + + /** + * Delegate list. + */ + private final List delegate = new ArrayList(); + + /** + * Helps to maintain uniqueness. + */ + private final Set set = new HashSet(); + + /** + * Factory method to create a SetList. + * @param list the list to decorate + */ + public static SetList decorate(List list) { + return new SetList(list); + } + + /** + * Contructs an new list copying the specified elements. + * @param coll a collection to copy + */ + protected SetList(List list) { + addAll(list); + } + + //----------------------------------------------------------------------- + /** + * Adds an element to the list if it is not already present. + *

+ * (Violation) + * The List interface requires that this method returns + * true always. However this class may return false + * because of the Set behaviour. + * + * @param object the object to add + * @return true if object was added + */ + public boolean add(Object object) { + // gets initial size + final int sizeBefore = size(); + + // adds element if unique + add(size(), object); + + // compares sizes to detect if collection changed + return (sizeBefore != size()); + } + + /** + * Adds an element to a specific index in the list if it is not already present. + *

+ * (Violation) + * The List interface makes the assumption that the element is + * always inserted. This may not happen with this implementation. + * + * @param index the index to insert at + * @param object the object to add + */ + public void add(int index, Object object) { + // Adds element if it is not contained already + if (!set.contains(object)) { + delegate.add(index, object); + set.add(object); + } + } + + //----------------------------------------------------------------------- + /** + * Adds a collection of objects to the end of the list avoiding duplicates. + *

+ * Only elements that are not already in this list will be added, and + * duplicates from the specified collection will be ignored. + *

+ * (Violation) + * The List interface makes the assumption that the elements + * are always inserted. This may not happen with this implementation. + * + * @param index the index to insert at + * @param coll the collection to add in iterator order + * @return true if this collection changed + */ + public boolean addAll(int index, Collection coll) { + // gets initial size + final int sizeBefore = size(); + + // adds all elements + for (final Iterator it = coll.iterator(); it.hasNext();) { + add(it.next()); + } + + // compares sizes to detect if collection changed + return sizeBefore != size(); + } + + //----------------------------------------------------------------------- + /** + * Sets the value at the specified index avoiding duplicates. + *

+ * The object is set into the specified index. + * Afterwards, any previous duplicate is removed + * If the object is not already in the list then a normal set occurs. + * If it is present, then the old version is removed and re-added at this index + * + * @param index the index to insert at + * @param object the object to set + * @return the previous object + */ + public Object set(int index, Object object) { + int pos = indexOf(object); + Object result = delegate.set(index, object); + if (pos == -1 || pos == index) { + return result; + } + return remove(pos); + } + + public Object get(int index) { + return delegate.get(index); + } + + public int size() { + return delegate.size(); + } + + public Object remove(int index) { + return delegate.remove(index); + } + + public void clear() { + delegate.clear(); + set.clear(); + } + +} diff --git a/src/test/org/apache/commons/collections/decorators/TestAll.java b/src/test/org/apache/commons/collections/decorators/TestAll.java index 017be77ce..3ab6ba6cc 100644 --- a/src/test/org/apache/commons/collections/decorators/TestAll.java +++ b/src/test/org/apache/commons/collections/decorators/TestAll.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/TestAll.java,v 1.15 2003/09/20 16:57:20 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestAll.java,v 1.16 2003/10/02 22:34:44 matth Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -65,7 +65,7 @@ import junit.framework.TestSuite; * Entry point for all collections decorators tests. * * @since Commons Collections 3.0 - * @version $Revision: 1.15 $ $Date: 2003/09/20 16:57:20 $ + * @version $Revision: 1.16 $ $Date: 2003/10/02 22:34:44 $ * * @author Stephen Colebourne */ @@ -93,6 +93,7 @@ public class TestAll extends TestCase { suite.addTest(TestLazySortedMap.suite()); suite.addTest(TestOrderedSet.suite()); + suite.addTest(TestSetList.suite()); suite.addTest(TestTransformedBag.suite()); suite.addTest(TestTransformedBuffer.suite()); diff --git a/src/test/org/apache/commons/collections/decorators/TestSetList.java b/src/test/org/apache/commons/collections/decorators/TestSetList.java new file mode 100755 index 000000000..4e071ecfd --- /dev/null +++ b/src/test/org/apache/commons/collections/decorators/TestSetList.java @@ -0,0 +1,182 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestSetList.java,v 1.1 2003/10/02 22:34:44 matth Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-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.decorators; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.ListIterator; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; + +/** + * JUnit tests. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/10/02 22:34:44 $ + * + * @author Matthew Hawthorne + */ +public class TestSetList extends TestCase { + + public static void main(String[] args) { + TestRunner.run(suite()); + } + + public static Test suite() { + return new TestSuite(TestSetList.class); + } + + public TestSetList(String testName) { + super(testName); + } + + //----------------------------------------------------------------------- + public void testConstructor() { + final SetList lset = + new SetList( + Arrays.asList(new Integer[] { new Integer(1), new Integer(1)})); + + assertEquals("Duplicate element was added.", 1, lset.size()); + } + + public void testAdd() { + final SetList lset = new SetList(new ArrayList()); + + // Duplicate element + final Object obj = new Integer(1); + lset.add(obj); + lset.add(obj); + assertEquals("Duplicate element was added.", 1, lset.size()); + + // Unique element + lset.add(new Integer(2)); + assertEquals("Unique element was not added.", 2, lset.size()); + } + + public void testAddAll() { + final SetList lset = new SetList(new ArrayList()); + + lset.addAll( + Arrays.asList(new Integer[] { new Integer(1), new Integer(1)})); + + assertEquals("Duplicate element was added.", 1, lset.size()); + } + + public void testSet() { + final SetList lset = new SetList(new ArrayList()); + + // Duplicate element + final Object obj1 = new Integer(1); + final Object obj2 = new Integer(2); + final Object obj3 = new Integer(3); + + lset.add(obj1); + lset.add(obj2); + lset.set(0, obj1); + assertEquals(2, lset.size()); + assertSame(obj1, lset.get(0)); + assertSame(obj2, lset.get(1)); + + lset.clear(); + lset.add(obj1); + lset.add(obj2); + lset.set(0, obj2); + assertEquals(1, lset.size()); + assertSame(obj2, lset.get(0)); + + lset.clear(); + lset.add(obj1); + lset.add(obj2); + lset.set(0, obj3); + assertEquals(2, lset.size()); + assertSame(obj3, lset.get(0)); + assertSame(obj2, lset.get(1)); + + lset.clear(); + lset.add(obj1); + lset.add(obj2); + lset.set(1, obj1); + assertEquals(1, lset.size()); + assertSame(obj1, lset.get(0)); + } + + public void testListIterator() { + final SetList lset = new SetList(new ArrayList()); + + final Object obj1 = new Integer(1); + final Object obj2 = new Integer(2); + lset.add(obj1); + lset.add(obj2); + + // Attempts to add a duplicate object + for (final ListIterator it = lset.listIterator(); it.hasNext();) { + it.next(); + + if (!it.hasNext()) { + it.add(obj1); + break; + } + } + + assertEquals("Duplicate element was added", 2, lset.size()); + } + +}