changeset 12927:d715a59bca20

8138963: java.lang.Objects new method to default to non-null Summary: add java.util.Object.nonNullElse and nonNullElseGet Reviewed-by: dfuchs, jrose, psandoz, smarks, igerasim, chegar
author rriggs
date Wed, 21 Oct 2015 14:18:49 -0400
parents 796a4f0d5082
children 9a84eb7c34e1
files src/java.base/share/classes/java/util/Objects.java test/java/util/Objects/BasicObjectsTest.java
diffstat 2 files changed, 82 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/Objects.java	Tue Oct 20 17:15:00 2015 -0700
+++ b/src/java.base/share/classes/java/util/Objects.java	Wed Oct 21 14:18:49 2015 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -282,6 +282,43 @@
     }
 
     /**
+     * Returns the first argument if it is non-{@code null} and
+     * otherwise returns the non-{@code null} second argument.
+     *
+     * @param obj an object
+     * @param defaultObj a non-{@code null} object to return if the first argument
+     *                   is {@code null}
+     * @param <T> the type of the reference
+     * @return the first argument if it is non-{@code null} and
+     *        otherwise the second argument if it is non-{@code null}
+     * @throws NullPointerException if both {@code obj} is null and
+     *        {@code defaultObj} is {@code null}
+     * @since 9
+     */
+    public static <T> T nonNullElse(T obj, T defaultObj) {
+        return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj");
+    }
+
+    /**
+     * Returns the first argument if it is non-{@code null} and otherwise
+     * returns the non-{@code null} value of {@code supplier.get()}.
+     *
+     * @param obj an object
+     * @param supplier of a non-{@code null} object to return if the first argument
+     *                 is {@code null}
+     * @param <T> the type of the first argument and return type
+     * @return the first argument if it is non-{@code null} and otherwise
+     *         the value from {@code supplier.get()} if it is non-{@code null}
+     * @throws NullPointerException if both {@code obj} is null and
+     *        either the {@code supplier} is {@code null} or
+     *        the {@code supplier.get()} value is {@code null}
+     * @since 9
+     */
+    public static <T> T nonNullElseGet(T obj, Supplier<? extends T> supplier) {
+        return (obj != null) ? obj : requireNonNull(requireNonNull(supplier, "supplier").get(), "supplier.get()");
+    }
+
+    /**
      * Checks that the specified object reference is not {@code null} and
      * throws a customized {@link NullPointerException} if it is.
      *
--- a/test/java/util/Objects/BasicObjectsTest.java	Tue Oct 20 17:15:00 2015 -0700
+++ b/test/java/util/Objects/BasicObjectsTest.java	Wed Oct 21 14:18:49 2015 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -44,6 +44,7 @@
         errors += testRequireNonNull();
         errors += testIsNull();
         errors += testNonNull();
+        errors += testNonNullOf();
         if (errors > 0 )
             throw new RuntimeException();
     }
@@ -232,4 +233,46 @@
 
         return errors;
     }
+
+    private static int testNonNullOf() {
+        int errors = 0;
+        String defString = new String("default");
+        String nullString = null;
+        String nonNullString = "non-null";
+
+        // Confirm the compile time return type matches
+        String result = Objects.nonNullElse(nullString, defString);
+        errors += (result == defString) ? 0 : 1;
+        errors += (Objects.nonNullElse(nonNullString, defString) == nonNullString) ? 0 : 1;
+        errors += (Objects.nonNullElse(nonNullString, null) == nonNullString) ? 0 : 1;
+        try {
+            Objects.nonNullElse(null, null);
+            errors += 1;
+        } catch (NullPointerException npe) {
+            // expected
+            errors += npe.getMessage().equals("defaultObj") ? 0 : 1;
+        }
+
+
+        // Test nonNullElseGet with a supplier
+        errors += (Objects.nonNullElseGet(nullString, () -> defString) == defString) ? 0 : 1;
+        errors += (Objects.nonNullElseGet(nonNullString, () -> defString) == nonNullString) ? 0 : 1;
+        errors += (Objects.nonNullElseGet(nonNullString, () -> null) == nonNullString) ? 0 : 1;
+
+        try {
+            Objects.nonNullElseGet(null, () -> null);
+            errors += 1;
+        } catch (NullPointerException npe) {
+            // expected
+            errors += npe.getMessage().equals("supplier.get()") ? 0 : 1;
+        }
+        try {       // supplier is null
+            Objects.nonNullElseGet(null, null);
+            errors += 1;
+        } catch (NullPointerException npe) {
+            // expected
+            errors += npe.getMessage().equals("supplier") ? 0 : 1;
+        }
+        return errors;
+    }
 }