common: Add a checkNotNull method to check contents of a Stream
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 30 Oct 2015 10:56:15 +0000 (06:56 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 20 Nov 2015 21:01:17 +0000 (16:01 -0500)
Change-Id: I7473a5df08a20396d7716bd986a1e1f4e1e0ef27
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/59325
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/AllCommonCoreTests.java
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/NonNullUtilsTest.java [new file with mode: 0644]
common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/NonNullUtils.java

index 3a9e4e68ebaa8cc7fd8b7c5d2e29f043a385cf92..f648667fa6d6e5de71f798371b15831a6c9ee0ea 100644 (file)
@@ -20,6 +20,7 @@ import org.junit.runners.Suite;
  */
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
+    NonNullUtilsTest.class,
     org.eclipse.tracecompass.common.core.tests.ObjectUtilsTest.class,
     org.eclipse.tracecompass.common.core.tests.collect.AllTests.class
 })
diff --git a/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/NonNullUtilsTest.java b/common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/NonNullUtilsTest.java
new file mode 100644 (file)
index 0000000..1d65b1d
--- /dev/null
@@ -0,0 +1,74 @@
+/*******************************************************************************
+ * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.common.core.tests;
+
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNullContents;
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
+import org.junit.Test;
+
+/**
+ * Tests for {@link NonNullUtils}.
+ */
+public class NonNullUtilsTest {
+
+    /**
+     * Test {@link NonNullUtils#checkNotNullContents} for a stream containing no
+     * null elements.
+     */
+    @Test
+    public void testCheckContents() {
+        List<@Nullable String> list = new ArrayList<>();
+        list.add("a");
+        list.add("b");
+        list.add("c");
+        list.add("d");
+
+        List<@NonNull String> out = checkNotNullContents(list.stream()).collect(Collectors.toList());
+        assertEquals(list, out);
+    }
+
+    /**
+     * Test {@link NonNullUtils#checkNotNullContents} with a null reference
+     * (should fail immediately).
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCheckContentsNullRef() {
+        checkNotNullContents(null);
+    }
+
+    /**
+     * Test {@link NonNullUtils#checkNotNullContents} with a stream containing a
+     * null value.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCheckContentsNullElement() {
+        List<@Nullable String> list = new ArrayList<>();
+        list.add("a");
+        list.add("b");
+        list.add(null);
+        list.add("d");
+
+        /*
+         * Should fail.
+         *
+         * Don't forget we need a terminal operation to process the contents of
+         * the stream!
+         */
+        checkNotNullContents(list.stream()).collect(Collectors.toList());
+    }
+}
index 3b15a746612ef11a8251fa4c51b4b77d990b0661..3a028804d3442452293ba7d293531b6ca699869e 100644 (file)
@@ -13,6 +13,7 @@
 package org.eclipse.tracecompass.common.core;
 
 import java.util.Map;
+import java.util.stream.Stream;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
@@ -151,5 +152,23 @@ public final class NonNullUtils {
         return map;
     }
 
-
+    /**
+     * Ensures a {@link Stream} does not contain any null values.
+     *
+     * This also "upcasts" the reference from a Stream<@Nullable T> to a
+     * Stream<@NonNull T>.
+     *
+     * @param stream
+     *            The stream to check for
+     * @return A stream with the same elements
+     * @throws NullPointerException
+     *             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) {
+        if (stream == null) {
+            throw new NullPointerException();
+        }
+        return checkNotNull(stream.map(t -> checkNotNull(t)));
+    }
 }
This page took 0.029048 seconds and 5 git commands to generate.