src/share/classes/java/util/Objects.java

Print this page
rev 9262 : 8033909: Objects.requireNonNull(T, Supplier) doesn't specify what happens if the passed supplier is null itself


 270      * throws a customized {@link NullPointerException} if it is.
 271      *
 272      * <p>Unlike the method {@link #requireNonNull(Object, String)},
 273      * this method allows creation of the message to be deferred until
 274      * after the null check is made. While this may confer a
 275      * performance advantage in the non-null case, when deciding to
 276      * call this method care should be taken that the costs of
 277      * creating the message supplier are less than the cost of just
 278      * creating the string message directly.
 279      *
 280      * @param obj     the object reference to check for nullity
 281      * @param messageSupplier supplier of the detail message to be
 282      * used in the event that a {@code NullPointerException} is thrown
 283      * @param <T> the type of the reference
 284      * @return {@code obj} if not {@code null}
 285      * @throws NullPointerException if {@code obj} is {@code null}
 286      * @since 1.8
 287      */
 288     public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {
 289         if (obj == null)
 290             throw new NullPointerException(messageSupplier.get());

 291         return obj;
 292     }
 293 }


 270      * throws a customized {@link NullPointerException} if it is.
 271      *
 272      * <p>Unlike the method {@link #requireNonNull(Object, String)},
 273      * this method allows creation of the message to be deferred until
 274      * after the null check is made. While this may confer a
 275      * performance advantage in the non-null case, when deciding to
 276      * call this method care should be taken that the costs of
 277      * creating the message supplier are less than the cost of just
 278      * creating the string message directly.
 279      *
 280      * @param obj     the object reference to check for nullity
 281      * @param messageSupplier supplier of the detail message to be
 282      * used in the event that a {@code NullPointerException} is thrown
 283      * @param <T> the type of the reference
 284      * @return {@code obj} if not {@code null}
 285      * @throws NullPointerException if {@code obj} is {@code null}
 286      * @since 1.8
 287      */
 288     public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {
 289         if (obj == null)
 290             throw new NullPointerException(messageSupplier == null ?
 291                                            null : messageSupplier.get());
 292         return obj;
 293     }
 294 }