Skip to content

Commit 3045132

Browse files
author
Gary Gregory
committed
Use Objects.requireNonNull()
Update Javadoc for existing calls.
1 parent 7ca5c2a commit 3045132

25 files changed

Lines changed: 176 additions & 209 deletions

src/main/java/org/apache/commons/io/ByteOrderMark.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.Serializable;
2020
import java.util.Locale;
21+
import java.util.Objects;
2122

2223
/**
2324
* Byte Order Mark (BOM) representation - see {@link org.apache.commons.io.input.BOMInputStream}.
@@ -67,28 +68,28 @@ public class ByteOrderMark implements Serializable {
6768
private final int[] bytes;
6869

6970
/**
70-
* Constructs a new BOM.
71+
* Constructs a new instance.
7172
*
7273
* @param charsetName The name of the charset the BOM represents
7374
* @param bytes The BOM's bytes
74-
* @throws IllegalArgumentException if the charsetName is null or
75-
* zero length
76-
* @throws IllegalArgumentException if the bytes are null or zero
77-
* length
75+
* @throws IllegalArgumentException if the charsetName is zero length
76+
* @throws IllegalArgumentException if the bytes are zero length
7877
*/
7978
public ByteOrderMark(final String charsetName, final int... bytes) {
80-
if (charsetName == null || charsetName.isEmpty()) {
79+
Objects.requireNonNull(charsetName, "charsetName");
80+
Objects.requireNonNull(bytes, "bytes");
81+
if (charsetName.isEmpty()) {
8182
throw new IllegalArgumentException("No charsetName specified");
8283
}
83-
if (bytes == null || bytes.length == 0) {
84+
if (bytes.length == 0) {
8485
throw new IllegalArgumentException("No bytes specified");
8586
}
8687
this.charsetName = charsetName;
8788
this.bytes = bytes.clone();
8889
}
8990

9091
/**
91-
* Indicates if this BOM's bytes equals another.
92+
* Indicates if this instance's bytes equals another.
9293
*
9394
* @param obj The object to compare to
9495
* @return true if the bom's bytes are equal, otherwise
@@ -99,7 +100,7 @@ public boolean equals(final Object obj) {
99100
if (!(obj instanceof ByteOrderMark)) {
100101
return false;
101102
}
102-
final ByteOrderMark bom = (ByteOrderMark)obj;
103+
final ByteOrderMark bom = (ByteOrderMark) obj;
103104
if (bytes.length != bom.length()) {
104105
return false;
105106
}
@@ -129,7 +130,7 @@ public int get(final int pos) {
129130
public byte[] getBytes() {
130131
final byte[] copy = IOUtils.byteArray(bytes.length);
131132
for (int i = 0; i < bytes.length; i++) {
132-
copy[i] = (byte)bytes[i];
133+
copy[i] = (byte) bytes[i];
133134
}
134135
return copy;
135136
}

src/main/java/org/apache/commons/io/FileSystemUtils.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Arrays;
2828
import java.util.List;
2929
import java.util.Locale;
30+
import java.util.Objects;
3031
import java.util.StringTokenizer;
3132

3233
/**
@@ -262,25 +263,22 @@ public FileSystemUtils() {
262263
* is zero or less
263264
* @return the amount of free drive space on the drive or volume
264265
* @throws IllegalArgumentException if the path is invalid
265-
* @throws IllegalStateException if an error occurred in initialisation
266+
* @throws IllegalStateException if an error occurred in initialization
266267
* @throws IOException if an error occurs when finding the free space
267268
*/
268269
long freeSpaceOS(final String path, final int os, final boolean kb, final Duration timeout) throws IOException {
269-
if (path == null) {
270-
throw new IllegalArgumentException("Path must not be null");
271-
}
270+
Objects.requireNonNull(path, "path");
272271
switch (os) {
273-
case WINDOWS:
274-
return kb ? freeSpaceWindows(path, timeout) / FileUtils.ONE_KB : freeSpaceWindows(path, timeout);
275-
case UNIX:
276-
return freeSpaceUnix(path, kb, false, timeout);
277-
case POSIX_UNIX:
278-
return freeSpaceUnix(path, kb, true, timeout);
279-
case OTHER:
280-
throw new IllegalStateException("Unsupported operating system");
281-
default:
282-
throw new IllegalStateException(
283-
"Exception caught when determining operating system");
272+
case WINDOWS:
273+
return kb ? freeSpaceWindows(path, timeout) / FileUtils.ONE_KB : freeSpaceWindows(path, timeout);
274+
case UNIX:
275+
return freeSpaceUnix(path, kb, false, timeout);
276+
case POSIX_UNIX:
277+
return freeSpaceUnix(path, kb, true, timeout);
278+
case OTHER:
279+
throw new IllegalStateException("Unsupported operating system");
280+
default:
281+
throw new IllegalStateException("Exception caught when determining operating system");
284282
}
285283
}
286284

src/main/java/org/apache/commons/io/HexDump.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.OutputStream;
2121
import java.nio.charset.Charset;
22+
import java.util.Objects;
2223

2324
/**
2425
* Dumps data in hexadecimal format.
@@ -74,22 +75,17 @@ public class HexDump {
7475
* the data to stream
7576
* @throws ArrayIndexOutOfBoundsException if the index is
7677
* outside the data array's bounds
77-
* @throws IllegalArgumentException if the output stream is null
78+
* @throws NullPointerException if the output stream is null
7879
*/
79-
8080
public static void dump(final byte[] data, final long offset,
8181
final OutputStream stream, final int index)
82-
throws IOException, ArrayIndexOutOfBoundsException,
83-
IllegalArgumentException {
84-
82+
throws IOException, ArrayIndexOutOfBoundsException {
83+
Objects.requireNonNull(stream, "stream");
8584
if (index < 0 || index >= data.length) {
8685
throw new ArrayIndexOutOfBoundsException(
8786
"illegal index: " + index + " into array of length "
8887
+ data.length);
8988
}
90-
if (stream == null) {
91-
throw new IllegalArgumentException("cannot write to nullstream");
92-
}
9389
long display_offset = offset + index;
9490
final StringBuilder buffer = new StringBuilder(74);
9591

src/main/java/org/apache/commons/io/comparator/ReverseFileComparator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.io.Serializable;
2121
import java.util.Comparator;
22+
import java.util.Objects;
2223

2324
/**
2425
* Reverses the result of comparing two {@link File} objects using the delegate {@link Comparator}.
@@ -36,10 +37,7 @@ class ReverseFileComparator extends AbstractFileComparator implements Serializab
3637
* @param delegate The comparator to delegate to.
3738
*/
3839
public ReverseFileComparator(final Comparator<File> delegate) {
39-
if (delegate == null) {
40-
throw new IllegalArgumentException("Delegate comparator is missing");
41-
}
42-
this.delegate = delegate;
40+
this.delegate = Objects.requireNonNull(delegate, "delegate");
4341
}
4442

4543
/**

src/main/java/org/apache/commons/io/filefilter/AbstractFileFilter.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@
3939
*/
4040
public abstract class AbstractFileFilter implements IOFileFilter, PathVisitor {
4141

42-
static <T> T requireNonNull(final T obj, final String message) {
43-
if (obj == null) {
44-
throw new IllegalArgumentException(message);
45-
}
46-
return obj;
47-
}
48-
4942
static FileVisitResult toDefaultFileVisitResult(final boolean accept) {
5043
return accept ? FileVisitResult.CONTINUE : FileVisitResult.TERMINATE;
5144
}

src/main/java/org/apache/commons/io/filefilter/DelegateFileFilter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.FileFilter;
2121
import java.io.FilenameFilter;
2222
import java.io.Serializable;
23+
import java.util.Objects;
2324

2425
/**
2526
* This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.
@@ -42,7 +43,7 @@ public class DelegateFileFilter extends AbstractFileFilter implements Serializab
4243
* @param fileFilter the filter to decorate
4344
*/
4445
public DelegateFileFilter(final FileFilter fileFilter) {
45-
requireNonNull(fileFilter, "filter");
46+
Objects.requireNonNull(fileFilter, "filter");
4647
this.fileFilter = fileFilter;
4748
this.filenameFilter = null;
4849
}
@@ -53,7 +54,7 @@ public DelegateFileFilter(final FileFilter fileFilter) {
5354
* @param filenameFilter the filter to decorate
5455
*/
5556
public DelegateFileFilter(final FilenameFilter filenameFilter) {
56-
requireNonNull(filenameFilter, "filter");
57+
Objects.requireNonNull(filenameFilter, "filter");
5758
this.filenameFilter = filenameFilter;
5859
this.fileFilter = null;
5960
}

src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,13 @@ public static IOFileFilter fileFileFilter() {
234234
*
235235
* @return a subset of {@code files} that is accepted by the
236236
* file filter.
237-
* @throws IllegalArgumentException if the filter is {@code null}
237+
* @throws NullPointerException if the filter is {@code null}
238238
* or {@code files} contains a {@code null} value.
239239
*
240240
* @since 2.0
241241
*/
242242
public static File[] filter(final IOFileFilter filter, final File... files) {
243-
if (filter == null) {
244-
throw new IllegalArgumentException("file filter is null");
245-
}
243+
Objects.requireNonNull(filter, "filter");
246244
if (files == null) {
247245
return FileUtils.EMPTY_FILE_ARRAY;
248246
}
@@ -291,15 +289,12 @@ public static File[] filter(final IOFileFilter filter, final Iterable<File> file
291289
* @param <R> the return type.
292290
* @param <A> the mutable accumulation type of the reduction operation (often hidden as an implementation detail)
293291
* @return a subset of files from the stream that is accepted by the filter.
294-
* @throws IllegalArgumentException if the filter is {@code null}.
292+
* @throws NullPointerException if the filter is {@code null}.
295293
*/
296294
private static <R, A> R filterFiles(final IOFileFilter filter, final Stream<File> stream,
297295
final Collector<? super File, A, R> collector) {
298-
//Objects.requireNonNull(filter, "filter");
296+
Objects.requireNonNull(filter, "filter");
299297
Objects.requireNonNull(collector, "collector");
300-
if (filter == null) {
301-
throw new IllegalArgumentException("file filter is null");
302-
}
303298
if (stream == null) {
304299
return Stream.<File>empty().collect(collector);
305300
}
@@ -723,20 +718,15 @@ public static IOFileFilter suffixFileFilter(final String suffix, final IOCase io
723718
*
724719
* @param filters The file filters
725720
* @return The list of file filters
726-
* @throws IllegalArgumentException if the filters are null or contain a
721+
* @throws NullPointerException if the filters are null or contain a
727722
* null value.
728723
* @since 2.0
729724
*/
730725
public static List<IOFileFilter> toList(final IOFileFilter... filters) {
731-
if (filters == null) {
732-
throw new IllegalArgumentException("The filters must not be null");
733-
}
726+
Objects.requireNonNull(filters, "filters");
734727
final List<IOFileFilter> list = new ArrayList<>(filters.length);
735728
for (int i = 0; i < filters.length; i++) {
736-
if (filters[i] == null) {
737-
throw new IllegalArgumentException("The filter[" + i + "] is null");
738-
}
739-
list.add(filters[i]);
729+
list.add(Objects.requireNonNull(filters[i], "filters[i]"));
740730
}
741731
return list;
742732
}

src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.file.Path;
2929
import java.nio.file.attribute.BasicFileAttributes;
3030
import java.util.Arrays;
31+
import java.util.Objects;
3132

3233
import org.apache.commons.io.IOUtils;
3334
import org.apache.commons.io.RandomAccessFileMode;
@@ -159,12 +160,12 @@ public MagicNumberFileFilter(final byte[] magicNumber) {
159160
* @param magicNumbers the magic number to look for in the file.
160161
* @param offset the byte offset in the file to start comparing bytes.
161162
*
162-
* @throws IllegalArgumentException if {@code magicNumber} is
163-
* {@code null}, or contains no bytes, or {@code offset}
163+
* @throws IllegalArgumentException if {@code magicNumber}
164+
* contains no bytes, or {@code offset}
164165
* is a negative number.
165166
*/
166167
public MagicNumberFileFilter(final byte[] magicNumbers, final long offset) {
167-
requireNonNull(magicNumbers, "magicNumbers");
168+
Objects.requireNonNull(magicNumbers, "magicNumbers");
168169
if (magicNumbers.length == 0) {
169170
throw new IllegalArgumentException("The magic number must contain at least one byte");
170171
}
@@ -218,11 +219,11 @@ public MagicNumberFileFilter(final String magicNumber) {
218219
* @param offset the byte offset in the file to start comparing bytes.
219220
*
220221
* @throws IllegalArgumentException if {@code magicNumber} is
221-
* {@code null} or the empty String, or {@code offset} is
222+
* the empty String, or {@code offset} is
222223
* a negative number.
223224
*/
224225
public MagicNumberFileFilter(final String magicNumber, final long offset) {
225-
requireNonNull(magicNumber, "magicNumber");
226+
Objects.requireNonNull(magicNumber, "magicNumber");
226227
if (magicNumber.isEmpty()) {
227228
throw new IllegalArgumentException("The magic number must contain at least one byte");
228229
}

src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public NameFileFilter(final List<String> names) {
9090
*
9191
* @param names the names to allow, must not be null
9292
* @param ioCase how to handle case sensitivity, null means case-sensitive
93-
* @throws IllegalArgumentException if the name list is null
93+
* @throws NullPointerException if the name list is null
9494
* @throws ClassCastException if the list does not contain Strings
9595
*/
9696
public NameFileFilter(final List<String> names, final IOCase ioCase) {
97-
requireNonNull(names, "names");
97+
Objects.requireNonNull(names, "names");
9898
this.names = names.toArray(EMPTY_STRING_ARRAY);
9999
this.ioCase = toIOCase(ioCase);
100100
}
@@ -128,12 +128,10 @@ public NameFileFilter(final String... names) {
128128
*
129129
* @param name the name to allow, must not be null
130130
* @param ioCase how to handle case sensitivity, null means case-sensitive
131-
* @throws IllegalArgumentException if the name is null
131+
* @throws NullPointerException if the name is null
132132
*/
133133
public NameFileFilter(final String name, final IOCase ioCase) {
134-
if (name == null) {
135-
throw new IllegalArgumentException("The wildcard must not be null");
136-
}
134+
Objects.requireNonNull(name, "name");
137135
this.names = new String[] {name};
138136
this.ioCase = toIOCase(ioCase);
139137
}
@@ -143,12 +141,10 @@ public NameFileFilter(final String name, final IOCase ioCase) {
143141
*
144142
* @param names the names to allow, must not be null
145143
* @param ioCase how to handle case sensitivity, null means case-sensitive
146-
* @throws IllegalArgumentException if the names array is null
144+
* @throws NullPointerException if the names array is null
147145
*/
148146
public NameFileFilter(final String[] names, final IOCase ioCase) {
149-
if (names == null) {
150-
throw new IllegalArgumentException("The array of names must not be null");
151-
}
147+
Objects.requireNonNull(names, "names");
152148
this.names = names.clone();
153149
this.ioCase = toIOCase(ioCase);
154150
}

src/main/java/org/apache/commons/io/filefilter/NotFileFilter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.FileVisitResult;
2222
import java.nio.file.Path;
2323
import java.nio.file.attribute.BasicFileAttributes;
24+
import java.util.Objects;
2425

2526
/**
2627
* This filter produces a logical NOT of the filters specified.
@@ -39,10 +40,10 @@ public class NotFileFilter extends AbstractFileFilter implements Serializable {
3940
* Constructs a new file filter that NOTs the result of another filter.
4041
*
4142
* @param filter the filter, must not be null
42-
* @throws IllegalArgumentException if the filter is null
43+
* @throws NullPointerException if the filter is null
4344
*/
4445
public NotFileFilter(final IOFileFilter filter) {
45-
requireNonNull(filter, "filter");
46+
Objects.requireNonNull(filter, "filter");
4647
this.filter = filter;
4748
}
4849

0 commit comments

Comments
 (0)