-
Enhancement
-
Resolution: Fixed
-
P4
-
9
-
b89
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8142775 | emb-9 | Roger Riggs | P4 | Resolved | Fixed | team |
java.util.Objects includes a number of convenience methods to make it easier to work with references that may be null, typically by conditionally substituting a default value for a null object reference.
Methods should be added:
/**
* Returns the first argument if it is not {@code null} and
* otherwise returns the non-null second argument.
*
* @param obj an object
* @param defaultObj a non-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 not {@code null} and
* otherwise the second argument if it is not null
* @throws NullPointerException if both {@code obj} is null and
* {@code nullDefault} 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 not {@code null} and otherwise
* returns the non-null value of {@code supplier.get()}.
*
* @param obj an object
* @param supplier of a non-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 not {@code null} and otherwise
* the value from {@code supplier.get()} if it is not {@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()");
}
Methods should be added:
/**
* Returns the first argument if it is not {@code null} and
* otherwise returns the non-null second argument.
*
* @param obj an object
* @param defaultObj a non-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 not {@code null} and
* otherwise the second argument if it is not null
* @throws NullPointerException if both {@code obj} is null and
* {@code nullDefault} 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 not {@code null} and otherwise
* returns the non-null value of {@code supplier.get()}.
*
* @param obj an object
* @param supplier of a non-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 not {@code null} and otherwise
* the value from {@code supplier.get()} if it is not {@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()");
}
- backported by
-
JDK-8142775 java.lang.Objects new method to default to non-null
-
- Resolved
-
- relates to
-
JDK-8141652 Rename methods Objects.nonNullElse* to requireNonNullElse*
-
- Resolved
-
-
JDK-8233268 Improve integration of Objects.requireNonNull and JEP 358 Helpful NPE
-
- Open
-