commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r731498 - in /commons/proper/collections/trunk/src: java/org/apache/commons/collections/list/SetUniqueList.java test/org/apache/commons/collections/list/TestSetUniqueList.java
Date Mon, 05 Jan 2009 09:09:56 GMT
Author: bayard
Date: Mon Jan  5 01:09:55 2009
New Revision: 731498

URL: http://svn.apache.org/viewvc?rev=731498&view=rev
Log:
Applying my patch from COLLECTIONS-307. Fixes the bug raised by Christian Semrau that SetUniqueList.subList()
was not redefining the uniqueness set when creating the sublist. 

Modified:
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/SetUniqueList.java
    commons/proper/collections/trunk/src/test/org/apache/commons/collections/list/TestSetUniqueList.java

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/SetUniqueList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/SetUniqueList.java?rev=731498&r1=731497&r2=731498&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/SetUniqueList.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/SetUniqueList.java
Mon Jan  5 01:09:55 2009
@@ -283,7 +283,26 @@
     }
 
     public List subList(int fromIndex, int toIndex) {
-        return new SetUniqueList(super.subList(fromIndex, toIndex), set);
+        List superSubList = super.subList(fromIndex, toIndex);
+        Set subSet = createSetBasedOnList(set, superSubList);
+        return new SetUniqueList(superSubList, subSet);
+    }
+
+    protected Set createSetBasedOnList(Set set, List list) {
+        Set subSet = null;
+        if(set.getClass().equals(HashSet.class)) {
+            subSet = new HashSet();
+        } else {
+            try {
+                subSet = (Set) set.getClass().newInstance();
+            } catch(InstantiationException ie) {
+                subSet = new HashSet();
+            } catch(IllegalAccessException iae) {
+                subSet = new HashSet();
+            }
+        }
+        subSet.addAll(list);
+        return subSet;
     }
 
     //-----------------------------------------------------------------------

Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/list/TestSetUniqueList.java?rev=731498&r1=731497&r2=731498&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
(original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
Mon Jan  5 01:09:55 2009
@@ -22,6 +22,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
+import java.util.Set;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
@@ -470,4 +471,50 @@
         assertEquals(4, decoratedList.size());
     }
 
+    public void testCollections307() {
+        List list = new ArrayList();
+        List uniqueList = SetUniqueList.decorate(list);
+
+        String hello = "Hello";
+        String world = "World";
+        uniqueList.add(hello);
+        uniqueList.add(world);
+
+        List subList = list.subList(0, 0);
+        List subUniqueList = uniqueList.subList(0, 0);
+
+        assertFalse(subList.contains(world)); // passes
+        assertFalse(subUniqueList.contains(world)); // fails
+
+        List worldList = new ArrayList();
+        worldList.add(world);
+        assertFalse(subList.contains("World")); // passes
+        assertFalse(subUniqueList.contains("World")); // fails
+
+        // repeat the test with a different class than HashSet; 
+        // which means subclassing SetUniqueList below
+        list = new ArrayList();
+        uniqueList = new SetUniqueList307(list, new java.util.TreeSet());
+
+        uniqueList.add(hello);
+        uniqueList.add(world);
+
+        subList = list.subList(0, 0);
+        subUniqueList = uniqueList.subList(0, 0);
+
+        assertFalse(subList.contains(world)); // passes
+        assertFalse(subUniqueList.contains(world)); // fails
+
+        worldList = new ArrayList();
+        worldList.add(world);
+        assertFalse(subList.contains("World")); // passes
+        assertFalse(subUniqueList.contains("World")); // fails
+    }
+
+    class SetUniqueList307 extends SetUniqueList {
+        public SetUniqueList307(List list, Set set) {
+            super(list, set);
+        }
+    }
+
 }



Mime
View raw message