You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/09/10 00:28:36 UTC

cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/decorators TestOrderedSet.java TestAll.java TestSequencedSet.java

scolebourne    2003/09/09 15:28:36

  Modified:    collections/src/java/org/apache/commons/collections
                        SequencedHashMap.java SetUtils.java
               collections/src/test/org/apache/commons/collections/decorators
                        TestAll.java
  Added:       collections/src/java/org/apache/commons/collections/decorators
                        OrderedSet.java
               collections/src/test/org/apache/commons/collections/decorators
                        TestOrderedSet.java
  Removed:     collections/src/java/org/apache/commons/collections/decorators
                        SequencedSet.java
               collections/src/test/org/apache/commons/collections/decorators
                        TestSequencedSet.java
  Log:
  Rename SequencedSet to OrderedSet
  
  Revision  Changes    Path
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/OrderedSet.java
  
  Index: OrderedSet.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/OrderedSet.java,v 1.1 2003/09/09 22:28:35 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.decorators;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Set;
  
  /**
   * Decorates a <code>Set</code> to ensure that the order of addition
   * is retained and used by the iterator.
   * <p>
   * If an object is added to the Set for a second time, it will remain in the
   * original position in the iteration.
   * <p>
   * The order can be observed via the iterator or toArray methods.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/09/09 22:28:35 $
   * 
   * @author Stephen Colebourne
   * @author Henning P. Schmiedehausen
   */
  public class OrderedSet extends AbstractSetDecorator implements Set {
  
      /** Internal list to hold the sequence of objects */
      protected final List setOrder = new ArrayList();
  
      /**
       * Factory method to create an ordered set.
       * 
       * @param set  the set to decorate, must not be null
       * @throws IllegalArgumentException if set is null
       */
      public static Set decorate(Set set) {
          return new OrderedSet(set);
      }
  
      /**
       * Constructor that wraps (not copies).
       * 
       * @param set  the set to decorate, must not be null
       * @throws IllegalArgumentException if set is null
       */
      protected OrderedSet(Set set) {
          super(set);
          setOrder.addAll(set);
      }
  
      //-----------------------------------------------------------------------
      public void clear() {
          collection.clear();
          setOrder.clear();
      }
  
      public Iterator iterator() {
          return new OrderedSetIterator(setOrder.iterator(), collection);
      }
  
      public boolean add(Object object) {
          if (collection.contains(object)) {
              // re-adding doesn't change order
              return collection.add(object);
          } else {
              // first add, so add to both set and list
              boolean result = collection.add(object);
              setOrder.add(object);
              return result;
          }
      }
  
      public boolean addAll(Collection coll) {
          boolean result = false;
          for (Iterator it = coll.iterator(); it.hasNext();) {
              Object object = it.next();
              result = result | add(object);
          }
          return result;
      }
  
      public boolean remove(Object object) {
          boolean result = collection.remove(object);
          setOrder.remove(object);
          return result;
      }
  
      public boolean removeAll(Collection coll) {
          boolean result = false;
          for (Iterator it = coll.iterator(); it.hasNext();) {
              Object object = it.next();
              result = result | remove(object);
          }
          return result;
      }
  
      public boolean retainAll(Collection coll) {
          boolean result = collection.retainAll(coll);
          if (result == false) {
              return false;
          } else if (collection.size() == 0) {
              setOrder.clear();
          } else {
              for (Iterator it = setOrder.iterator(); it.hasNext();) {
                  Object object = (Object) it.next();
                  if (collection.contains(object) == false) {
                      it.remove();
                  }
              }
          }
          return result;
      }
  
      public Object[] toArray() {
          return setOrder.toArray();
      }
  
      public Object[] toArray(Object a[]) {
          return setOrder.toArray(a);
      }
  
      /**
       * Internal iterator handle remove.
       */
      protected static class OrderedSetIterator extends AbstractIteratorDecorator {
          
          /** Object we iterate on */
          protected final Collection set;
          /** Last object retrieved */
          protected Object last;
  
          private OrderedSetIterator(Iterator iterator, Collection set) {
              super(iterator);
              this.set = set;
          }
  
          public Object next() {
              last = iterator.next();
              return last;
          }
  
          public void remove() {
              set.remove(last);
              iterator.remove();
              last = null;
          }
      }
  
  }
  
  
  
  1.20      +48 -47    jakarta-commons/collections/src/java/org/apache/commons/collections/SequencedHashMap.java
  
  Index: SequencedHashMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/SequencedHashMap.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- SequencedHashMap.java	31 Aug 2003 17:26:44 -0000	1.19
  +++ SequencedHashMap.java	9 Sep 2003 22:28:35 -0000	1.20
  @@ -75,33 +75,34 @@
   import java.util.Set;
   
   /**
  - *  A map of objects whose mapping entries are sequenced based on the order in
  - *  which they were added.  This data structure has fast <i>O(1)</i> search
  - *  time, deletion time, and insertion time.
  - *
  - *  <p>Although this map is sequenced, it cannot implement {@link
  - *  java.util.List} because of incompatible interface definitions.  The remove
  - *  methods in List and Map have different return values (see: {@link
  - *  java.util.List#remove(Object)} and {@link java.util.Map#remove(Object)}).
  - *
  - *  <p>This class is not thread safe.  When a thread safe implementation is
  - *  required, use {@link Collections#synchronizedMap(Map)} as it is documented,
  - *  or use explicit synchronization controls.
  + * A map of objects whose mapping entries are sequenced based on the order in
  + * which they were added.  This data structure has fast <i>O(1)</i> search
  + * time, deletion time, and insertion time.
  + * <p>
  + * Although this map is sequenced, it cannot implement
  + * {@link java.util.List} because of incompatible interface definitions.
  + * The remove methods in List and Map have different return values 
  + * (see: {@link java.util.List#remove(Object)} and {@link java.util.Map#remove(Object)}).
  + * <p>
  + * This class is not thread safe.  When a thread safe implementation is
  + * required, use {@link Collections#synchronizedMap(Map)} as it is documented,
  + * or use explicit synchronization controls.
    *
  + * @see org.apache.commons.collections.decorators.OrderedSet
    * @since Commons Collections 2.0
    * @version $Revision$ $Date$
    * 
  - * @author <a href="mailto:mas@apache.org">Michael A. Smith</A>
  - * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
  - * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
  + * @author Michael A. Smith
  + * @author Daniel Rall
  + * @author Henning P. Schmiedehausen
    * @author Stephen Colebourne
    */
   public class SequencedHashMap implements Map, Cloneable, Externalizable {
   
       /**
  -     *  {@link java.util.Map.Entry} that doubles as a node in the linked list
  -     *  of sequenced mappings.  
  -     **/
  +     * {@link java.util.Map.Entry} that doubles as a node in the linked list
  +     * of sequenced mappings.  
  +     */
       private static class Entry implements Map.Entry {
           // Note: This class cannot easily be made clonable.  While the actual
           // implementation of a clone would be simple, defining the semantics is
  @@ -172,7 +173,7 @@
        *  Construct an empty sentinel used to hold the head (sentinel.next) and the
        *  tail (sentinel.prev) of the list.  The sentinel has a <code>null</code>
        *  key and value.
  -     **/
  +     */
       private static final Entry createSentinel() {
           Entry s = new Entry(null, null);
           s.prev = s;
  @@ -182,12 +183,12 @@
   
       /**
        *  Sentinel used to hold the head and tail of the list of entries.
  -     **/
  +     */
       private Entry sentinel;
   
       /**
        *  Map of keys to entries
  -     **/
  +     */
       private HashMap entries;
   
       /**
  @@ -195,13 +196,13 @@
        *  excluding modifications made through a collection view's iterator
        *  (e.g. entrySet().iterator().remove()).  This is used to create a
        *  fail-fast behavior with the iterators.
  -     **/
  +     */
       private transient long modCount = 0;
   
       /**
        *  Construct a new sequenced hash map with default initial size and load
        *  factor.
  -     **/
  +     */
       public SequencedHashMap() {
           sentinel = createSentinel();
           entries = new HashMap();
  @@ -214,7 +215,7 @@
        *  @param initialSize the initial size for the hash table 
        *
        *  @see HashMap#HashMap(int)
  -     **/
  +     */
       public SequencedHashMap(int initialSize) {
           sentinel = createSentinel();
           entries = new HashMap(initialSize);
  @@ -229,7 +230,7 @@
        *  @param loadFactor the load factor for the hash table.
        *
        *  @see HashMap#HashMap(int,float)
  -     **/
  +     */
       public SequencedHashMap(int initialSize, float loadFactor) {
           sentinel = createSentinel();
           entries = new HashMap(initialSize, loadFactor);
  @@ -239,7 +240,7 @@
        *  Construct a new sequenced hash map and add all the elements in the
        *  specified map.  The order in which the mappings in the specified map are
        *  added is defined by {@link #putAll(Map)}.  
  -     **/
  +     */
       public SequencedHashMap(Map m) {
           this();
           putAll(m);
  @@ -248,7 +249,7 @@
       /**
        *  Removes an internal entry from the linked list.  This does not remove
        *  it from the underlying map.
  -     **/
  +     */
       private void removeEntry(Entry entry) {
           entry.next.prev = entry.prev;
           entry.prev.next = entry.next;
  @@ -257,7 +258,7 @@
       /**
        *  Inserts a new internal entry to the tail of the linked list.  This does
        *  not add the entry to the underlying map.
  -     **/
  +     */
       private void insertEntry(Entry entry) {
           entry.next = sentinel;
           entry.prev = sentinel.prev;
  @@ -339,7 +340,7 @@
        *
        *  @return The first entry in the sequence, or <code>null</code> if the
        *  map is empty.
  -     **/
  +     */
       public Map.Entry getFirst() {
           // sentinel.next points to the "first" element of the sequence -- the head
           // of the list, which is exactly the entry we need to return.  We must test
  @@ -356,7 +357,7 @@
        *
        *  @return The first key in the sequence, or <code>null</code> if the
        *  map is empty.
  -     **/
  +     */
       public Object getFirstKey() {
           // sentinel.next points to the "first" element of the sequence -- the head
           // of the list -- and the requisite key is returned from it.  An empty list
  @@ -376,7 +377,7 @@
        *
        *  @return The first value in the sequence, or <code>null</code> if the
        *  map is empty.
  -     **/
  +     */
       public Object getFirstValue() {
           // sentinel.next points to the "first" element of the sequence -- the head
           // of the list -- and the requisite value is returned from it.  An empty
  @@ -406,7 +407,7 @@
        *
        *  @return The last entry in the sequence, or <code>null</code> if the map
        *  is empty.
  -     **/
  +     */
       public Map.Entry getLast() {
           // sentinel.prev points to the "last" element of the sequence -- the tail
           // of the list, which is exactly the entry we need to return.  We must test
  @@ -423,7 +424,7 @@
        *
        *  @return The last key in the sequence, or <code>null</code> if the map is
        *  empty.
  -     **/
  +     */
       public Object getLastKey() {
           // sentinel.prev points to the "last" element of the sequence -- the tail
           // of the list -- and the requisite key is returned from it.  An empty list
  @@ -443,7 +444,7 @@
        *
        *  @return The last value in the sequence, or <code>null</code> if the map
        *  is empty.
  -     **/
  +     */
       public Object getLastValue() {
           // sentinel.prev points to the "last" element of the sequence -- the tail
           // of the list -- and the requisite value is returned from it.  An empty
  @@ -502,7 +503,7 @@
       /**
        *  Fully remove an entry from the map, returning the old entry or null if
        *  there was no such entry with the specified key.
  -     **/
  +     */
       private Entry removeImpl(Object key) {
           Entry e = (Entry) entries.remove(key);
           if (e == null)
  @@ -521,7 +522,7 @@
        *  @param t the mappings that should be added to this map.
        *
        *  @throws NullPointerException if <code>t</code> is <code>null</code>
  -     **/
  +     */
       public void putAll(Map t) {
           Iterator iter = t.entrySet().iterator();
           while (iter.hasNext()) {
  @@ -572,7 +573,7 @@
        *  method is suitable for debugging purposes only.  If a specific format is
        *  required, use {@link #entrySet()}.{@link Set#iterator() iterator()} and
        *  iterate over the entries in the map formatting them as appropriate.
  -     **/
  +     */
       public String toString() {
           StringBuffer buf = new StringBuffer();
           buf.append('[');
  @@ -732,20 +733,20 @@
            *  on the same element.  Essientially, if this value is negative (i.e. the
            *  bit specified by {@link #REMOVED_MASK} is set), the current position
            *  has been removed.  If positive, remove can still be called.
  -         **/
  +         */
           private int returnType;
   
           /**
            *  Holds the "current" position in the iterator.  When pos.next is the
            *  sentinel, we've reached the end of the list.
  -         **/
  +         */
           private Entry pos = sentinel;
   
           /**
            *  Holds the expected modification count.  If the actual modification
            *  count of the map differs from this value, then a concurrent
            *  modification has occurred.
  -         **/
  +         */
           private transient long expectedModCount = modCount;
   
           /**
  @@ -753,7 +754,7 @@
            *  they were added.  The {@link #next()} method returns the type specified
            *  by <code>returnType</code> which must be either {@link #KEY}, {@link
            *  #VALUE}, or {@link #ENTRY}.
  -         **/
  +         */
           public OrderedIterator(int returnType) {
               //// Since this is a private inner class, nothing else should have
               //// access to the constructor.  Since we know the rest of the outer
  @@ -774,7 +775,7 @@
            *
            *  @return <code>true</code> if there are more elements left to be
            *  returned from the iterator; <code>false</code> otherwise.
  -         **/
  +         */
           public boolean hasNext() {
               return pos.next != sentinel;
           }
  @@ -789,7 +790,7 @@
            *
            *  @throws ConcurrentModificationException if a modification occurs in
            *  the underlying map.
  -         **/
  +         */
           public Object next() {
               if (modCount != expectedModCount) {
                   throw new ConcurrentModificationException();
  @@ -826,7 +827,7 @@
            *
            *  @throws ConcurrentModificationException if a modification occurs in
            *  the underlying map.
  -         **/
  +         */
           public void remove() {
               if ((returnType & REMOVED_MASK) != 0) {
                   throw new IllegalStateException("remove() must follow next()");
  @@ -891,7 +892,7 @@
        *
        *  @throws ArrayIndexOutOfBoundsException if the specified index is
        *  <code>&lt; 0</code> or <code>&gt;</code> the size of the map.
  -     **/
  +     */
       private Map.Entry getEntry(int index) {
           Entry pos = sentinel;
   
  
  
  
  1.16      +22 -5     jakarta-commons/collections/src/java/org/apache/commons/collections/SetUtils.java
  
  Index: SetUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/SetUtils.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- SetUtils.java	31 Aug 2003 17:26:44 -0000	1.15
  +++ SetUtils.java	9 Sep 2003 22:28:36 -0000	1.16
  @@ -64,6 +64,7 @@
   import java.util.SortedSet;
   import java.util.TreeSet;
   
  +import org.apache.commons.collections.decorators.OrderedSet;
   import org.apache.commons.collections.decorators.PredicatedSet;
   import org.apache.commons.collections.decorators.PredicatedSortedSet;
   import org.apache.commons.collections.decorators.TransformedSet;
  @@ -87,7 +88,7 @@
   
       /**
        * An empty unmodifiable set.
  -     * This uses the {@link Collections Collections} implementation 
  +     * This uses the {@link Collections} implementation 
        * and is provided for completeness.
        */
       public static final Set EMPTY_SET = Collections.EMPTY_SET;
  @@ -247,7 +248,7 @@
        * Set. It is important not to use the original set after invoking this 
        * method, as it is a backdoor for adding untransformed objects.
        *
  -     * @param set  the set to predicate, must not be null
  +     * @param set  the set to transform, must not be null
        * @param transformer  the transformer for the set, must not be null
        * @return a transformed set backed by the given set
        * @throws IllegalArgumentException  if the Set or Transformer is null
  @@ -256,6 +257,22 @@
           return TransformedSet.decorate(set, transformer);
       }
       
  +    
  +    /**
  +     * Returns a set that maintains the order of elements that are added
  +     * backed by the given set.
  +     * <p>
  +     * If an element is added twice, the order is determined by the first add.
  +     * The order is observed through the iterator or toArray.
  +     *
  +     * @param set  the set to order, must not be null
  +     * @return an ordered set backed by the given set
  +     * @throws IllegalArgumentException  if the Set is null
  +     */
  +    public static Set orderedSet(Set set) {
  +        return OrderedSet.decorate(set);
  +    }
  +    
       //-----------------------------------------------------------------------
       /**
        * Returns a synchronized sorted set backed by the given sorted set.
  @@ -332,7 +349,7 @@
        * Set. It is important not to use the original set after invoking this 
        * method, as it is a backdoor for adding untransformed objects.
        *
  -     * @param set  the set to predicate, must not be null
  +     * @param set  the set to transform, must not be null
        * @param transformer  the transformer for the set, must not be null
        * @return a transformed set backed by the given set
        * @throws IllegalArgumentException  if the Set or Transformer is null
  
  
  
  1.9       +3 -3      jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestAll.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TestAll.java	9 Sep 2003 03:03:57 -0000	1.8
  +++ TestAll.java	9 Sep 2003 22:28:36 -0000	1.9
  @@ -87,7 +87,7 @@
           suite.addTest(TestFixedSizeMap.suite());
           suite.addTest(TestFixedSizeSortedMap.suite());
           
  -        suite.addTest(TestSequencedSet.suite());
  +        suite.addTest(TestOrderedSet.suite());
           
           suite.addTest(TestTransformedBag.suite());
           suite.addTest(TestTransformedBuffer.suite());
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestOrderedSet.java
  
  Index: TestOrderedSet.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestOrderedSet.java,v 1.1 2003/09/09 22:28:36 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.decorators;
  
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.collections.TestSet;
  
  /**
   * Extension of {@link TestSet} for exercising the {@link OrderedSet}
   * implementation.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/09/09 22:28:36 $
   * 
   * @author Henning P. Schmiedehausen
   * @author Stephen Colebourne
   */
  public class TestOrderedSet extends TestSet {
  
      public TestOrderedSet(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestOrderedSet.class);
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestOrderedSet.class.getName()};
          junit.textui.TestRunner.main(testCaseName);
      }
  
      public Set makeEmptySet() {
          return OrderedSet.decorate(new HashSet());
      }
  
      public Set setupSet() {
          Set set = makeEmptySet();
  
          for (int i = 0; i < 10; i++) {
              set.add(Integer.toString(i));
          }
          return set;
      }
  
      public void testOrdering() {
          Set set = setupSet();
          Iterator it = set.iterator();
  
          for (int i = 0; i < 10; i++) {
              assertEquals("Sequence is wrong", Integer.toString(i), it.next());
          }
  
          for (int i = 0; i < 10; i += 2) {
              assertTrue("Must be able to remove int", set.remove(Integer.toString(i)));
          }
  
          it = set.iterator();
          for (int i = 1; i < 10; i += 2) {
              assertEquals("Sequence is wrong after remove ", Integer.toString(i), it.next());
          }
  
          for (int i = 0; i < 10; i++) {
              set.add(Integer.toString(i));
          }
  
          assertEquals("Size of set is wrong!", 10, set.size());
  
          it = set.iterator();
          for (int i = 1; i < 10; i += 2) {
              assertEquals("Sequence is wrong", Integer.toString(i), it.next());
          }
          for (int i = 0; i < 10; i += 2) {
              assertEquals("Sequence is wrong", Integer.toString(i), it.next());
          }
      }
      
  }