Use Objects#requireNonNull()

This commit is contained in:
Gary Gregory 2022-07-05 09:00:04 -04:00
parent a588f7e693
commit aae5a3522f
6 changed files with 26 additions and 31 deletions

View File

@ -26,6 +26,7 @@ import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.IntFunction;
@ -3794,14 +3795,10 @@ public class ArrayUtils {
* @since 3.4
*/
public static <T> boolean isSorted(final T[] array, final Comparator<T> comparator) {
if (comparator == null) {
throw new IllegalArgumentException("Comparator should not be null.");
}
Objects.requireNonNull(comparator, "comparator");
if (array == null || array.length < 2) {
return true;
}
T previous = array[0];
final int n = array.length;
for (int i = 1; i < n; i++) {

View File

@ -716,13 +716,12 @@ public class ExceptionUtils {
*
* @param causeFrames stack trace of a cause throwable
* @param wrapperFrames stack trace of a wrapper throwable
* @throws IllegalArgumentException if either argument is null
* @throws NullPointerException if either argument is null
* @since 2.0
*/
public static void removeCommonFrames(final List<String> causeFrames, final List<String> wrapperFrames) {
if (causeFrames == null || wrapperFrames == null) {
throw new IllegalArgumentException("The List must not be null");
}
Objects.requireNonNull(causeFrames, "causeFrames");
Objects.requireNonNull(wrapperFrames, "wrapperFrames");
int causeFrameIndex = causeFrames.size() - 1;
int wrapperFrameIndex = wrapperFrames.size() - 1;
while (causeFrameIndex >= 0 && wrapperFrameIndex >= 0) {

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Objects;
/**
* An API for translating text.
@ -74,30 +75,28 @@ public abstract class CharSequenceTranslator {
* tightly coupled with the abstract method of this class.
*
* @param input CharSequence that is being translated
* @param out Writer to translate the text to
* @param writer Writer to translate the text to
* @throws IOException if and only if the Writer produces an IOException
*/
public final void translate(final CharSequence input, final Writer out) throws IOException {
if (out == null) {
throw new IllegalArgumentException("The Writer must not be null");
}
public final void translate(final CharSequence input, final Writer writer) throws IOException {
Objects.requireNonNull(writer, "writer");
if (input == null) {
return;
}
int pos = 0;
final int len = input.length();
while (pos < len) {
final int consumed = translate(input, pos, out);
final int consumed = translate(input, pos, writer);
if (consumed == 0) {
// inlined implementation of Character.toChars(Character.codePointAt(input, pos))
// avoids allocating temp char arrays and duplicate checks
final char c1 = input.charAt(pos);
out.write(c1);
writer.write(c1);
pos++;
if (Character.isHighSurrogate(c1) && pos < len) {
final char c2 = input.charAt(pos);
if (Character.isLowSurrogate(c2)) {
out.write(c2);
writer.write(c2);
pos++;
}
}

View File

@ -1646,7 +1646,7 @@ public class ArrayUtilsTest {
@Test
public void testIsSortedNullComparator() {
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.isSorted(null, null));
assertThrows(NullPointerException.class, () -> ArrayUtils.isSorted(null, null));
}
@Test

View File

@ -55,8 +55,8 @@ public class StringEscapeUtilsTest {
@Test
public void testEscapeJava() throws IOException {
assertNull(StringEscapeUtils.escapeJava(null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_JAVA.translate(null, null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_JAVA.translate("", null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.ESCAPE_JAVA.translate(null, null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.ESCAPE_JAVA.translate("", null));
assertEscapeJava("empty string", "", "");
assertEscapeJava(FOO, FOO);
@ -112,8 +112,8 @@ public class StringEscapeUtilsTest {
@Test
public void testUnescapeJava() throws IOException {
assertNull(StringEscapeUtils.unescapeJava(null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_JAVA.translate(null, null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_JAVA.translate("", null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.UNESCAPE_JAVA.translate(null, null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.UNESCAPE_JAVA.translate("", null));
assertThrows(RuntimeException.class, () -> StringEscapeUtils.unescapeJava("\\u02-3"));
assertUnescapeJava("", "");
@ -152,8 +152,8 @@ public class StringEscapeUtilsTest {
@Test
public void testEscapeEcmaScript() {
assertNull(StringEscapeUtils.escapeEcmaScript(null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_ECMASCRIPT.translate(null, null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_ECMASCRIPT.translate("", null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.ESCAPE_ECMASCRIPT.translate(null, null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.ESCAPE_ECMASCRIPT.translate("", null));
assertEquals("He didn\\'t say, \\\"stop!\\\"", StringEscapeUtils.escapeEcmaScript("He didn't say, \"stop!\""));
assertEquals("document.getElementById(\\\"test\\\").value = \\'<script>alert(\\'aaa\\');<\\/script>\\';",
@ -163,8 +163,8 @@ public class StringEscapeUtilsTest {
@Test
public void testUnescapeEcmaScript() {
assertNull(StringEscapeUtils.escapeEcmaScript(null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate(null, null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate("", null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate(null, null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate("", null));
assertEquals("He didn't say, \"stop!\"", StringEscapeUtils.unescapeEcmaScript("He didn\\'t say, \\\"stop!\\\""));
assertEquals("document.getElementById(\"test\").value = '<script>alert('aaa');</script>';",
@ -546,8 +546,8 @@ public class StringEscapeUtilsTest {
@Test
public void testEscapeJson() {
assertNull(StringEscapeUtils.escapeJson(null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_JSON.translate(null, null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.ESCAPE_JSON.translate("", null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.ESCAPE_JSON.translate(null, null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.ESCAPE_JSON.translate("", null));
assertEquals("He didn't say, \\\"stop!\\\"", StringEscapeUtils.escapeJson("He didn't say, \"stop!\""));
@ -560,8 +560,8 @@ public class StringEscapeUtilsTest {
@Test
public void testUnescapeJson() {
assertNull(StringEscapeUtils.unescapeJson(null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_JSON.translate(null, null));
assertThrows(IllegalArgumentException.class, () -> StringEscapeUtils.UNESCAPE_JSON.translate("", null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.UNESCAPE_JSON.translate(null, null));
assertThrows(NullPointerException.class, () -> StringEscapeUtils.UNESCAPE_JSON.translate("", null));
assertEquals("He didn't say, \"stop!\"", StringEscapeUtils.unescapeJson("He didn't say, \\\"stop!\\\""));

View File

@ -666,7 +666,7 @@ public class ExceptionUtilsTest {
@Test
public void testRemoveCommonFrames_ListList() {
assertThrows(IllegalArgumentException.class, () -> ExceptionUtils.removeCommonFrames(null, null));
assertThrows(NullPointerException.class, () -> ExceptionUtils.removeCommonFrames(null, null));
}
@Test