Skip to content

Commit c0368c8

Browse files
author
Stephen Colebourne
committed
Add case sensitivity support to NameFileFilter
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@418643 13f79535-47bb-0310-9956-ffa450edef68
1 parent 30bf8b4 commit c0368c8

3 files changed

Lines changed: 82 additions & 11 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Enhancements from 1.2
5151
- Accepts both files and directories
5252
- Ability to control case-sensitivity
5353

54+
- NameFileFilter
55+
- Ability to control case-sensitivity
56+
5457

5558
Feedback
5659
--------

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

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2004 The Apache Software Foundation.
2+
* Copyright 2002-2004,2006 The Apache Software Foundation.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818
import java.io.File;
1919
import java.util.List;
2020

21+
import org.apache.commons.io.IOCase;
22+
2123
/**
2224
* Filters filenames for a certain name.
2325
* <p>
@@ -45,22 +47,35 @@ public class NameFileFilter extends AbstractFileFilter {
4547

4648
/** The filenames to search for */
4749
private String[] names;
50+
/** Whether the comparison is case sensitive. */
51+
private IOCase caseSensitivity;
4852

4953
/**
50-
* Constructs a new name file filter for a single name.
54+
* Constructs a new case-sensitive name file filter for a single name.
5155
*
5256
* @param name the name to allow, must not be null
53-
* @throws IllegalArgumentException if the prefix is null
57+
* @throws IllegalArgumentException if the name is null
5458
*/
5559
public NameFileFilter(String name) {
60+
this(name, null);
61+
}
62+
63+
/**
64+
* Construct a new name file filter specifying case-sensitivity.
65+
*
66+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
67+
* @throws IllegalArgumentException if the name is null
68+
*/
69+
public NameFileFilter(String name, IOCase caseSensitivity) {
5670
if (name == null) {
57-
throw new IllegalArgumentException("The name must not be null");
71+
throw new IllegalArgumentException("The wildcard must not be null");
5872
}
5973
this.names = new String[] {name};
74+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
6075
}
6176

6277
/**
63-
* Constructs a new name file filter for any of an array of names.
78+
* Constructs a new case-sensitive name file filter for an array of names.
6479
* <p>
6580
* The array is not cloned, so could be changed after constructing the
6681
* instance. This would be inadvisable however.
@@ -69,26 +84,55 @@ public NameFileFilter(String name) {
6984
* @throws IllegalArgumentException if the names array is null
7085
*/
7186
public NameFileFilter(String[] names) {
87+
this(names, null);
88+
}
89+
90+
/**
91+
* Constructs a new name file filter for an array of names specifying case-sensitivity.
92+
* <p>
93+
* The array is not cloned, so could be changed after constructing the
94+
* instance. This would be inadvisable however.
95+
*
96+
* @param names the names to allow, must not be null
97+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
98+
* @throws IllegalArgumentException if the names array is null
99+
*/
100+
public NameFileFilter(String[] names, IOCase caseSensitivity) {
72101
if (names == null) {
73102
throw new IllegalArgumentException("The array of names must not be null");
74103
}
75104
this.names = names;
105+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
76106
}
77107

78108
/**
79-
* Constructs a new name file filter for a list of names.
109+
* Constructs a new case-sensitive name file filter for a list of names.
80110
*
81111
* @param names the names to allow, must not be null
82112
* @throws IllegalArgumentException if the name list is null
83113
* @throws ClassCastException if the list does not contain Strings
84114
*/
85115
public NameFileFilter(List names) {
116+
this(names, null);
117+
}
118+
119+
/**
120+
* Constructs a new name file filter for a list of names specifying case-sensitivity.
121+
*
122+
* @param names the names to allow, must not be null
123+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
124+
* @throws IllegalArgumentException if the name list is null
125+
* @throws ClassCastException if the list does not contain Strings
126+
*/
127+
public NameFileFilter(List names, IOCase caseSensitivity) {
86128
if (names == null) {
87129
throw new IllegalArgumentException("The list of names must not be null");
88130
}
89131
this.names = (String[]) names.toArray(new String[names.size()]);
132+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
90133
}
91134

135+
//-----------------------------------------------------------------------
92136
/**
93137
* Checks to see if the filename matches.
94138
*
@@ -98,13 +142,13 @@ public NameFileFilter(List names) {
98142
public boolean accept(File file) {
99143
String name = file.getName();
100144
for (int i = 0; i < this.names.length; i++) {
101-
if (name.equals(this.names[i])) {
145+
if (caseSensitivity.checkEquals(name, names[i])) {
102146
return true;
103147
}
104148
}
105149
return false;
106150
}
107-
151+
108152
/**
109153
* Checks to see if the filename matches.
110154
*
@@ -113,12 +157,12 @@ public boolean accept(File file) {
113157
* @return true if the filename matches
114158
*/
115159
public boolean accept(File file, String name) {
116-
for (int i = 0; i < this.names.length; i++) {
117-
if (name.equals(this.names[i])) {
160+
for (int i = 0; i < names.length; i++) {
161+
if (caseSensitivity.checkEquals(name, names[i])) {
118162
return true;
119163
}
120164
}
121165
return false;
122166
}
123-
167+
124168
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ public void testNameFilter() throws Exception {
193193
assertFiltering(filter, new File("foo"), true);
194194
assertFiltering(filter, new File("bar"), true);
195195
assertFiltering(filter, new File("fred"), false);
196+
197+
filter = new NameFileFilter(new String[] { "foo", "bar" }, IOCase.SENSITIVE);
198+
assertFiltering(filter, new File("foo"), true);
199+
assertFiltering(filter, new File("bar"), true);
200+
assertFiltering(filter, new File("FOO"), false);
201+
assertFiltering(filter, new File("BAR"), false);
202+
203+
filter = new NameFileFilter(new String[] { "foo", "bar" }, IOCase.INSENSITIVE);
204+
assertFiltering(filter, new File("foo"), true);
205+
assertFiltering(filter, new File("bar"), true);
206+
assertFiltering(filter, new File("FOO"), true);
207+
assertFiltering(filter, new File("BAR"), true);
208+
209+
filter = new NameFileFilter(new String[] { "foo", "bar" }, IOCase.SYSTEM);
210+
assertFiltering(filter, new File("foo"), true);
211+
assertFiltering(filter, new File("bar"), true);
212+
assertFiltering(filter, new File("FOO"), WINDOWS);
213+
assertFiltering(filter, new File("BAR"), WINDOWS);
214+
215+
filter = new NameFileFilter(new String[] { "foo", "bar" }, (IOCase) null);
216+
assertFiltering(filter, new File("foo"), true);
217+
assertFiltering(filter, new File("bar"), true);
218+
assertFiltering(filter, new File("FOO"), false);
219+
assertFiltering(filter, new File("BAR"), false);
196220

197221
// repeat for a List
198222
java.util.ArrayList list = new java.util.ArrayList();

0 commit comments

Comments
 (0)