Skip to content

Commit 9286d35

Browse files
author
Stephen Colebourne
committed
Add FileFileFilter that accepts files and not directories
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@424742 13f79535-47bb-0310-9956-ffa450edef68
1 parent e656ba3 commit 9286d35

4 files changed

Lines changed: 89 additions & 2 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ Enhancements from 1.2
5454
- NameFileFilter
5555
- Ability to control case-sensitivity
5656

57+
- FileFileFilter
58+
- New IOFileFilter implementation
59+
- Accepts files where File.isFile() is true
60+
- In other words it filters out directories
61+
5762
- CanReadFileFilter
5863
- New IOFileFilter implementation
5964
- Accepts files where File.canRead() is true
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2006 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.io.File;
19+
20+
/**
21+
* This filter accepts <code>File</code>s that are files (not directories).
22+
* <p>
23+
* For example, here is how to print out a list of the real files
24+
* within the current directory:
25+
*
26+
* <pre>
27+
* File dir = new File(".");
28+
* String[] files = dir.list( FileFileFilter.FILE );
29+
* for ( int i = 0; i &lt; files.length; i++ ) {
30+
* System.out.println(files[i]);
31+
* }
32+
* </pre>
33+
*
34+
* @since Commons IO 1.3
35+
* @version $Revision: 155419 $ $Date: 2005-02-26 13:02:41 +0000 (Sat, 26 Feb 2005) $
36+
*/
37+
public class FileFileFilter extends AbstractFileFilter {
38+
39+
/** Singleton instance of file filter */
40+
public static final IOFileFilter FILE = new FileFileFilter();
41+
42+
/**
43+
* Restrictive consructor.
44+
*/
45+
protected FileFileFilter() {
46+
}
47+
48+
/**
49+
* Checks to see if the file is a file.
50+
*
51+
* @param file the File to check
52+
* @return true if the file is a file
53+
*/
54+
public boolean accept(File file) {
55+
return file.isFile();
56+
}
57+
58+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,21 @@ public static IOFileFilter nameFileFilter(String name) {
7676
/**
7777
* Returns a filter that checks if the file is a directory.
7878
*
79-
* @return directory file filter
79+
* @return file filter that accepts only directories and not files
8080
*/
8181
public static IOFileFilter directoryFileFilter() {
8282
return DirectoryFileFilter.INSTANCE;
8383
}
84-
84+
85+
/**
86+
* Returns a filter that checks if the file is a file (and not a directory).
87+
*
88+
* @return file filter that accepts only files and not directories
89+
*/
90+
public static IOFileFilter fileFileFilter() {
91+
return FileFileFilter.FILE;
92+
}
93+
8594
//-----------------------------------------------------------------------
8695
/**
8796
* Returns a filter that ANDs the two specified filters.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ public void testDirectory() throws Exception {
138138
assertFiltering(filter, new File("STATUS.html"), false);
139139
}
140140

141+
public void testFiles() throws Exception {
142+
// XXX: This test presumes the current working dir is the base dir of the source checkout.
143+
IOFileFilter filter = FileFileFilter.FILE;
144+
145+
assertFiltering(filter, new File("src/"), false);
146+
assertFiltering(filter, new File("src/java/"), false);
147+
148+
assertFiltering(filter, new File("project.xml"), true);
149+
150+
assertFiltering(filter, new File("imaginary"), false);
151+
assertFiltering(filter, new File("imaginary/"), false);
152+
153+
assertFiltering(filter, new File("STATUS.html"), true);
154+
}
155+
141156
public void testPrefix() throws Exception {
142157
IOFileFilter filter = new PrefixFileFilter(new String[] { "foo", "bar" });
143158
File testFile = new File( "test" );

0 commit comments

Comments
 (0)