sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1592865 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/internal/util/CheckedArrayList.java test/java/org/apache/sis/internal/util/CheckedArrayListTest.java
Date Tue, 06 May 2014 20:17:48 GMT
Author: desruisseaux
Date: Tue May  6 20:17:48 2014
New Revision: 1592865

URL: http://svn.apache.org/r1592865
Log:
Fixed a hole in type checking: need to wrap subList. Note that this is only a partial fix
since it does not protect agaisnt null value. Furthermore, other holes still exists (e.g.
in listIterator()). We are only performing the checks on a "best effort" basis.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CheckedArrayListTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java?rev=1592865&r1=1592864&r2=1592865&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
[UTF-8] Tue May  6 20:17:48 2014
@@ -50,7 +50,7 @@ import static org.apache.sis.util.Argume
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-2.1)
- * @version 0.4
+ * @version 0.5
  * @module
  *
  * @see Collections#checkedList(List, Class)
@@ -148,9 +148,10 @@ public final class CheckedArrayList<E> e
      *
      * @param  element the object to check, or {@code null}.
      * @return {@code true} if the instance is valid, {@code false} if it shall be ignored.
-     * @throws IllegalArgumentException if the specified element can not be added to this
list.
+     * @throws NullPointerException if the given element is {@code null}.
+     * @throws ClassCastException if the given element is not of the expected type.
      */
-    private boolean ensureValid(final E element) throws IllegalArgumentException {
+    private boolean ensureValid(final E element) {
         if (type.isInstance(element)) {
             return true;
         }
@@ -166,7 +167,7 @@ public final class CheckedArrayList<E> e
         if (element == null) {
             throw new NullArgumentException(message);
         } else {
-            throw new IllegalArgumentException(message);
+            throw new ClassCastException(message);
         }
     }
 
@@ -175,10 +176,11 @@ public final class CheckedArrayList<E> e
      *
      * @param  collection the collection to check, or {@code null}.
      * @return The potentially filtered collection of elements to add.
-     * @throws IllegalArgumentException if at least one element can not be added to this
list.
+     * @throws NullPointerException if an element is {@code null}.
+     * @throws ClassCastException if an element is not of the expected type.
      */
     @SuppressWarnings("unchecked")
-    private List<E> ensureValidCollection(final Collection<? extends E> collection)
throws IllegalArgumentException {
+    private List<E> ensureValidCollection(final Collection<? extends E> collection)
{
         int count = 0;
         final Object[] array = collection.toArray();
         for (final Object element : array) {
@@ -213,10 +215,11 @@ public final class CheckedArrayList<E> e
      * @param  element element to be stored at the specified position.
      * @return the element previously at the specified position.
      * @throws IndexOutOfBoundsException if index out of range.
-     * @throws IllegalArgumentException if the specified element is not of the expected type.
+     * @throws NullPointerException if the given element is {@code null}.
+     * @throws ClassCastException if the given element is not of the expected type.
      */
     @Override
-    public E set(final int index, final E element) throws IllegalArgumentException {
+    public E set(final int index, final E element) {
         if (ensureValid(element)) {
             return super.set(index, element);
         }
@@ -228,10 +231,11 @@ public final class CheckedArrayList<E> e
      *
      * @param  element element to be appended to this list.
      * @return always {@code true}.
-     * @throws IllegalArgumentException if the specified element is not of the expected type.
+     * @throws NullPointerException if the given element is {@code null}.
+     * @throws ClassCastException if the given element is not of the expected type.
      */
     @Override
-    public boolean add(final E element) throws IllegalArgumentException {
+    public boolean add(final E element) {
         if (ensureValid(element)) {
             return super.add(element);
         }
@@ -244,10 +248,11 @@ public final class CheckedArrayList<E> e
      * @param  index index at which the specified element is to be inserted.
      * @param  element element to be inserted.
      * @throws IndexOutOfBoundsException if index out of range.
-     * @throws IllegalArgumentException if the specified element is not of the expected type.
+     * @throws NullPointerException if the given element is {@code null}.
+     * @throws ClassCastException if the given element is not of the expected type.
      */
     @Override
-    public void add(final int index, final E element) throws IllegalArgumentException {
+    public void add(final int index, final E element) {
         if (ensureValid(element)) {
             super.add(index, element);
         }
@@ -259,10 +264,11 @@ public final class CheckedArrayList<E> e
      *
      * @param  collection the elements to be inserted into this list.
      * @return {@code true} if this list changed as a result of the call.
-     * @throws IllegalArgumentException if at least one element is not of the expected type.
+     * @throws NullPointerException if an element is {@code null}.
+     * @throws ClassCastException if an element is not of the expected type.
      */
     @Override
-    public boolean addAll(final Collection<? extends E> collection) throws IllegalArgumentException
{
+    public boolean addAll(final Collection<? extends E> collection) {
         return super.addAll(ensureValidCollection(collection));
     }
 
@@ -273,10 +279,26 @@ public final class CheckedArrayList<E> e
      * @param  index index at which to insert first element fromm the specified collection.
      * @param  collection elements to be inserted into this list.
      * @return {@code true} if this list changed as a result of the call.
-     * @throws IllegalArgumentException if at least one element is not of the expected type.
+     * @throws NullPointerException if an element is {@code null}.
+     * @throws ClassCastException if an element is not of the expected type.
      */
     @Override
-    public boolean addAll(final int index, final Collection<? extends E> collection)
throws IllegalArgumentException {
+    public boolean addAll(final int index, final Collection<? extends E> collection)
{
         return super.addAll(index, ensureValidCollection(collection));
     }
+
+    /**
+     * Returns a checked sublist.
+     *
+     * <p><b>Limitation:</b> current implementation checks only the type.
+     * It does not prevent the insertion of {@code null} values.</p>
+     *
+     * @param  fromIndex Index of the first element.
+     * @param  toIndex   Index after the last element.
+     * @return The sublist in the given index range.
+     */
+    @Override
+    public List<E> subList(final int fromIndex, final int toIndex) {
+        return Collections.checkedList(super.subList(fromIndex, toIndex), type);
+    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CheckedArrayListTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CheckedArrayListTest.java?rev=1592865&r1=1592864&r2=1592865&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CheckedArrayListTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/CheckedArrayListTest.java
[UTF-8] Tue May  6 20:17:48 2014
@@ -16,9 +16,11 @@
  */
 package org.apache.sis.internal.util;
 
+import java.util.List;
 import java.util.Arrays;
 import java.util.Collection;
 import org.apache.sis.util.NullArgumentException;
+import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -30,7 +32,7 @@ import static org.apache.sis.test.Assert
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4
- * @version 0.4
+ * @version 0.5
  * @module
  */
 public final strictfp class CheckedArrayListTest extends TestCase {
@@ -90,16 +92,40 @@ public final strictfp class CheckedArray
      * Ensures that we can not element of the wrong type.
      */
     @Test
-    @SuppressWarnings({"unchecked","rawtypes"})
     public void testAddWrongType() {
-        final CheckedArrayList list = new CheckedArrayList<>(String.class);
+        final CheckedArrayList<String> list = new CheckedArrayList<>(String.class);
+        final String message = testAddWrongType(list);
+        assertTrue("element", message.contains("element"));
+        assertTrue("Integer", message.contains("Integer"));
+        assertTrue("String",  message.contains("String"));
+    }
+
+    /**
+     * Implementation of {@link #testAddWrongType()}, also shared by {@link #testAddWrongTypeToSublist()}.
+     * Returns the exception message.
+     */
+    @SuppressWarnings({"unchecked","rawtypes"})
+    private static String testAddWrongType(final List list) {
         try {
             list.add(Integer.valueOf(4));
-        } catch (IllegalArgumentException e) {
-            final String message = e.getMessage();
-            assertTrue("element", message.contains("element"));
-            assertTrue("Integer", message.contains("Integer"));
-            assertTrue("String",  message.contains("String"));
+            fail("Shall not be allowed to add an integer to the list.");
+            return null;
+        } catch (ClassCastException e) {
+            return e.getMessage();
         }
     }
+
+    /**
+     * Ensures that we can not element of the wrong type in a sublist.
+     */
+    @Test
+    @DependsOnMethod("testAddWrongType")
+    public void testAddWrongTypeToSublist() {
+        final CheckedArrayList<String> list = new CheckedArrayList<>(String.class);
+        assertTrue(list.add("One"));
+        assertTrue(list.add("Two"));
+        assertTrue(list.add("Three"));
+        testAddWrongType(list.subList(1, 3));
+        // Exception message is JDK-dependent, so we can not test it.
+    }
 }



Mime
View raw message