diff --git a/jdk/src/java.base/share/classes/java/util/Objects.java b/jdk/src/java.base/share/classes/java/util/Objects.java index 0d8d9e16833..65089553b87 100644 --- a/jdk/src/java.base/share/classes/java/util/Objects.java +++ b/jdk/src/java.base/share/classes/java/util/Objects.java @@ -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 @@ -281,6 +281,43 @@ public final class Objects { return obj != null; } + /** + * 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 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 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 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 nonNullElseGet(T obj, Supplier 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. diff --git a/jdk/test/java/util/Objects/BasicObjectsTest.java b/jdk/test/java/util/Objects/BasicObjectsTest.java index 3a1cad9cd32..878bb825019 100644 --- a/jdk/test/java/util/Objects/BasicObjectsTest.java +++ b/jdk/test/java/util/Objects/BasicObjectsTest.java @@ -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 @@ public class BasicObjectsTest { errors += testRequireNonNull(); errors += testIsNull(); errors += testNonNull(); + errors += testNonNullOf(); if (errors > 0 ) throw new RuntimeException(); } @@ -232,4 +233,46 @@ public class BasicObjectsTest { 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; + } }