common: Add utility class for non-null things
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Tue, 2 Dec 2014 13:58:28 +0000 (08:58 -0500)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Thu, 11 Dec 2014 15:00:01 +0000 (10:00 -0500)
These utility methods should help avoid the crufty

    @SuppressWarnings("null")
    @NonNull ret = ...;
    return ret;

pattern we had been using so far.

Change-Id: I8c1aa8469dc466a1b42aae799b24789dc23837cf
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/37516
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Hudson CI
org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/NonNullUtils.java [new file with mode: 0644]

diff --git a/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/NonNullUtils.java b/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/NonNullUtils.java
new file mode 100644 (file)
index 0000000..e2f0897
--- /dev/null
@@ -0,0 +1,62 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * 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
+ *
+ * Contributors:
+ *   Alexandre Montplaisir - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.common.core;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+
+/**
+ * Utility methods to handle {@link org.eclipse.jdt.annotation.NonNull}
+ * annotations.
+ *
+ * @author Alexandre Montplaisir
+ */
+public final class NonNullUtils {
+
+    private NonNullUtils() {}
+
+    /**
+     * Convert a potentially null string into an empty one if it is null.
+     *
+     * @param str
+     *            The string to null-check, and convert to an empty string if
+     *            null.
+     * @return The non-null string
+     */
+    public static String nullToEmptyString(@Nullable String str) {
+        if (str == null) {
+            return ""; //$NON-NLS-1$
+        }
+        return str;
+    }
+
+    /**
+     * Convert a non-annotated object reference to a {@link NonNull} one.
+     *
+     * If the reference is actually null, a {@link NullPointerException} is
+     * thrown. This is usually more desirable than letting an unwanted null
+     * reference go through and fail much later.
+     *
+     * @param obj
+     *            The object that is supposed to be non-null
+     * @return A {@link NonNull} reference to the same object
+     * @throws NullPointerException
+     *             If the reference was actually null
+     */
+    public static <T> T checkNotNull(@Nullable T obj) {
+        if (obj == null) {
+            throw new NullPointerException();
+        }
+        return obj;
+    }
+}
This page took 0.033512 seconds and 5 git commands to generate.