提交 0e365c6e 编写于 作者: M mduigou

8013712: Add Objects.nonNull and Objects.isNull

Reviewed-by: mchung, darcy
上级 11547923
......@@ -226,4 +226,40 @@ public final class Objects {
throw new NullPointerException(message);
return obj;
}
/**
* Returns {@code true} if the provided reference is {@code null} otherwise
* returns {@code false}.
*
* @apiNote This method exists to be used as a
* {@link java.util.function.Predicate}, {@code filter(Objects::isNull)}
*
* @param obj a reference to be checked against {@code null}
* @return {@code true} if the provided reference is {@code null} otherwise
* {@code false}
*
* @see java.util.function.Predicate
* @since 1.8
*/
public static boolean isNull(Object obj) {
return obj == null;
}
/**
* Returns {@code true} if the provided reference is non-{@code null}
* otherwise returns {@code false}.
*
* @apiNote This method exists to be used as a
* {@link java.util.function.Predicate}, {@code filter(Objects::nonNull)}
*
* @param obj a reference to be checked against {@code null}
* @return {@code true} if the provided reference is non-{@code null}
* otherwise {@code false}
*
* @see java.util.function.Predicate
* @since 1.8
*/
public static boolean nonNull(Object obj) {
return obj != null;
}
}
......@@ -40,6 +40,8 @@ public class BasicObjectsTest {
errors += testToString();
errors += testToString2();
errors += testCompare();
errors += testRequireNonNull();
errors += testIsNull();
errors += testNonNull();
if (errors > 0 )
throw new RuntimeException();
......@@ -158,7 +160,7 @@ public class BasicObjectsTest {
return errors;
}
private static int testNonNull() {
private static int testRequireNonNull() {
int errors = 0;
String s;
......@@ -206,4 +208,22 @@ public class BasicObjectsTest {
}
return errors;
}
private static int testIsNull() {
int errors = 0;
errors += Objects.isNull(null) ? 0 : 1;
errors += Objects.isNull(Objects.class) ? 1 : 0;
return errors;
}
private static int testNonNull() {
int errors = 0;
errors += Objects.nonNull(null) ? 1 : 0;
errors += Objects.nonNull(Objects.class) ? 0 : 1;
return errors;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册