Skip to content

Commit fe3554c

Browse files
author
Niall Pemberton
committed
IO-133 - Make fields final so classes are immutable/threadsafe - thanks to Sebb for the patch
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@591058 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6d38af6 commit fe3554c

11 files changed

Lines changed: 53 additions & 48 deletions

RELEASE-NOTES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Enhancements from 1.3.2
5656

5757
- Make IOFileFilter implementations Serializable [IO-131]
5858

59+
- Make fields final so classes are immutable/threadsafe [IO-133]
60+
- changes to Age, Delegate, Name, Not, Prefix, Regex, Size, Suffix and Wildcard IOFileFilter
61+
implementations.
5962

6063
Feedback
6164
--------

src/java/org/apache/commons/io/filefilter/AgeFileFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
public class AgeFileFilter extends AbstractFileFilter implements Serializable {
4747

4848
/** The cutoff time threshold. */
49-
private long cutoff;
49+
private final long cutoff;
5050
/** Whether the files accepted will be older or newer. */
51-
private boolean acceptOlder;
51+
private final boolean acceptOlder;
5252

5353
/**
5454
* Constructs a new age file filter for files equal to or older than

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
public class DelegateFileFilter extends AbstractFileFilter implements Serializable {
3333

3434
/** The Filename filter */
35-
private FilenameFilter filenameFilter;
35+
private final FilenameFilter filenameFilter;
3636
/** The File filter */
37-
private FileFilter fileFilter;
37+
private final FileFilter fileFilter;
3838

3939
/**
4040
* Constructs a delegate file filter around an existing FilenameFilter.
@@ -46,6 +46,7 @@ public DelegateFileFilter(FilenameFilter filter) {
4646
throw new IllegalArgumentException("The FilenameFilter must not be null");
4747
}
4848
this.filenameFilter = filter;
49+
this.fileFilter = null;
4950
}
5051

5152
/**
@@ -58,6 +59,7 @@ public DelegateFileFilter(FileFilter filter) {
5859
throw new IllegalArgumentException("The FileFilter must not be null");
5960
}
6061
this.fileFilter = filter;
62+
this.filenameFilter = null;
6163
}
6264

6365
/**
@@ -96,7 +98,7 @@ public boolean accept(File dir, String name) {
9698
*/
9799
public String toString() {
98100
String delegate = (fileFilter != null ? fileFilter.toString() : filenameFilter.toString());
99-
return super.toString() + "(" + delegate.toString() + ")";
101+
return super.toString() + "(" + delegate + ")";
100102
}
101103

102104
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
public class NameFileFilter extends AbstractFileFilter implements Serializable {
4848

4949
/** The filenames to search for */
50-
private String[] names;
50+
private final String[] names;
5151
/** Whether the comparison is case sensitive. */
52-
private IOCase caseSensitivity;
52+
private final IOCase caseSensitivity;
5353

5454
/**
5555
* Constructs a new case-sensitive name file filter for a single name.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public class NotFileFilter extends AbstractFileFilter implements Serializable {
3131

3232
/** The filter */
33-
private IOFileFilter filter;
33+
private final IOFileFilter filter;
3434

3535
/**
3636
* Constructs a new file filter that NOTs the result of another filters.

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
public class PrefixFileFilter extends AbstractFileFilter implements Serializable {
4747

4848
/** The filename prefixes to search for */
49-
private String[] prefixes;
49+
private final String[] prefixes;
5050

5151
/** Whether the comparison is case sensitive. */
52-
private IOCase caseSensitivity = IOCase.SENSITIVE;
52+
private final IOCase caseSensitivity;
5353

5454
/**
5555
* Constructs a new Prefix file filter for a single prefix.
@@ -58,10 +58,7 @@ public class PrefixFileFilter extends AbstractFileFilter implements Serializable
5858
* @throws IllegalArgumentException if the prefix is null
5959
*/
6060
public PrefixFileFilter(String prefix) {
61-
if (prefix == null) {
62-
throw new IllegalArgumentException("The prefix must not be null");
63-
}
64-
this.prefixes = new String[] {prefix};
61+
this(prefix, IOCase.SENSITIVE);
6562
}
6663

6764
/**
@@ -74,7 +71,10 @@ public PrefixFileFilter(String prefix) {
7471
* @since Commons IO 1.4
7572
*/
7673
public PrefixFileFilter(String prefix, IOCase caseSensitivity) {
77-
this(prefix);
74+
if (prefix == null) {
75+
throw new IllegalArgumentException("The prefix must not be null");
76+
}
77+
this.prefixes = new String[] {prefix};
7878
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
7979
}
8080

@@ -88,10 +88,7 @@ public PrefixFileFilter(String prefix, IOCase caseSensitivity) {
8888
* @throws IllegalArgumentException if the prefix array is null
8989
*/
9090
public PrefixFileFilter(String[] prefixes) {
91-
if (prefixes == null) {
92-
throw new IllegalArgumentException("The array of prefixes must not be null");
93-
}
94-
this.prefixes = prefixes;
91+
this(prefixes, IOCase.SENSITIVE);
9592
}
9693

9794
/**
@@ -107,7 +104,10 @@ public PrefixFileFilter(String[] prefixes) {
107104
* @since Commons IO 1.4
108105
*/
109106
public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {
110-
this(prefixes);
107+
if (prefixes == null) {
108+
throw new IllegalArgumentException("The array of prefixes must not be null");
109+
}
110+
this.prefixes = prefixes;
111111
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
112112
}
113113

@@ -119,10 +119,7 @@ public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {
119119
* @throws ClassCastException if the list does not contain Strings
120120
*/
121121
public PrefixFileFilter(List prefixes) {
122-
if (prefixes == null) {
123-
throw new IllegalArgumentException("The list of prefixes must not be null");
124-
}
125-
this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);
122+
this(prefixes, IOCase.SENSITIVE);
126123
}
127124

128125
/**
@@ -136,7 +133,10 @@ public PrefixFileFilter(List prefixes) {
136133
* @since Commons IO 1.4
137134
*/
138135
public PrefixFileFilter(List prefixes, IOCase caseSensitivity) {
139-
this(prefixes);
136+
if (prefixes == null) {
137+
throw new IllegalArgumentException("The list of prefixes must not be null");
138+
}
139+
this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);
140140
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
141141
}
142142

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
public class RegexFileFilter extends AbstractFileFilter implements Serializable {
4646

4747
/** The regular expression pattern that will be used to match filenames */
48-
private Pattern pattern;
48+
private final Pattern pattern;
4949

5050
/**
5151
* Construct a new regular expression filter.

src/java/org/apache/commons/io/filefilter/SizeFileFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
public class SizeFileFilter extends AbstractFileFilter implements Serializable {
4242

4343
/** The size threshold. */
44-
private long size;
44+
private final long size;
4545
/** Whether the files accepted will be larger or smaller. */
46-
private boolean acceptLarger;
46+
private final boolean acceptLarger;
4747

4848
/**
4949
* Constructs a new size file filter for files equal to or

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
public class SuffixFileFilter extends AbstractFileFilter implements Serializable {
4848

4949
/** The filename suffixes to search for */
50-
private String[] suffixes;
50+
private final String[] suffixes;
5151

5252
/** Whether the comparison is case sensitive. */
53-
private IOCase caseSensitivity = IOCase.SENSITIVE;
53+
private final IOCase caseSensitivity;
5454

5555
/**
5656
* Constructs a new Suffix file filter for a single extension.
@@ -59,10 +59,7 @@ public class SuffixFileFilter extends AbstractFileFilter implements Serializable
5959
* @throws IllegalArgumentException if the suffix is null
6060
*/
6161
public SuffixFileFilter(String suffix) {
62-
if (suffix == null) {
63-
throw new IllegalArgumentException("The suffix must not be null");
64-
}
65-
this.suffixes = new String[] {suffix};
62+
this(suffix, IOCase.SENSITIVE);
6663
}
6764

6865
/**
@@ -75,7 +72,10 @@ public SuffixFileFilter(String suffix) {
7572
* @since Commons IO 1.4
7673
*/
7774
public SuffixFileFilter(String suffix, IOCase caseSensitivity) {
78-
this(suffix);
75+
if (suffix == null) {
76+
throw new IllegalArgumentException("The suffix must not be null");
77+
}
78+
this.suffixes = new String[] {suffix};
7979
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
8080
}
8181

@@ -89,10 +89,7 @@ public SuffixFileFilter(String suffix, IOCase caseSensitivity) {
8989
* @throws IllegalArgumentException if the suffix array is null
9090
*/
9191
public SuffixFileFilter(String[] suffixes) {
92-
if (suffixes == null) {
93-
throw new IllegalArgumentException("The array of suffixes must not be null");
94-
}
95-
this.suffixes = suffixes;
92+
this(suffixes, IOCase.SENSITIVE);
9693
}
9794

9895
/**
@@ -108,7 +105,10 @@ public SuffixFileFilter(String[] suffixes) {
108105
* @since Commons IO 1.4
109106
*/
110107
public SuffixFileFilter(String[] suffixes, IOCase caseSensitivity) {
111-
this(suffixes);
108+
if (suffixes == null) {
109+
throw new IllegalArgumentException("The array of suffixes must not be null");
110+
}
111+
this.suffixes = suffixes;
112112
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
113113
}
114114

@@ -120,10 +120,7 @@ public SuffixFileFilter(String[] suffixes, IOCase caseSensitivity) {
120120
* @throws ClassCastException if the list does not contain Strings
121121
*/
122122
public SuffixFileFilter(List suffixes) {
123-
if (suffixes == null) {
124-
throw new IllegalArgumentException("The list of suffixes must not be null");
125-
}
126-
this.suffixes = (String[]) suffixes.toArray(new String[suffixes.size()]);
123+
this(suffixes, IOCase.SENSITIVE);
127124
}
128125

129126
/**
@@ -137,7 +134,10 @@ public SuffixFileFilter(List suffixes) {
137134
* @since Commons IO 1.4
138135
*/
139136
public SuffixFileFilter(List suffixes, IOCase caseSensitivity) {
140-
this(suffixes);
137+
if (suffixes == null) {
138+
throw new IllegalArgumentException("The list of suffixes must not be null");
139+
}
140+
this.suffixes = (String[]) suffixes.toArray(new String[suffixes.size()]);
141141
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
142142
}
143143

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
public class WildcardFileFilter extends AbstractFileFilter implements Serializable {
5353

5454
/** The wildcards that will be used to match filenames. */
55-
private String[] wildcards;
55+
private final String[] wildcards;
5656
/** Whether the comparison is case sensitive. */
57-
private IOCase caseSensitivity;
57+
private final IOCase caseSensitivity;
5858

5959
/**
6060
* Construct a new case-sensitive wildcard filter for a single wildcard.

0 commit comments

Comments
 (0)