common: Add a checkNotNullContents() for arrays
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Tue, 24 Nov 2015 23:50:31 +0000 (18:50 -0500)
committerAlexandre Montplaisir <alexmonthy@efficios.com>
Fri, 27 Nov 2015 22:07:05 +0000 (17:07 -0500)
For Collections we can easily go through a Stream and back
to call checkNotNullContents(Stream<T>), for arrays it's slightly
more complicated, so we might as well define a new method
specifically for arrays.

Also add some "tests" (not actual runnable tests, but just snippets
of code that should compile) to make sure the annotations are working
correctly.

Change-Id: I2d18ba912076ea98cfca3628e7c8d6077209f74b
Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Reviewed-on: https://git.eclipse.org/r/59427
Reviewed-by: Hudson CI
Reviewed-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tested-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/NonNullUtilsTest.java
common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/NonNullUtils.java

index 1d65b1da77bb10876448d5b63d712e92bfb5d2f6..e164b9b816eaa43f944121b4640e61888bd1e66e 100644 (file)
@@ -9,12 +9,16 @@
 
 package org.eclipse.tracecompass.common.core.tests;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNullContents;
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
@@ -27,11 +31,77 @@ import org.junit.Test;
 public class NonNullUtilsTest {
 
     /**
-     * Test {@link NonNullUtils#checkNotNullContents} for a stream containing no
-     * null elements.
+     * Test code to ensure the null-annotations on
+     * {@link NonNullUtils#checkNotNullContents(Stream)} work correctly.
+     *
+     * Not mean to be run as a {@link Test}, the code should simply not produce
+     * any compilation errors or warnings.
+     */
+    public void testAnnotationsCheckNotNullContentsStream() {
+        Stream<String> a                     = (new ArrayList<String>()).stream();
+        Stream<@Nullable String> b           = (new ArrayList<@Nullable String>()).stream();
+        @Nullable Stream<String> c           = (new ArrayList<String>()).stream();
+        @Nullable Stream<@Nullable String> d = (new ArrayList<@Nullable String>()).stream();
+        Stream<@Nullable String> e           = (new ArrayList<@Nullable String>()).stream();
+        @NonNull Stream<String> f            = checkNotNull((new ArrayList<String>()).stream());
+        @NonNull Stream<@NonNull String> g   = checkNotNull((new ArrayList<@NonNull String>()).stream());
+
+        @NonNull Stream<@NonNull String> checkedA = checkNotNullContents(a);
+        @NonNull Stream<@NonNull String> checkedB = checkNotNullContents(b);
+        @NonNull Stream<@NonNull String> checkedC = checkNotNullContents(c);
+        @NonNull Stream<@NonNull String> checkedD = checkNotNullContents(d);
+        @NonNull Stream<@NonNull String> checkedE = checkNotNullContents(e);
+        @NonNull Stream<@NonNull String> checkedF = checkNotNullContents(f);
+        @NonNull Stream<@NonNull String> checkedG = checkNotNullContents(g);
+
+        assertNotNull(checkedA);
+        assertNotNull(checkedB);
+        assertNotNull(checkedC);
+        assertNotNull(checkedD);
+        assertNotNull(checkedE);
+        assertNotNull(checkedF);
+        assertNotNull(checkedG);
+    }
+
+    /**
+     * Test code to ensure the null-annotations on
+     * {@link NonNullUtils#checkNotNullContents(Object[])} work correctly.
+     *
+     * Not mean to be run as a {@link Test}, the code should simply not produce
+     * any compilation errors or warnings.
+     */
+    public void testAnnotationsCheckNotNullContentsArray() {
+        String[] a                      = new String[] {};
+        @Nullable String[] b            = new @Nullable String[] {};
+        String @Nullable [] c           = new String[] {};
+        @Nullable String @Nullable [] d = new @Nullable String[] {};
+        @NonNull String[] e             = new @NonNull String[] {};
+        String @NonNull [] f            = new String[] {};
+        @NonNull String @NonNull [] g   = new @NonNull String[] {};
+
+        @NonNull String @NonNull [] checkedA = checkNotNullContents(a);
+        @NonNull String @NonNull [] checkedB = checkNotNullContents(b);
+        @NonNull String @NonNull [] checkedC = checkNotNullContents(c);
+        @NonNull String @NonNull [] checkedD = checkNotNullContents(d);
+        @NonNull String @NonNull [] checkedE = checkNotNullContents(e);
+        @NonNull String @NonNull [] checkedF = checkNotNullContents(f);
+        @NonNull String @NonNull [] checkedG = checkNotNullContents(g);
+
+        assertNotNull(checkedA);
+        assertNotNull(checkedB);
+        assertNotNull(checkedC);
+        assertNotNull(checkedD);
+        assertNotNull(checkedE);
+        assertNotNull(checkedF);
+        assertNotNull(checkedG);
+    }
+
+    /**
+     * Test {@link NonNullUtils#checkNotNullContents(Stream)} for a stream
+     * containing no null elements.
      */
     @Test
-    public void testCheckContents() {
+    public void testCheckContentsStream() {
         List<@Nullable String> list = new ArrayList<>();
         list.add("a");
         list.add("b");
@@ -43,20 +113,20 @@ public class NonNullUtilsTest {
     }
 
     /**
-     * Test {@link NonNullUtils#checkNotNullContents} with a null reference
-     * (should fail immediately).
+     * Test {@link NonNullUtils#checkNotNullContents(Stream)} with a null
+     * reference (should fail immediately).
      */
     @Test(expected = NullPointerException.class)
-    public void testCheckContentsNullRef() {
-        checkNotNullContents(null);
+    public void testCheckContentsStreamNullRef() {
+        checkNotNullContents((Stream<@Nullable ?>) null);
     }
 
     /**
-     * Test {@link NonNullUtils#checkNotNullContents} with a stream containing a
-     * null value.
+     * Test {@link NonNullUtils#checkNotNullContents(Stream)} with a stream
+     * containing a null value.
      */
     @Test(expected = NullPointerException.class)
-    public void testCheckContentsNullElement() {
+    public void testCheckContentsStreamNullElement() {
         List<@Nullable String> list = new ArrayList<>();
         list.add("a");
         list.add("b");
@@ -71,4 +141,43 @@ public class NonNullUtilsTest {
          */
         checkNotNullContents(list.stream()).collect(Collectors.toList());
     }
+
+    /**
+     * Test {@link NonNullUtils#checkNotNullContents(Object[])} for an array
+     * containing no null elements.
+     */
+    @Test
+    public void testCheckContentsArray() {
+        @Nullable String[] array = new @Nullable String[3];
+        array[0] = "a";
+        array[1] = "b";
+        array[2] = "c";
+
+        @NonNull String[] out = checkNotNullContents(array);
+        assertArrayEquals(array, out);
+    }
+
+    /**
+     * Test {@link NonNullUtils#checkNotNullContents(Object[])} with a null
+     * reference (should fail immediately).
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCheckContentsArrayNullRef() {
+        checkNotNullContents((@Nullable Object[]) null);
+    }
+
+    /**
+     * Test {@link NonNullUtils#checkNotNullContents(Object[])} with an array
+     * containing a null value.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCheckContentsArrayNullElement() {
+        @Nullable String[] array = new @Nullable String[3];
+        array[0] = "a";
+        array[1] = null;
+        array[2] = "c";
+
+        /* Should fail */
+        checkNotNullContents(array);
+    }
 }
index 3a028804d3442452293ba7d293531b6ca699869e..119c3fac641c39965cb395ee93203ff0daffe47d 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.eclipse.tracecompass.common.core;
 
+import java.util.Arrays;
 import java.util.Map;
 import java.util.stream.Stream;
 
@@ -165,10 +166,30 @@ public final class NonNullUtils {
      *             If the stream itself or any of its values are null
      * @since 2.0
      */
-    public static <T> Stream<@NonNull T> checkNotNullContents(@Nullable Stream<@Nullable T> stream) {
+    public static <T> Stream<@NonNull T> checkNotNullContents(@Nullable Stream<T> stream) {
         if (stream == null) {
             throw new NullPointerException();
         }
         return checkNotNull(stream.map(t -> checkNotNull(t)));
     }
+
+    /**
+     * Ensures an array does not contain any null elements.
+     *
+     * @param array
+     *            The array to check
+     * @return The same array, now with guaranteed @NonNull elements
+     * @throws NullPointerException
+     *             If the array reference or any contained element was null
+     * @since 2.0
+     */
+    public static <T> @NonNull T[] checkNotNullContents(T @Nullable [] array) {
+        if (array == null) {
+            throw new NullPointerException();
+        }
+        Arrays.stream(array).forEach(elem -> checkNotNull(elem));
+        @SuppressWarnings("null")
+        @NonNull T[] ret = (@NonNull T[]) array;
+        return ret;
+    }
 }
This page took 0.027739 seconds and 5 git commands to generate.