8138963: java.lang.Objects new method to default to non-null

Add java.util.Object.nonNullElse and nonNullElseGet

Reviewed-by: dfuchs, jrose, psandoz, smarks, igerasim, chegar
This commit is contained in:
Roger Riggs 2015-10-21 14:18:49 -04:00
parent 9aff88de9e
commit 4758ab0627
2 changed files with 82 additions and 2 deletions

View File

@ -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 <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.

View File

@ -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;
}
}