sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1450137 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/util/collection/CodeListSet.java test/java/org/apache/sis/util/collection/CodeListSetTest.java
Date Tue, 26 Feb 2013 11:12:11 GMT
Author: desruisseaux
Date: Tue Feb 26 11:12:11 2013
New Revision: 1450137

URL: http://svn.apache.org/r1450137
Log:
Added test for collections of different kind of code lists.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CodeListSetTest.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java?rev=1450137&r1=1450136&r2=1450137&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java
[UTF-8] Tue Feb 26 11:12:11 2013
@@ -203,6 +203,13 @@ public class CodeListSet<E extends CodeL
     }
 
     /**
+     * Returns the bitmask to use for a bulk operation with an other set of code lists.
+     */
+    private long mask(final CodeListSet<?> other) {
+        return (elementType == other.elementType) ? other.values : 0;
+    }
+
+    /**
      * Adds all elements of the given collection from this set.
      *
      * @param  c The collection containing elements to be removed from this set.
@@ -211,10 +218,7 @@ public class CodeListSet<E extends CodeL
     @Override
     public boolean removeAll(final Collection<?> c) {
         if (c instanceof CodeListSet) {
-            final CodeListSet<?> o = (CodeListSet) c;
-            if (elementType == o.elementType) {
-                return values != (values &= ~o.values);
-            }
+            return values != (values &= ~mask((CodeListSet<?>) c));
         }
         return super.removeAll(c);
     }
@@ -228,10 +232,7 @@ public class CodeListSet<E extends CodeL
     @Override
     public boolean retainAll(final Collection<?> c) {
         if (c instanceof CodeListSet) {
-            final CodeListSet<?> o = (CodeListSet) c;
-            if (elementType == o.elementType) {
-                return values != (values &= o.values);
-            }
+            return values != (values &= mask((CodeListSet<?>) c));
         }
         return super.retainAll(c);
     }

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CodeListSetTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CodeListSetTest.java?rev=1450137&r1=1450136&r2=1450137&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CodeListSetTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CodeListSetTest.java
[UTF-8] Tue Feb 26 11:12:11 2013
@@ -17,6 +17,7 @@
 package org.apache.sis.util.collection;
 
 import org.opengis.referencing.cs.AxisDirection;
+import org.opengis.metadata.spatial.PixelOrientation;
 import org.apache.sis.test.TestCase;
 import org.apache.sis.test.DependsOnMethod;
 import org.junit.Test;
@@ -57,6 +58,19 @@ public final strictfp class CodeListSetT
     }
 
     /**
+     * Creates a code list of an other kind. The returned set contains a code list having
+     * the same ordinal value than {@link AxisDirection#NORTH}, so we can detect if the
+     * {@code SortedSet} confuses the code list types.
+     */
+    private CodeListSet<PixelOrientation> createOtherKind() {
+        // For the validity of the tests, ordinal value must be the same.
+        assertEquals(NORTH.ordinal(), PixelOrientation.LOWER_LEFT.ordinal());
+        final CodeListSet<PixelOrientation> c = new CodeListSet<>(PixelOrientation.class);
+        assertTrue(c.add(PixelOrientation.LOWER_LEFT));
+        return c;
+    }
+
+    /**
      * Tests the {@link CodeListSet#toArray()} method.
      * This will indirectly tests the iterator.
      */
@@ -91,6 +105,10 @@ public final strictfp class CodeListSetT
         assertFalse("WEST",   c.contains(WEST));
         assertTrue ("UP",     c.contains(UP));
         assertFalse("DOWN",   c.contains(DOWN));
+
+        assertFalse("Should be null-safe.", c.contains(null));
+        assertFalse("Code list of other kind should not be included.",
+                c.contains(PixelOrientation.LOWER_LEFT));
     }
 
     /**
@@ -100,6 +118,10 @@ public final strictfp class CodeListSetT
     @DependsOnMethod("testContains")
     public void testRemove() {
         final CodeListSet<AxisDirection> c = create(4);
+        assertFalse("Should be null-safe.", c.remove(null));
+        assertFalse("Code list of other kind should not be included.",
+                c.remove(PixelOrientation.LOWER_LEFT));
+
         assertTrue ("NORTH",  c.remove  (NORTH));
         assertFalse("SOUTH",  c.remove  (SOUTH));
         assertFalse("NORTH",  c.contains(NORTH));
@@ -129,6 +151,7 @@ public final strictfp class CodeListSetT
         assertTrue (c.containsAll(o));
         assertTrue (o.add(NORTH_EAST));
         assertFalse(c.containsAll(o));
+        assertFalse(c.containsAll(createOtherKind()));
     }
 
     /**
@@ -141,6 +164,7 @@ public final strictfp class CodeListSetT
         final CodeListSet<AxisDirection> o = create(2);
         assertTrue(o.add(NORTH_EAST)); // Extra value shall be ignored.
 
+        assertFalse(c.removeAll(createOtherKind()));
         assertTrue(c.removeAll(o));
         assertArrayEquals(new Object[] {UP, FUTURE}, c.toArray());
         assertFalse("Invoking a second time should not make any difference.", c.removeAll(o));
@@ -161,6 +185,8 @@ public final strictfp class CodeListSetT
         assertArrayEquals(new Object[] {NORTH, EAST}, c.toArray());
         assertFalse("Invoking a second time should not make any difference.", c.retainAll(o));
         assertEquals(2, c.size());
+        assertTrue(c.retainAll(createOtherKind()));
+        assertTrue(c.isEmpty());
     }
 
     /**



Mime
View raw message