JDK-8138963 : java.lang.Objects new method to default to non-null
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2015-10-06
  • Updated: 2019-10-31
  • Resolved: 2015-10-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b89Fixed
Related Reports
Relates :  
Relates :  
Description
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()");
    }

Comments
Review thread recap: http://mail.openjdk.java.net/pipermail/core-libs-dev/2015-October/035759.html
15-10-2015

Guava has an equivalent, Objects.firstNonNull(). See the bottom of https://github.com/google/guava/wiki/UsingAndAvoidingNullExplained and also http://google.github.io/guava/releases/18.0/api/docs/ But note that in Guava 18.0 (the Aug 2014 release) Objects.firstNonNull() has been deprecated in favor of MoreObjects.firstNonNull(). I'm not sure of the reason for this. A supplier-based version might be useful. Not sure if it's worth the clutter though. If it's added, it should probably have a different name so as to avoid overload ambiguity. Also, the parameter type should be Supplier<? extends T>.
06-10-2015

Would it make sense to also introduce a method with the second argument of type j.u.f.Supplier<T>, so that it can be lazily calculated upon the need?
06-10-2015