Skip to content

Commit ba53c60

Browse files
committed
Bug #30705 - Add conditional file filters.
I effectively merged these new filters into the existing And and Or filters, rather than having multiple flavours of them. In practice, this means that I added the contructors from the original filters to these new ones, and then replaced the old with the new. All of the pre-existing tests work with the resulting filters, along with all the new tests supplied with the enhancement request. Thanks to Steven Caswell for the code. I also had to exclude the abstract test cases in the Maven build file, so that JUnit wouldn't try to instantiate and run them, and regenerate the Ant build file. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140618 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7a58802 commit ba53c60

9 files changed

Lines changed: 1198 additions & 92 deletions

File tree

build.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<!--build.xml generated by maven from project.xml version 1.1-dev
4-
on date October 23 2004, time 1747-->
4+
on date October 24 2004, time 1323-->
55

66
<project default="jar" name="commons-io" basedir=".">
77
<property name="defaulttargetdir" value="target">
@@ -106,6 +106,8 @@
106106
<fileset dir="src/test">
107107
<include name="**/*Test*">
108108
</include>
109+
<exclude name="**/*AbstractTestCase*">
110+
</exclude>
109111
<exclude name="**/testtools/**">
110112
</exclude>
111113
</fileset>

project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
<include>**/*Test*</include>
184184
</includes>
185185
<excludes>
186+
<exclude>**/*AbstractTestCase*</exclude>
186187
<exclude>**/testtools/**</exclude>
187188
</excludes>
188189
</unitTest>
Lines changed: 108 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2002-2004 The Apache Software Foundation.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,56 +16,119 @@
1616
package org.apache.commons.io.filefilter;
1717

1818
import java.io.File;
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.Iterator;
22+
import java.util.List;
1923

2024
/**
21-
* This filter produces a logical AND of the two filters specified.
25+
* A {@link FileFilter} providing conditional AND logic across a list of
26+
* file filters. This filter returns <code>true</code> if all filters in the
27+
* list return <code>true</code>. Otherwise, it returns <code>false</code>.
28+
* Checking of the file filter list stops when the first filter returns
29+
* <code>false</code>.
2230
*
2331
* @since Commons IO 1.0
24-
* @version $Revision: 1.8 $ $Date: 2004/02/23 04:37:57 $
25-
*
26-
* @author Stephen Colebourne
32+
* @version $Revision: 1.9 $ $Date: 2004/10/24 21:58:44 $
33+
*
34+
* @author Steven Caswell
2735
*/
28-
public class AndFileFilter extends AbstractFileFilter {
29-
30-
/** The first filter */
31-
private IOFileFilter filter1;
32-
/** The second filter */
33-
private IOFileFilter filter2;
36+
public class AndFileFilter
37+
extends AbstractFileFilter
38+
implements IOFileFilter, ConditionalFileFilter {
39+
40+
private List fileFilters;
41+
42+
/**
43+
* Constructs a new instance of <code>AndFileFilter</code>.
44+
*/
45+
public AndFileFilter() {
46+
this.fileFilters = new ArrayList();
47+
}
48+
49+
/**
50+
* Constructs a new instance of <code>AndFileFilter</code>
51+
* with the specified list of filters.
52+
*/
53+
public AndFileFilter(final List fileFilters) {
54+
this.fileFilters = new ArrayList(fileFilters);
55+
}
3456

35-
/**
36-
* Constructs a new file filter that ANDs the result of two other filters.
37-
*
38-
* @param filter1 the first filter, must not be null
39-
* @param filter2 the second filter, must not be null
40-
* @throws IllegalArgumentException if either filter is null
41-
*/
42-
public AndFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
43-
if (filter1 == null || filter2 == null) {
44-
throw new IllegalArgumentException("The filters must not be null");
45-
}
46-
this.filter1 = filter1;
47-
this.filter2 = filter2;
57+
/**
58+
* Constructs a new file filter that ANDs the result of two other filters.
59+
*
60+
* @param filter1 the first filter, must not be null
61+
* @param filter2 the second filter, must not be null
62+
* @throws IllegalArgumentException if either filter is null
63+
*/
64+
public AndFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
65+
if (filter1 == null || filter2 == null) {
66+
throw new IllegalArgumentException("The filters must not be null");
67+
}
68+
this.fileFilters = new ArrayList();
69+
addFileFilter(filter1);
70+
addFileFilter(filter2);
71+
}
72+
73+
/**
74+
* @{inheritDoc}
75+
*/
76+
public void addFileFilter(final IOFileFilter ioFileFilter) {
77+
this.fileFilters.add(ioFileFilter);
78+
}
79+
80+
/**
81+
* @{inheritDoc}
82+
*/
83+
public List getFileFilters() {
84+
return Collections.unmodifiableList(this.fileFilters);
85+
}
86+
87+
/**
88+
* @{inheritDoc}
89+
*/
90+
public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
91+
return this.fileFilters.remove(ioFileFilter);
92+
}
93+
94+
/**
95+
* {@inheritDoc}
96+
*/
97+
public void setFileFilters(final List fileFilters) {
98+
this.fileFilters = new ArrayList(fileFilters);
99+
}
100+
101+
/**
102+
* @{inheritDoc}
103+
*/
104+
public boolean accept(final File file) {
105+
if(this.fileFilters.size() == 0) {
106+
return false;
107+
}
108+
for(Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
109+
IOFileFilter fileFilter = (IOFileFilter) iter.next();
110+
if(!fileFilter.accept(file)) {
111+
return false;
112+
}
48113
}
114+
return true;
115+
}
49116

50-
/**
51-
* Checks to see if both filters are true.
52-
*
53-
* @param file the File to check
54-
* @return true if both filters are true
55-
*/
56-
public boolean accept(File file) {
57-
return filter1.accept(file) && filter2.accept(file);
117+
/**
118+
* @{inheritDoc}
119+
*/
120+
public boolean accept(final File file, final String name)
121+
{
122+
if(this.fileFilters.size() == 0) {
123+
return false;
58124
}
59-
60-
/**
61-
* Checks to see if both filters are true.
62-
*
63-
* @param file the File directory
64-
* @param name the filename
65-
* @return true if both filters are true
66-
*/
67-
public boolean accept(File file, String name) {
68-
return filter1.accept(file, name) && filter2.accept(file, name);
125+
for(Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
126+
IOFileFilter fileFilter = (IOFileFilter) iter.next();
127+
if(!fileFilter.accept(file, name)) {
128+
return false;
129+
}
69130
}
70-
131+
return true;
132+
}
133+
71134
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2002-2004 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.io.filefilter;
17+
18+
import java.util.List;
19+
20+
/**
21+
* Defines operations for conditional file filters.
22+
*
23+
* @since Commons IO 1.0
24+
* @version $Revision: 1.1 $ $Date: 2004/10/24 21:58:44 $
25+
*
26+
* @author Steven Caswell
27+
*/
28+
public interface ConditionalFileFilter
29+
{
30+
/**
31+
* Adds the specified file filter to the list of file filters at the end of
32+
* the list.
33+
*
34+
* @param ioFileFilter the filter to be added
35+
*/
36+
public void addFileFilter(IOFileFilter ioFileFilter);
37+
38+
/**
39+
* Returns this conditional file filter's list of file filters.
40+
*
41+
* @return the file filter list
42+
*/
43+
public List getFileFilters();
44+
45+
/**
46+
* Removes the specified file filter.
47+
*
48+
* @param ioFileFilter filter to be removed
49+
* @return <code>true</code> if the filter was found in the list,
50+
* <code>false</code> otherwise
51+
*/
52+
public boolean removeFileFilter(IOFileFilter ioFileFilter);
53+
54+
/**
55+
* Sets the list of file filters, replacing any previously configured
56+
* file filters on this filter.
57+
*
58+
* @param fileFilters the list of filters
59+
*/
60+
public void setFileFilters(List fileFilters);
61+
62+
}

0 commit comments

Comments
 (0)