From Josh on ###@###.###
(http://mail.openjdk.java.net/pipermail/core-libs-dev/2009-October/002891.html)
I strongly suggest that you do add these two methods:
/**
* Checks that the specified object reference is not {@code null}. This
* method is designed primarily for doing parameter validation in methods
* and constructors, as demonstrated below:
* <pre>
* public Foo(Bar bar) {
* this.bar = Objects.nonNull(bar);
* }
* </pre>
*
* @param obj the object reference to check for nullity
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T nonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@Link NullPointerException} if it is. This method
* is designed primarily for doing parameter validation in methods and
* constructors with multiple parameters, as demonstrated below:
* <pre>
* public Foo(Bar bar, Baz baz) {
* this.bar = Objects.nonNull(bar, "bar must not be null");
* this.baz = Objects.nonNull(baz, "baz must not be null");
* }
* </pre>
*
* @param obj the object reference to check for nullity
* @param message detail message to be used in the event that a {@code
* NullPointerException} is thrown
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T nonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}
They do a great job reducing the verbiage in validity-checking of arguments that must not be null.
(http://mail.openjdk.java.net/pipermail/core-libs-dev/2009-October/002891.html)
I strongly suggest that you do add these two methods:
/**
* Checks that the specified object reference is not {@code null}. This
* method is designed primarily for doing parameter validation in methods
* and constructors, as demonstrated below:
* <pre>
* public Foo(Bar bar) {
* this.bar = Objects.nonNull(bar);
* }
* </pre>
*
* @param obj the object reference to check for nullity
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T nonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@Link NullPointerException} if it is. This method
* is designed primarily for doing parameter validation in methods and
* constructors with multiple parameters, as demonstrated below:
* <pre>
* public Foo(Bar bar, Baz baz) {
* this.bar = Objects.nonNull(bar, "bar must not be null");
* this.baz = Objects.nonNull(baz, "baz must not be null");
* }
* </pre>
*
* @param obj the object reference to check for nullity
* @param message detail message to be used in the event that a {@code
* NullPointerException} is thrown
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T nonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}
They do a great job reducing the verbiage in validity-checking of arguments that must not be null.
- relates to
-
JDK-6797535 Add shared two argument static equals method to the platform
- Resolved
-
JDK-6891113 More methods for java.util.Objects: deepEquals, hash, toString with default
- Resolved
-
JDK-7012540 java.util.Objects.nonNull() incorrectly named
- Closed
-
JDK-8011800 Add java.util.Objects.requireNonNull(T, Supplier<String>)
- Closed