commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/set TestMapBackedSet.java TestMapBackedSet2.java TestAll.java
Date Tue, 20 Apr 2004 23:51:31 GMT
scolebourne    2004/04/20 16:51:31

  Modified:    collections RELEASE-NOTES.html
               collections/src/test/org/apache/commons/collections/set
                        TestAll.java
  Added:       collections/src/java/org/apache/commons/collections/set
                        MapBackedSet.java
               collections/src/test/org/apache/commons/collections/set
                        TestMapBackedSet.java TestMapBackedSet2.java
  Log:
  Add MapBackedSet that creates a Set by decorating a Map
  
  Revision  Changes    Path
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/set/MapBackedSet.java
  
  Index: MapBackedSet.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.collections.set;
  
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  
  /**
   * Decorates a <code>Map</code> to obtain <code>Set</code> behaviour.
   * <p>
   * This class is used to create a <code>Set</code> with the same properties
as
   * the key set of any map. Thus, a ReferenceSet can be created by wrapping a
   * <code>ReferenceMap</code> in an instance of this class.
   * <p>
   * Most map implementation can be used to create a set by passing in dummy values.
   * Exceptions include <code>BidiMap</code> implementations, as they require
unique values.
   *
   * @since Commons Collections 3.1
   * @version $Revision: 1.1 $ $Date: 2004/04/20 23:51:31 $
   * 
   * @author Stephen Colebourne
   */
  public final class MapBackedSet implements Set {
      
      /** The map being used as the backing store */
      protected final Map map;
      /** The dummyValue to use */
      protected final Object dummyValue;
  
      /**
       * Factory method to create a set from a map.
       * 
       * @param map  the map to decorate, must not be null
       * @throws IllegalArgumentException if set is null
       */
      public static Set decorate(Map map) {
          return decorate(map, null);
      }
  
      /**
       * Factory method to create a set from a map.
       * 
       * @param map  the map to decorate, must not be null
       * @param dummyValue  the dummy value to use
       * @throws IllegalArgumentException if map is null
       */
      public static Set decorate(Map map, Object dummyValue) {
          if (map == null) {
              throw new IllegalArgumentException("The map must not be null");
          }
          return new MapBackedSet(map, dummyValue);
      }
  
      //-----------------------------------------------------------------------
      /**
       * Constructor that wraps (not copies).
       * 
       * @param map  the map to decorate, must not be null
       * @param dummyValue  the dummy value to use
       * @throws IllegalArgumentException if map is null
       */
      private MapBackedSet(Map map, Object dummyValue) {
          super();
          this.map = map;
          this.dummyValue = dummyValue;
      }
  
      //-----------------------------------------------------------------------
      public int size() {
          return map.size();
      }
  
      public boolean isEmpty() {
          return map.isEmpty();
      }
  
      public Iterator iterator() {
          return map.keySet().iterator();
      }
  
      public boolean contains(Object obj) {
          return map.containsKey(obj);
      }
  
      public boolean containsAll(Collection coll) {
          return map.keySet().containsAll(coll);
      }
  
      public boolean add(Object obj) {
          int size = map.size();
          map.put(obj, dummyValue);
          return (map.size() != size);
      }
  
      public boolean addAll(Collection coll) {
          int size = map.size();
          for (Iterator it = coll.iterator(); it.hasNext();) {
              Object obj = (Object) it.next();
              map.put(obj, dummyValue);
          }
          return (map.size() != size);
      }
  
      public boolean remove(Object obj) {
          int size = map.size();
          map.remove(obj);
          return (map.size() != size);
      }
  
      public boolean removeAll(Collection coll) {
          return map.keySet().removeAll(coll);
      }
  
      public boolean retainAll(Collection coll) {
          return map.keySet().retainAll(coll);
      }
  
      public void clear() {
          map.clear();
      }
  
      public Object[] toArray() {
          return map.keySet().toArray();
      }
  
      public Object[] toArray(Object[] array) {
          return map.keySet().toArray(array);
      }
  
      public boolean equals(Object obj) {
          return map.keySet().equals(obj);
      }
  
      public int hashCode() {
          return map.keySet().hashCode();
      }
  
  }
  
  
  
  1.39      +1 -0      jakarta-commons/collections/RELEASE-NOTES.html
  
  Index: RELEASE-NOTES.html
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/RELEASE-NOTES.html,v
  retrieving revision 1.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- RELEASE-NOTES.html	20 Apr 2004 23:47:53 -0000	1.38
  +++ RELEASE-NOTES.html	20 Apr 2004 23:51:31 -0000	1.39
  @@ -29,6 +29,7 @@
   <li>MultiKeyMap - A map that allows multiple keys to be used to map the value</li>
   <li>TransformedPredicate - A predicate where the input object is transformed [26946]</li>
   <li>ObjectGraphIterator - An iterator that can iterate over a graph of objects</li>
  +<li>MapBackedSet - Set created by decorating a map</li>
   <li>AbstractReferenceMap - New base class for reference maps [26503]</li>
   </ul>
   
  
  
  
  1.6       +3 -1      jakarta-commons/collections/src/test/org/apache/commons/collections/set/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/set/TestAll.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestAll.java	18 Feb 2004 01:20:39 -0000	1.5
  +++ TestAll.java	20 Apr 2004 23:51:31 -0000	1.6
  @@ -43,6 +43,8 @@
           
           suite.addTest(TestCompositeSet.suite());
           suite.addTest(TestListOrderedSet.suite());
  +        suite.addTest(TestMapBackedSet.suite());
  +        suite.addTest(TestMapBackedSet2.suite());
           suite.addTest(TestPredicatedSet.suite());
           suite.addTest(TestPredicatedSortedSet.suite());
           suite.addTest(TestTransformedSet.suite());
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/set/TestMapBackedSet.java
  
  Index: TestMapBackedSet.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.collections.set;
  
  import java.util.Set;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.collections.map.HashedMap;
  
  /**
   * JUnit test.
   *
   * @since Commons Collections 3.1
   * @version $Revision: 1.1 $ $Date: 2004/04/20 23:51:31 $
   * 
   * @author Stephen Colebourne
   */
  public class TestMapBackedSet extends AbstractTestSet {
  
      public TestMapBackedSet(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestMapBackedSet.class);
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestMapBackedSet.class.getName()};
          junit.textui.TestRunner.main(testCaseName);
      }
  
      public Set makeEmptySet() {
          return MapBackedSet.decorate(new HashedMap());
      }
  
  }
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/set/TestMapBackedSet2.java
  
  Index: TestMapBackedSet2.java
  ===================================================================
  /*
   *  Copyright 2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package org.apache.commons.collections.set;
  
  import java.util.Iterator;
  import java.util.Set;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.collections.map.LinkedMap;
  
  /**
   * JUnit test.
   *
   * @since Commons Collections 3.1
   * @version $Revision: 1.1 $ $Date: 2004/04/20 23:51:31 $
   * 
   * @author Stephen Colebourne
   */
  public class TestMapBackedSet2 extends AbstractTestSet {
  
      public TestMapBackedSet2(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestMapBackedSet2.class);
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestMapBackedSet2.class.getName()};
          junit.textui.TestRunner.main(testCaseName);
      }
  
      public Set makeEmptySet() {
          return MapBackedSet.decorate(new LinkedMap());
      }
  
      protected 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());
          }
      }
      
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message