Use Objects.requireNonNull() instead of custom check. Minor formatting.

This commit is contained in:
Gary Gregory 2019-12-25 23:00:43 -05:00
parent ff05749cdc
commit 3744263970
3 changed files with 16 additions and 29 deletions

View File

@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@ -433,10 +434,8 @@ public static void tryWithResources(FailableRunnable<? extends Throwable> pActio
errorHandler = pErrorHandler;
}
if (pResources != null) {
for (FailableRunnable<? extends Throwable> runnable : pResources) {
if (runnable == null) {
throw new NullPointerException("A resource action must not be null.");
}
for (FailableRunnable<? extends Throwable> failableRunnable : pResources) {
Objects.requireNonNull(failableRunnable, "runnable");
}
}
Throwable th = null;
@ -514,9 +513,8 @@ public static void tryWithResources(FailableRunnable<? extends Throwable> pActio
* @return Never returns anything, this method never terminates normally.
*/
public static RuntimeException rethrow(Throwable pThrowable) {
if (pThrowable == null) {
throw new NullPointerException("The Throwable must not be null.");
} else if (pThrowable instanceof RuntimeException) {
Objects.requireNonNull(pThrowable, "pThrowable");
if (pThrowable instanceof RuntimeException) {
throw (RuntimeException) pThrowable;
} else if (pThrowable instanceof Error) {
throw (Error) pThrowable;

View File

@ -19,6 +19,7 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
/**
@ -221,10 +222,7 @@ public static <T> T notNull(final T object) {
* @see #notNull(Object)
*/
public static <T> T notNull(final T object, final String message, final Object... values) {
if (object == null) {
throw new NullPointerException(String.format(message, values));
}
return object;
return Objects.requireNonNull(object, () -> String.format(message, values));
}
// notEmpty array
@ -247,9 +245,7 @@ public static <T> T notNull(final T object, final String message, final Object..
* @see #notEmpty(Object[])
*/
public static <T> T[] notEmpty(final T[] array, final String message, final Object... values) {
if (array == null) {
throw new NullPointerException(String.format(message, values));
}
Objects.requireNonNull(array, () -> String.format(message, values));
if (array.length == 0) {
throw new IllegalArgumentException(String.format(message, values));
}
@ -296,9 +292,7 @@ public static <T> T[] notEmpty(final T[] array) {
* @see #notEmpty(Object[])
*/
public static <T extends Collection<?>> T notEmpty(final T collection, final String message, final Object... values) {
if (collection == null) {
throw new NullPointerException(String.format(message, values));
}
Objects.requireNonNull(collection, () -> String.format(message, values));
if (collection.isEmpty()) {
throw new IllegalArgumentException(String.format(message, values));
}
@ -345,9 +339,7 @@ public static <T extends Collection<?>> T notEmpty(final T collection) {
* @see #notEmpty(Object[])
*/
public static <T extends Map<?, ?>> T notEmpty(final T map, final String message, final Object... values) {
if (map == null) {
throw new NullPointerException(String.format(message, values));
}
Objects.requireNonNull(map, () -> String.format(message, values));
if (map.isEmpty()) {
throw new IllegalArgumentException(String.format(message, values));
}
@ -394,9 +386,7 @@ public static <T extends Collection<?>> T notEmpty(final T collection) {
* @see #notEmpty(CharSequence)
*/
public static <T extends CharSequence> T notEmpty(final T chars, final String message, final Object... values) {
if (chars == null) {
throw new NullPointerException(String.format(message, values));
}
Objects.requireNonNull(chars, () -> String.format(message, values));
if (chars.length() == 0) {
throw new IllegalArgumentException(String.format(message, values));
}
@ -447,9 +437,7 @@ public static <T extends CharSequence> T notEmpty(final T chars) {
* @since 3.0
*/
public static <T extends CharSequence> T notBlank(final T chars, final String message, final Object... values) {
if (chars == null) {
throw new NullPointerException(String.format(message, values));
}
Objects.requireNonNull(chars, () -> String.format(message, values));
if (StringUtils.isBlank(chars)) {
throw new IllegalArgumentException(String.format(message, values));
}

View File

@ -21,6 +21,7 @@
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Comparator;
import java.util.Objects;
import org.apache.commons.lang3.ArrayUtils;
@ -282,9 +283,9 @@ public static int reflectionCompare(
if (lhs == rhs) {
return 0;
}
if (lhs == null || rhs == null) {
throw new NullPointerException();
}
Objects.requireNonNull(lhs, "lhs");
Objects.requireNonNull(rhs, "rhs");
Class<?> lhsClazz = lhs.getClass();
if (!lhsClazz.isInstance(rhs)) {
throw new ClassCastException();