Skip to content

Commit 9666d74

Browse files
author
Niall Pemberton
committed
IO-126 Add facility to specify case sensitivity for prefix and suffix file filters
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@584162 13f79535-47bb-0310-9956-ffa450edef68
1 parent e941766 commit 9666d74

4 files changed

Lines changed: 190 additions & 4 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ Enhancements from 1.3.2
3232
- ThesholdingOuputStream [IO-121]
3333
- Add a reset() method which sets the count of the bytes written back to zero.
3434

35+
- PrefixFileFilter [IO-126]
36+
- Add faciltiy to specify case sensitivty on prefix matching
37+
38+
- SuffixFileFilter [IO-126]
39+
- Add faciltiy to specify case sensitivty on suffix matching
40+
3541

3642
Feedback
3743
--------

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

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

1919
import java.io.File;
2020
import java.util.List;
21+
import org.apache.commons.io.IOCase;
2122

2223
/**
2324
* Filters filenames for a certain prefix.
@@ -46,6 +47,9 @@ public class PrefixFileFilter extends AbstractFileFilter {
4647
/** The filename prefixes to search for */
4748
private String[] prefixes;
4849

50+
/** Whether the comparison is case sensitive. */
51+
private IOCase caseSensitivity = IOCase.SENSITIVE;
52+
4953
/**
5054
* Constructs a new Prefix file filter for a single prefix.
5155
*
@@ -59,6 +63,20 @@ public PrefixFileFilter(String prefix) {
5963
this.prefixes = new String[] {prefix};
6064
}
6165

66+
/**
67+
* Constructs a new Prefix file filter for a single prefix
68+
* specifying case-sensitivity.
69+
*
70+
* @param prefix the prefix to allow, must not be null
71+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
72+
* @throws IllegalArgumentException if the prefix is null
73+
* @since Commons IO 1.4
74+
*/
75+
public PrefixFileFilter(String prefix, IOCase caseSensitivity) {
76+
this(prefix);
77+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
78+
}
79+
6280
/**
6381
* Constructs a new Prefix file filter for any of an array of prefixes.
6482
* <p>
@@ -75,6 +93,23 @@ public PrefixFileFilter(String[] prefixes) {
7593
this.prefixes = prefixes;
7694
}
7795

96+
/**
97+
* Constructs a new Prefix file filter for any of an array of prefixes
98+
* specifying case-sensitivity.
99+
* <p>
100+
* The array is not cloned, so could be changed after constructing the
101+
* instance. This would be inadvisable however.
102+
*
103+
* @param prefixes the prefixes to allow, must not be null
104+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
105+
* @throws IllegalArgumentException if the prefix is null
106+
* @since Commons IO 1.4
107+
*/
108+
public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {
109+
this(prefixes);
110+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
111+
}
112+
78113
/**
79114
* Constructs a new Prefix file filter for a list of prefixes.
80115
*
@@ -89,6 +124,21 @@ public PrefixFileFilter(List prefixes) {
89124
this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);
90125
}
91126

127+
/**
128+
* Constructs a new Prefix file filter for a list of prefixes
129+
* specifying case-sensitivity.
130+
*
131+
* @param prefixes the prefixes to allow, must not be null
132+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
133+
* @throws IllegalArgumentException if the prefix list is null
134+
* @throws ClassCastException if the list does not contain Strings
135+
* @since Commons IO 1.4
136+
*/
137+
public PrefixFileFilter(List prefixes, IOCase caseSensitivity) {
138+
this(prefixes);
139+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
140+
}
141+
92142
/**
93143
* Checks to see if the filename starts with the prefix.
94144
*
@@ -98,7 +148,7 @@ public PrefixFileFilter(List prefixes) {
98148
public boolean accept(File file) {
99149
String name = file.getName();
100150
for (int i = 0; i < this.prefixes.length; i++) {
101-
if (name.startsWith(this.prefixes[i])) {
151+
if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
102152
return true;
103153
}
104154
}
@@ -114,7 +164,7 @@ public boolean accept(File file) {
114164
*/
115165
public boolean accept(File file, String name) {
116166
for (int i = 0; i < prefixes.length; i++) {
117-
if (name.startsWith(prefixes[i])) {
167+
if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
118168
return true;
119169
}
120170
}

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

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

1919
import java.io.File;
2020
import java.util.List;
21+
import org.apache.commons.io.IOCase;
2122

2223
/**
2324
* Filters files based on the suffix (what the filename ends with).
@@ -47,6 +48,9 @@ public class SuffixFileFilter extends AbstractFileFilter {
4748
/** The filename suffixes to search for */
4849
private String[] suffixes;
4950

51+
/** Whether the comparison is case sensitive. */
52+
private IOCase caseSensitivity = IOCase.SENSITIVE;
53+
5054
/**
5155
* Constructs a new Suffix file filter for a single extension.
5256
*
@@ -60,6 +64,20 @@ public SuffixFileFilter(String suffix) {
6064
this.suffixes = new String[] {suffix};
6165
}
6266

67+
/**
68+
* Constructs a new Suffix file filter for a single extension
69+
* specifying case-sensitivity.
70+
*
71+
* @param suffix the suffix to allow, must not be null
72+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
73+
* @throws IllegalArgumentException if the suffix is null
74+
* @since Commons IO 1.4
75+
*/
76+
public SuffixFileFilter(String suffix, IOCase caseSensitivity) {
77+
this(suffix);
78+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
79+
}
80+
6381
/**
6482
* Constructs a new Suffix file filter for an array of suffixs.
6583
* <p>
@@ -76,6 +94,23 @@ public SuffixFileFilter(String[] suffixes) {
7694
this.suffixes = suffixes;
7795
}
7896

97+
/**
98+
* Constructs a new Suffix file filter for an array of suffixs
99+
* specifying case-sensitivity.
100+
* <p>
101+
* The array is not cloned, so could be changed after constructing the
102+
* instance. This would be inadvisable however.
103+
*
104+
* @param suffixes the suffixes to allow, must not be null
105+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
106+
* @throws IllegalArgumentException if the suffix array is null
107+
* @since Commons IO 1.4
108+
*/
109+
public SuffixFileFilter(String[] suffixes, IOCase caseSensitivity) {
110+
this(suffixes);
111+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
112+
}
113+
79114
/**
80115
* Constructs a new Suffix file filter for a list of suffixes.
81116
*
@@ -90,6 +125,21 @@ public SuffixFileFilter(List suffixes) {
90125
this.suffixes = (String[]) suffixes.toArray(new String[suffixes.size()]);
91126
}
92127

128+
/**
129+
* Constructs a new Suffix file filter for a list of suffixes
130+
* specifying case-sensitivity.
131+
*
132+
* @param suffixes the suffixes to allow, must not be null
133+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
134+
* @throws IllegalArgumentException if the suffix list is null
135+
* @throws ClassCastException if the list does not contain Strings
136+
* @since Commons IO 1.4
137+
*/
138+
public SuffixFileFilter(List suffixes, IOCase caseSensitivity) {
139+
this(suffixes);
140+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
141+
}
142+
93143
/**
94144
* Checks to see if the filename ends with the suffix.
95145
*
@@ -99,7 +149,7 @@ public SuffixFileFilter(List suffixes) {
99149
public boolean accept(File file) {
100150
String name = file.getName();
101151
for (int i = 0; i < this.suffixes.length; i++) {
102-
if (name.endsWith(this.suffixes[i])) {
152+
if (caseSensitivity.checkEndsWith(name, suffixes[i])) {
103153
return true;
104154
}
105155
}
@@ -115,7 +165,7 @@ public boolean accept(File file) {
115165
*/
116166
public boolean accept(File file, String name) {
117167
for (int i = 0; i < this.suffixes.length; i++) {
118-
if (name.endsWith(this.suffixes[i])) {
168+
if (caseSensitivity.checkEndsWith(name, suffixes[i])) {
119169
return true;
120170
}
121171
}

src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,46 @@ public void testSuffix() throws Exception {
129129
}
130130
}
131131

132+
public void testSuffixCaseInsensitive() throws Exception {
133+
134+
IOFileFilter filter = new SuffixFileFilter(new String[] { "tes", "est" }, IOCase.INSENSITIVE);
135+
assertFiltering(filter, new File("foo.tes"), true);
136+
assertFiltering(filter, new File("foo.est"), true);
137+
assertFiltering(filter, new File("foo.EST"), true); //case-sensitive
138+
assertFiltering(filter, new File("foo.TES"), true); //case-sensitive
139+
assertFiltering(filter, new File("foo.exe"), false);
140+
141+
filter = new SuffixFileFilter("est", IOCase.INSENSITIVE);
142+
assertFiltering(filter, new File("test"), true);
143+
assertFiltering(filter, new File("TEST"), true);
144+
145+
List suffixes = Arrays.asList( new String[] { "tes", "est" } );
146+
filter = new SuffixFileFilter(suffixes, IOCase.INSENSITIVE);
147+
assertFiltering(filter, new File("bar.tes"), true);
148+
assertFiltering(filter, new File("bar.est"), true);
149+
assertFiltering(filter, new File("bar.EST"), true); //case-sensitive
150+
assertFiltering(filter, new File("bar.TES"), true); //case-sensitive
151+
assertFiltering(filter, new File("bar.exe"), false);
152+
153+
try {
154+
new SuffixFileFilter((String) null, IOCase.INSENSITIVE);
155+
fail();
156+
} catch (IllegalArgumentException ex) {
157+
}
158+
159+
try {
160+
new SuffixFileFilter((String[]) null, IOCase.INSENSITIVE);
161+
fail();
162+
} catch (IllegalArgumentException ex) {
163+
}
164+
165+
try {
166+
new SuffixFileFilter((List) null, IOCase.INSENSITIVE);
167+
fail();
168+
} catch (IllegalArgumentException ex) {
169+
}
170+
}
171+
132172
public void testDirectory() throws Exception {
133173
// XXX: This test presumes the current working dir is the base dir of the source checkout.
134174
IOFileFilter filter = new DirectoryFileFilter();
@@ -211,6 +251,46 @@ public void testPrefix() throws Exception {
211251
}
212252
}
213253

254+
public void testPrefixCaseInsensitive() throws Exception {
255+
256+
IOFileFilter filter = new PrefixFileFilter(new String[] { "foo", "bar" }, IOCase.INSENSITIVE);
257+
assertFiltering(filter, new File("foo.test1"), true);
258+
assertFiltering(filter, new File("bar.test1"), true);
259+
assertFiltering(filter, new File("FOO.test1"), true); //case-sensitive
260+
assertFiltering(filter, new File("BAR.test1"), true); //case-sensitive
261+
262+
filter = new PrefixFileFilter("bar", IOCase.INSENSITIVE);
263+
assertFiltering(filter, new File("foo.test2"), false);
264+
assertFiltering(filter, new File("bar.test2"), true);
265+
assertFiltering(filter, new File("FOO.test2"), false); //case-sensitive
266+
assertFiltering(filter, new File("BAR.test2"), true); //case-sensitive
267+
268+
List prefixes = Arrays.asList( new String[] { "foo", "bar" } );
269+
filter = new PrefixFileFilter(prefixes, IOCase.INSENSITIVE);
270+
assertFiltering(filter, new File("foo.test3"), true);
271+
assertFiltering(filter, new File("bar.test3"), true);
272+
assertFiltering(filter, new File("FOO.test3"), true); //case-sensitive
273+
assertFiltering(filter, new File("BAR.test3"), true); //case-sensitive
274+
275+
try {
276+
new PrefixFileFilter((String) null, IOCase.INSENSITIVE);
277+
fail();
278+
} catch (IllegalArgumentException ex) {
279+
}
280+
281+
try {
282+
new PrefixFileFilter((String[]) null, IOCase.INSENSITIVE);
283+
fail();
284+
} catch (IllegalArgumentException ex) {
285+
}
286+
287+
try {
288+
new PrefixFileFilter((List) null, IOCase.INSENSITIVE);
289+
fail();
290+
} catch (IllegalArgumentException ex) {
291+
}
292+
}
293+
214294
public void testNameFilter() throws Exception {
215295
IOFileFilter filter = new NameFileFilter(new String[] { "foo", "bar" });
216296
assertFiltering(filter, new File("foo"), true);

0 commit comments

Comments
 (0)