Skip to content

Commit dacc8c6

Browse files
author
Gary Gregory
committed
Refactor null-checks.
1 parent eb40d8e commit dacc8c6

10 files changed

Lines changed: 25 additions & 42 deletions

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
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+
4249
static FileVisitResult toDefaultFileVisitResult(final boolean accept) {
4350
return accept ? FileVisitResult.CONTINUE : FileVisitResult.TERMINATE;
4451
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,22 @@ public class DelegateFileFilter extends AbstractFileFilter implements Serializab
3939
/**
4040
* Constructs a delegate file filter around an existing FileFilter.
4141
*
42-
* @param filter the filter to decorate
42+
* @param fileFilter the filter to decorate
4343
*/
44-
public DelegateFileFilter(final FileFilter filter) {
45-
if (filter == null) {
46-
throw new IllegalArgumentException("The FileFilter must not be null");
47-
}
48-
this.fileFilter = filter;
44+
public DelegateFileFilter(final FileFilter fileFilter) {
45+
requireNonNull(fileFilter, "filter");
46+
this.fileFilter = fileFilter;
4947
this.filenameFilter = null;
5048
}
5149

5250
/**
5351
* Constructs a delegate file filter around an existing FilenameFilter.
5452
*
55-
* @param filter the filter to decorate
53+
* @param filenameFilter the filter to decorate
5654
*/
57-
public DelegateFileFilter(final FilenameFilter filter) {
58-
if (filter == null) {
59-
throw new IllegalArgumentException("The FilenameFilter must not be null");
60-
}
61-
this.filenameFilter = filter;
55+
public DelegateFileFilter(final FilenameFilter filenameFilter) {
56+
requireNonNull(filenameFilter, "filter");
57+
this.filenameFilter = filenameFilter;
6258
this.fileFilter = null;
6359
}
6460

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ public MagicNumberFileFilter(final byte[] magicNumber) {
164164
* is a negative number.
165165
*/
166166
public MagicNumberFileFilter(final byte[] magicNumbers, final long offset) {
167-
if (magicNumbers == null) {
168-
throw new IllegalArgumentException("The magic number cannot be null");
169-
}
167+
requireNonNull(magicNumbers, "magicNumbers");
170168
if (magicNumbers.length == 0) {
171169
throw new IllegalArgumentException("The magic number must contain at least one byte");
172170
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ public NameFileFilter(final List<String> names) {
9494
* @throws ClassCastException if the list does not contain Strings
9595
*/
9696
public NameFileFilter(final List<String> names, final IOCase ioCase) {
97-
if (names == null) {
98-
throw new IllegalArgumentException("The list of names must not be null");
99-
}
97+
requireNonNull(names, "names");
10098
this.names = names.toArray(EMPTY_STRING_ARRAY);
10199
this.ioCase = toIOCase(ioCase);
102100
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public class NotFileFilter extends AbstractFileFilter implements Serializable {
4242
* @throws IllegalArgumentException if the filter is null
4343
*/
4444
public NotFileFilter(final IOFileFilter filter) {
45-
if (filter == null) {
46-
throw new IllegalArgumentException("The filter must not be null");
47-
}
45+
requireNonNull(filter, "filter");
4846
this.filter = filter;
4947
}
5048

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ public PrefixFileFilter(final List<String> prefixes) {
9595
* @since 1.4
9696
*/
9797
public PrefixFileFilter(final List<String> prefixes, final IOCase ioCase) {
98-
if (prefixes == null) {
99-
throw new IllegalArgumentException("The list of prefixes must not be null");
100-
}
98+
requireNonNull(prefixes, "prefixes");
10199
this.prefixes = prefixes.toArray(EMPTY_STRING_ARRAY);
102100
this.isCase = IOCase.value(ioCase, IOCase.SENSITIVE);
103101
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ public class RegexFileFilter extends AbstractFileFilter implements Serializable
7878
* @return a new Pattern.
7979
*/
8080
private static Pattern compile(final String pattern, final int flags) {
81-
if (pattern == null) {
82-
throw new IllegalArgumentException("Pattern is missing");
83-
}
81+
requireNonNull(pattern, "pattern");
8482
return Pattern.compile(pattern, flags);
8583
}
8684

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ public SuffixFileFilter(final List<String> suffixes) {
9797
* @since 1.4
9898
*/
9999
public SuffixFileFilter(final List<String> suffixes, final IOCase ioCase) {
100-
if (suffixes == null) {
101-
throw new IllegalArgumentException("The list of suffixes must not be null");
102-
}
100+
requireNonNull(suffixes, "suffixes");
103101
this.suffixes = suffixes.toArray(EMPTY_STRING_ARRAY);
104102
this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
105103
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ public WildcardFileFilter(final List<String> wildcards) {
104104
* @throws ClassCastException if the list does not contain Strings
105105
*/
106106
public WildcardFileFilter(final List<String> wildcards, final IOCase ioCase) {
107-
if (wildcards == null) {
108-
throw new IllegalArgumentException("The wildcard list must not be null");
109-
}
107+
requireNonNull(wildcards, "wildcards");
110108
this.wildcards = wildcards.toArray(EMPTY_STRING_ARRAY);
111109
this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
112110
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ public class WildcardFilter extends AbstractFileFilter implements Serializable {
9292
* @throws ClassCastException if the list does not contain Strings
9393
*/
9494
public WildcardFilter(final List<String> wildcards) {
95-
if (wildcards == null) {
96-
throw new IllegalArgumentException("The wildcard list must not be null");
97-
}
95+
requireNonNull(wildcards, "wildcards");
9896
this.wildcards = wildcards.toArray(EMPTY_STRING_ARRAY);
9997
}
10098

@@ -105,9 +103,7 @@ public WildcardFilter(final List<String> wildcards) {
105103
* @throws IllegalArgumentException if the pattern is null
106104
*/
107105
public WildcardFilter(final String wildcard) {
108-
if (wildcard == null) {
109-
throw new IllegalArgumentException("The wildcard must not be null");
110-
}
106+
requireNonNull(wildcard, "wildcard");
111107
this.wildcards = new String[] { wildcard };
112108
}
113109

@@ -118,9 +114,7 @@ public WildcardFilter(final String wildcard) {
118114
* @throws IllegalArgumentException if the pattern array is null
119115
*/
120116
public WildcardFilter(final String... wildcards) {
121-
if (wildcards == null) {
122-
throw new IllegalArgumentException("The wildcard array must not be null");
123-
}
117+
requireNonNull(wildcards, "wildcards");
124118
this.wildcards = wildcards.clone();
125119
}
126120

0 commit comments

Comments
 (0)