Skip to content

Commit 8db20da

Browse files
committed
Use Objects.requireNonNull() instead of custom check.
1 parent 72d53cf commit 8db20da

11 files changed

Lines changed: 95 additions & 139 deletions

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

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

2425
import org.apache.commons.io.filefilter.FileFilterUtils;
2526
import org.apache.commons.io.filefilter.IOFileFilter;
@@ -348,9 +349,7 @@ protected DirectoryWalker(IOFileFilter directoryFilter, IOFileFilter fileFilter,
348349
* @throws IOException if an I/O Error occurs
349350
*/
350351
protected final void walk(final File startDirectory, final Collection<T> results) throws IOException {
351-
if (startDirectory == null) {
352-
throw new NullPointerException("Start Directory is null");
353-
}
352+
Objects.requireNonNull(startDirectory, "startDirectory");
354353
try {
355354
handleStart(startDirectory, results);
356355
walk(startDirectory, 0, results);

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collections;
2525
import java.util.HashSet;
2626
import java.util.List;
27+
import java.util.Objects;
2728

2829
/**
2930
* Keeps track of files awaiting deletion, and deletes them when an associated
@@ -90,9 +91,7 @@ public void track(final File file, final Object marker) {
9091
* @throws NullPointerException if the file is null
9192
*/
9293
public void track(final File file, final Object marker, final FileDeleteStrategy deleteStrategy) {
93-
if (file == null) {
94-
throw new NullPointerException("The file must not be null");
95-
}
94+
Objects.requireNonNull(file, "file");
9695
addTracker(file.getPath(), marker, deleteStrategy);
9796
}
9897

@@ -120,9 +119,7 @@ public void track(final String path, final Object marker) {
120119
* @throws NullPointerException if the path is null
121120
*/
122121
public void track(final String path, final Object marker, final FileDeleteStrategy deleteStrategy) {
123-
if (path == null) {
124-
throw new NullPointerException("The path must not be null");
125-
}
122+
Objects.requireNonNull(path, "path");
126123
addTracker(path, marker, deleteStrategy);
127124
}
128125

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

Lines changed: 53 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Date;
4242
import java.util.Iterator;
4343
import java.util.List;
44+
import java.util.Objects;
4445
import java.util.zip.CRC32;
4546
import java.util.zip.CheckedInputStream;
4647
import java.util.zip.Checksum;
@@ -262,19 +263,15 @@ private static void checkEqualSizes(final File srcFile, final File destFile, fin
262263
/**
263264
* Checks requirements for file copy.
264265
*
265-
* @param src the source file
266-
* @param dest the destination
266+
* @param source the source file
267+
* @param destination the destination
267268
* @throws FileNotFoundException if the destination does not exist
268269
*/
269-
private static void checkFileRequirements(final File src, final File dest) throws FileNotFoundException {
270-
if (src == null) {
271-
throw new NullPointerException("Source must not be null");
272-
}
273-
if (dest == null) {
274-
throw new NullPointerException("Destination must not be null");
275-
}
276-
if (!src.exists()) {
277-
throw new FileNotFoundException("Source '" + src + "' does not exist");
270+
private static void checkFileRequirements(final File source, final File destination) throws FileNotFoundException {
271+
Objects.requireNonNull(source, "source");
272+
Objects.requireNonNull(destination, "target");
273+
if (!source.exists()) {
274+
throw new FileNotFoundException("Source '" + source + "' does not exist");
278275
}
279276
}
280277

@@ -699,29 +696,25 @@ public static void copyDirectory(final File srcDir, final File destDir,
699696
* If the modification operation fails, no indication is provided.
700697
* </p>
701698
*
702-
* @param srcDir an existing directory to copy, must not be {@code null}
703-
* @param destDir the directory to place the copy in, must not be {@code null}
699+
* @param sourceDir an existing directory to copy, must not be {@code null}
700+
* @param destinationDir the directory to place the copy in, must not be {@code null}
704701
*
705702
* @throws NullPointerException if source or destination is {@code null}
706703
* @throws IllegalArgumentException if {@code srcDir} or {@code destDir} is not a directory
707704
* @throws IOException if source or destination is invalid
708705
* @throws IOException if an IO error occurs during copying
709706
* @since 1.2
710707
*/
711-
public static void copyDirectoryToDirectory(final File srcDir, final File destDir) throws IOException {
712-
if (srcDir == null) {
713-
throw new NullPointerException("Source must not be null");
714-
}
715-
if (srcDir.exists() && srcDir.isDirectory() == false) {
716-
throw new IllegalArgumentException("Source '" + srcDir + "' is not a directory");
708+
public static void copyDirectoryToDirectory(final File sourceDir, final File destinationDir) throws IOException {
709+
Objects.requireNonNull(sourceDir, "sourceDir");
710+
if (sourceDir.exists() && sourceDir.isDirectory() == false) {
711+
throw new IllegalArgumentException("Source '" + sourceDir + "' is not a directory");
717712
}
718-
if (destDir == null) {
719-
throw new NullPointerException("Destination must not be null");
713+
Objects.requireNonNull(destinationDir, "destinationDir");
714+
if (destinationDir.exists() && destinationDir.isDirectory() == false) {
715+
throw new IllegalArgumentException("Destination '" + destinationDir + "' is not a directory");
720716
}
721-
if (destDir.exists() && destDir.isDirectory() == false) {
722-
throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
723-
}
724-
copyDirectory(srcDir, new File(destDir, srcDir.getName()), true);
717+
copyDirectory(sourceDir, new File(destinationDir, sourceDir.getName()), true);
725718
}
726719

727720
/**
@@ -867,8 +860,8 @@ public static void copyFileToDirectory(final File srcFile, final File destDir) t
867860
* If the modification operation fails, no indication is provided.
868861
* </p>
869862
*
870-
* @param srcFile an existing file to copy, must not be {@code null}
871-
* @param destDir the directory to place the copy in, must not be {@code null}
863+
* @param sourceFile an existing file to copy, must not be {@code null}
864+
* @param destinationDir the directory to place the copy in, must not be {@code null}
872865
* @param preserveFileDate true if the file date of the copy
873866
* should be the same as the original
874867
*
@@ -880,16 +873,14 @@ public static void copyFileToDirectory(final File srcFile, final File destDir) t
880873
* @see #copyFile(File, File, boolean)
881874
* @since 1.3
882875
*/
883-
public static void copyFileToDirectory(final File srcFile, final File destDir, final boolean preserveFileDate)
876+
public static void copyFileToDirectory(final File sourceFile, final File destinationDir, final boolean preserveFileDate)
884877
throws IOException {
885-
if (destDir == null) {
886-
throw new NullPointerException("Destination must not be null");
887-
}
888-
if (destDir.exists() && destDir.isDirectory() == false) {
889-
throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
878+
Objects.requireNonNull(destinationDir, "destinationDir");
879+
if (destinationDir.exists() && destinationDir.isDirectory() == false) {
880+
throw new IllegalArgumentException("Destination '" + destinationDir + "' is not a directory");
890881
}
891-
final File destFile = new File(destDir, srcFile.getName());
892-
copyFile(srcFile, destFile, preserveFileDate);
882+
final File destFile = new File(destinationDir, sourceFile.getName());
883+
copyFile(sourceFile, destFile, preserveFileDate);
893884
}
894885

895886
/**
@@ -933,8 +924,8 @@ public static void copyInputStreamToFile(final InputStream source, final File de
933924
* If the modification operation fails, no indication is provided.
934925
* </p>
935926
*
936-
* @param src an existing file or directory to copy, must not be {@code null}
937-
* @param destDir the directory to place the copy in, must not be {@code null}
927+
* @param sourceFile an existing file or directory to copy, must not be {@code null}
928+
* @param destinationDir the directory to place the copy in, must not be {@code null}
938929
*
939930
* @throws NullPointerException if source or destination is {@code null}
940931
* @throws IOException if source or destination is invalid
@@ -943,16 +934,14 @@ public static void copyInputStreamToFile(final InputStream source, final File de
943934
* @see #copyFileToDirectory(File, File)
944935
* @since 2.6
945936
*/
946-
public static void copyToDirectory(final File src, final File destDir) throws IOException {
947-
if (src == null) {
948-
throw new NullPointerException("Source must not be null");
949-
}
950-
if (src.isFile()) {
951-
copyFileToDirectory(src, destDir);
952-
} else if (src.isDirectory()) {
953-
copyDirectoryToDirectory(src, destDir);
937+
public static void copyToDirectory(final File sourceFile, final File destinationDir) throws IOException {
938+
Objects.requireNonNull(sourceFile, "sourceFile");
939+
if (sourceFile.isFile()) {
940+
copyFileToDirectory(sourceFile, destinationDir);
941+
} else if (sourceFile.isDirectory()) {
942+
copyDirectoryToDirectory(sourceFile, destinationDir);
954943
} else {
955-
throw new IOException("The source " + src + " does not exist");
944+
throw new IOException("The source " + sourceFile + " does not exist");
956945
}
957946
}
958947

@@ -972,21 +961,19 @@ public static void copyToDirectory(final File src, final File destDir) throws IO
972961
* If the modification operation fails, no indication is provided.
973962
* </p>
974963
*
975-
* @param srcs a existing files to copy, must not be {@code null}
976-
* @param destDir the directory to place the copy in, must not be {@code null}
964+
* @param sourceIterable a existing files to copy, must not be {@code null}
965+
* @param destinationDir the directory to place the copy in, must not be {@code null}
977966
*
978967
* @throws NullPointerException if source or destination is null
979968
* @throws IOException if source or destination is invalid
980969
* @throws IOException if an IO error occurs during copying
981970
* @see #copyFileToDirectory(File, File)
982971
* @since 2.6
983972
*/
984-
public static void copyToDirectory(final Iterable<File> srcs, final File destDir) throws IOException {
985-
if (srcs == null) {
986-
throw new NullPointerException("Sources must not be null");
987-
}
988-
for (final File src : srcs) {
989-
copyFileToDirectory(src, destDir);
973+
public static void copyToDirectory(final Iterable<File> sourceIterable, final File destinationDir) throws IOException {
974+
Objects.requireNonNull(sourceIterable, "sourceIterable");
975+
for (final File src : sourceIterable) {
976+
copyFileToDirectory(src, destinationDir);
990977
}
991978
}
992979

@@ -1430,12 +1417,8 @@ public static void forceMkdirParent(final File file) throws IOException {
14301417
* @since 2.1
14311418
*/
14321419
public static File getFile(final File directory, final String... names) {
1433-
if (directory == null) {
1434-
throw new NullPointerException("directory must not be null");
1435-
}
1436-
if (names == null) {
1437-
throw new NullPointerException("names must not be null");
1438-
}
1420+
Objects.requireNonNull(directory, "directory");
1421+
Objects.requireNonNull(names, "names");
14391422
File file = directory;
14401423
for (final String name : names) {
14411424
file = new File(file, name);
@@ -1451,9 +1434,7 @@ public static File getFile(final File directory, final String... names) {
14511434
* @since 2.1
14521435
*/
14531436
public static File getFile(final String... names) {
1454-
if (names == null) {
1455-
throw new NullPointerException("names must not be null");
1456-
}
1437+
Objects.requireNonNull(names, "names");
14571438
File file = null;
14581439
for (final String name : names) {
14591440
if (file == null) {
@@ -1726,9 +1707,7 @@ public static boolean isFileOlder(final File file, final long timeMillis) {
17261707
* @since 2.0
17271708
*/
17281709
public static boolean isSymlink(final File file) {
1729-
if (file == null) {
1730-
throw new NullPointerException("File must not be null");
1731-
}
1710+
Objects.requireNonNull(file, "file");
17321711
return Files.isSymbolicLink(file.toPath());
17331712
}
17341713

@@ -2645,9 +2624,7 @@ private static void validateListFilesParameters(final File directory, final IOFi
26452624
if (!directory.isDirectory()) {
26462625
throw new IllegalArgumentException("Parameter 'directory' is not a directory: " + directory);
26472626
}
2648-
if (fileFilter == null) {
2649-
throw new NullPointerException("Parameter 'fileFilter' is null");
2650-
}
2627+
Objects.requireNonNull(fileFilter, "fileFilter");
26512628
}
26522629

26532630
/**
@@ -2658,19 +2635,15 @@ private static void validateListFilesParameters(final File directory, final IOFi
26582635
* <li>Throws {@link FileNotFoundException} if {@code src} does not exist</li>
26592636
* </ul>
26602637
*
2661-
* @param src the file or directory to be moved
2662-
* @param dest the destination file or directory
2638+
* @param source the file or directory to be moved
2639+
* @param destination the destination file or directory
26632640
* @throws FileNotFoundException if {@code src} file does not exist
26642641
*/
2665-
private static void validateMoveParameters(final File src, final File dest) throws FileNotFoundException {
2666-
if (src == null) {
2667-
throw new NullPointerException("Source must not be null");
2668-
}
2669-
if (dest == null) {
2670-
throw new NullPointerException("Destination must not be null");
2671-
}
2672-
if (!src.exists()) {
2673-
throw new FileNotFoundException("Source '" + src + "' does not exist");
2642+
private static void validateMoveParameters(final File source, final File destination) throws FileNotFoundException {
2643+
Objects.requireNonNull(source, "source");
2644+
Objects.requireNonNull(destination, "destination");
2645+
if (!source.exists()) {
2646+
throw new FileNotFoundException("Source '" + source + "' does not exist");
26742647
}
26752648
}
26762649

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collection;
2525
import java.util.Deque;
2626
import java.util.List;
27+
import java.util.Objects;
2728
import java.util.regex.Matcher;
2829
import java.util.regex.Pattern;
2930

@@ -1210,10 +1211,8 @@ public static boolean equals(
12101211
if (normalized) {
12111212
fileName1 = normalize(fileName1);
12121213
fileName2 = normalize(fileName2);
1213-
if (fileName1 == null || fileName2 == null) {
1214-
throw new NullPointerException(
1215-
"Error normalizing one or both of the file names");
1216-
}
1214+
Objects.requireNonNull(fileName1, "Error normalizing one or both of the file names");
1215+
Objects.requireNonNull(fileName2, "Error normalizing one or both of the file names");
12171216
}
12181217
if (caseSensitivity == null) {
12191218
caseSensitivity = IOCase.SENSITIVE;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.commons.io;
1818

19+
import java.util.Objects;
20+
1921
/**
2022
* Enumeration of IO case sensitivity.
2123
* <p>
@@ -139,9 +141,8 @@ public boolean isCaseSensitive() {
139141
* @throws NullPointerException if either string is null
140142
*/
141143
public int checkCompareTo(final String str1, final String str2) {
142-
if (str1 == null || str2 == null) {
143-
throw new NullPointerException("The strings must not be null");
144-
}
144+
Objects.requireNonNull(str1, "str1");
145+
Objects.requireNonNull(str2, "str2");
145146
return sensitive ? str1.compareTo(str2) : str1.compareToIgnoreCase(str2);
146147
}
147148

@@ -157,9 +158,8 @@ public int checkCompareTo(final String str1, final String str2) {
157158
* @throws NullPointerException if either string is null
158159
*/
159160
public boolean checkEquals(final String str1, final String str2) {
160-
if (str1 == null || str2 == null) {
161-
throw new NullPointerException("The strings must not be null");
162-
}
161+
Objects.requireNonNull(str1, "str1");
162+
Objects.requireNonNull(str2, "str2");
163163
return sensitive ? str1.equals(str2) : str1.equalsIgnoreCase(str2);
164164
}
165165

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,8 @@ public class IOUtils {
188188
*/
189189
public static BufferedInputStream buffer(final InputStream inputStream) {
190190
// reject null early on rather than waiting for IO operation to fail
191-
if (inputStream == null) { // not checked by BufferedInputStream
192-
throw new NullPointerException();
193-
}
191+
// not checked by BufferedInputStream
192+
Objects.requireNonNull(inputStream, "inputStream");
194193
return inputStream instanceof BufferedInputStream ?
195194
(BufferedInputStream) inputStream : new BufferedInputStream(inputStream);
196195
}
@@ -207,9 +206,8 @@ public static BufferedInputStream buffer(final InputStream inputStream) {
207206
*/
208207
public static BufferedInputStream buffer(final InputStream inputStream, final int size) {
209208
// reject null early on rather than waiting for IO operation to fail
210-
if (inputStream == null) { // not checked by BufferedInputStream
211-
throw new NullPointerException();
212-
}
209+
// not checked by BufferedInputStream
210+
Objects.requireNonNull(inputStream, "inputStream");
213211
return inputStream instanceof BufferedInputStream ?
214212
(BufferedInputStream) inputStream : new BufferedInputStream(inputStream, size);
215213
}
@@ -225,9 +223,8 @@ public static BufferedInputStream buffer(final InputStream inputStream, final in
225223
*/
226224
public static BufferedOutputStream buffer(final OutputStream outputStream) {
227225
// reject null early on rather than waiting for IO operation to fail
228-
if (outputStream == null) { // not checked by BufferedOutputStream
229-
throw new NullPointerException();
230-
}
226+
// not checked by BufferedInputStream
227+
Objects.requireNonNull(outputStream, "outputStream");
231228
return outputStream instanceof BufferedOutputStream ?
232229
(BufferedOutputStream) outputStream : new BufferedOutputStream(outputStream);
233230
}
@@ -244,9 +241,8 @@ public static BufferedOutputStream buffer(final OutputStream outputStream) {
244241
*/
245242
public static BufferedOutputStream buffer(final OutputStream outputStream, final int size) {
246243
// reject null early on rather than waiting for IO operation to fail
247-
if (outputStream == null) { // not checked by BufferedOutputStream
248-
throw new NullPointerException();
249-
}
244+
// not checked by BufferedInputStream
245+
Objects.requireNonNull(outputStream, "outputStream");
250246
return outputStream instanceof BufferedOutputStream ?
251247
(BufferedOutputStream) outputStream : new BufferedOutputStream(outputStream, size);
252248
}

0 commit comments

Comments
 (0)