Skip to content

Commit 1f8990a

Browse files
author
Niall Pemberton
committed
IO-85 IOFileFilter implementations for File.canRead(), File.canWrite(), File.isHidden() and empty files/directories
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@424601 13f79535-47bb-0310-9956-ffa450edef68
1 parent 180e6c3 commit 1f8990a

6 files changed

Lines changed: 397 additions & 0 deletions

File tree

RELEASE-NOTES.txt

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

57+
- CanReadFileFilter
58+
- New IOFileFilter implementation
59+
- Accepts files where File.canRead() is true
60+
61+
- CanWriteFileFilter
62+
- New IOFileFilter implementation
63+
- Accepts files where File.canWrite() is true
64+
65+
- HiddenFileFilter
66+
- New IOFileFilter implementation
67+
- Accepts files where File.isHidden() is true
68+
69+
- EmptyFileFilter
70+
- New IOFileFilter implementation
71+
- Accepts files or directories that are empty
72+
5773

5874
Feedback
5975
--------
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2005-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 can be read.
22+
* <p>
23+
* Example, showing how to print out a list of the
24+
* current directory's <i>readable</i> files:
25+
*
26+
* <pre>
27+
* File dir = new File(".");
28+
* String[] files = dir.list( CanReadFileFilter.CAN_READ );
29+
* for ( int i = 0; i &lt; files.length; i++ ) {
30+
* System.out.println(files[i]);
31+
* }
32+
* </pre>
33+
*
34+
* <p>
35+
* Example, showing how to print out a list of the
36+
* current directory's <i>un-readable</i> files:
37+
*
38+
* <pre>
39+
* File dir = new File(".");
40+
* String[] files = dir.list( CanReadFileFilter.CANNOT_READ );
41+
* for ( int i = 0; i &lt; files.length; i++ ) {
42+
* System.out.println(files[i]);
43+
* }
44+
* </pre>
45+
*
46+
* <p>
47+
* Example, showing how to print out a list of the
48+
* current directory's <i>read-only</i> files:
49+
*
50+
* <pre>
51+
* File dir = new File(".");
52+
* String[] files = dir.list( CanReadFileFilter.READ_ONLY );
53+
* for ( int i = 0; i &lt; files.length; i++ ) {
54+
* System.out.println(files[i]);
55+
* }
56+
* </pre>
57+
*
58+
* @since Commons IO 1.3
59+
* @version $Revision$
60+
*/
61+
public class CanReadFileFilter extends AbstractFileFilter {
62+
63+
/** Singleton instance of <i>readable</i> filter */
64+
public static final IOFileFilter CAN_READ = new CanReadFileFilter();
65+
66+
/** Singleton instance of not <i>readable</i> filter */
67+
public static final IOFileFilter CANNOT_READ = new NotFileFilter(CAN_READ);
68+
69+
/** Singleton instance of <i>read-only</i> filter */
70+
public static final IOFileFilter READ_ONLY = new AndFileFilter(CAN_READ,
71+
CanWriteFileFilter.CANNOT_WRITE);
72+
73+
/**
74+
* Restrictive consructor.
75+
*/
76+
protected CanReadFileFilter() {
77+
}
78+
79+
/**
80+
* Checks to see if the file can be read.
81+
*
82+
* @param file the File to check.
83+
* @return <code>true</code> if the file can be
84+
* read, otherwise <code>false</code>.
85+
*/
86+
public boolean accept(File file) {
87+
return file.canRead();
88+
}
89+
90+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2005-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 can be written to.
22+
* <p>
23+
* Example, showing how to print out a list of the
24+
* current directory's <i>writable</i> files:
25+
*
26+
* <pre>
27+
* File dir = new File(".");
28+
* String[] files = dir.list( CanWriteFileFilter.CAN_WRITE );
29+
* for ( int i = 0; i &lt; files.length; i++ ) {
30+
* System.out.println(files[i]);
31+
* }
32+
* </pre>
33+
*
34+
* <p>
35+
* Example, showing how to print out a list of the
36+
* current directory's <i>un-writable</i> files:
37+
*
38+
* <pre>
39+
* File dir = new File(".");
40+
* String[] files = dir.list( CanWriteFileFilter.CANNOT_WRITE );
41+
* for ( int i = 0; i &lt; files.length; i++ ) {
42+
* System.out.println(files[i]);
43+
* }
44+
* </pre>
45+
*
46+
* <p>
47+
* <b>N.B.</b> For read-only files, use
48+
* <code>CanReadFileFilter.READ_ONLY</code>.
49+
*
50+
* @since Commons IO 1.3
51+
* @version $Revision$
52+
*/
53+
public class CanWriteFileFilter extends AbstractFileFilter {
54+
55+
/** Singleton instance of <i>writable</i> filter */
56+
public static final IOFileFilter CAN_WRITE = new CanWriteFileFilter();
57+
58+
/** Singleton instance of not <i>writable</i> filter */
59+
public static final IOFileFilter CANNOT_WRITE = new NotFileFilter(CAN_WRITE);
60+
61+
/**
62+
* Restrictive consructor.
63+
*/
64+
protected CanWriteFileFilter() {
65+
}
66+
67+
/**
68+
* Checks to see if the file can be written to.
69+
*
70+
* @param file the File to check
71+
* @return <code>true</code> if the file can be
72+
* written to, otherwise <code>false</code>.
73+
*/
74+
public boolean accept(File file) {
75+
return file.canWrite();
76+
}
77+
78+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2005-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 files or directories that are empty.
22+
* <p>
23+
* If the <code>File</code> is a directory it checks that
24+
* it contains no files.
25+
* <p>
26+
* Example, showing how to print out a list of the
27+
* current directory's empty files/directories:
28+
*
29+
* <pre>
30+
* File dir = new File(".");
31+
* String[] files = dir.list( EmptyFileFilter.EMPTY );
32+
* for ( int i = 0; i &lt; files.length; i++ ) {
33+
* System.out.println(files[i]);
34+
* }
35+
* </pre>
36+
*
37+
* <p>
38+
* Example, showing how to print out a list of the
39+
* current directory's non-empty files/directories:
40+
*
41+
* <pre>
42+
* File dir = new File(".");
43+
* String[] files = dir.list( EmptyFileFilter.NOT_EMPTY );
44+
* for ( int i = 0; i &lt; files.length; i++ ) {
45+
* System.out.println(files[i]);
46+
* }
47+
* </pre>
48+
*
49+
* @since Commons IO 1.3
50+
* @version $Revision$
51+
*/
52+
public class EmptyFileFilter extends AbstractFileFilter {
53+
54+
/** Singleton instance of <i>empty</i> filter */
55+
public static final IOFileFilter EMPTY = new EmptyFileFilter();
56+
57+
/** Singleton instance of <i>not-empty</i> filter */
58+
public static final IOFileFilter NOT_EMPTY = new NotFileFilter(EMPTY);
59+
60+
/**
61+
* Restrictive consructor.
62+
*/
63+
protected EmptyFileFilter() {
64+
}
65+
66+
/**
67+
* Checks to see if the file is empty.
68+
*
69+
* @param file the file or directory to check
70+
* @return <code>true</code> if the file or directory
71+
* is <i>empty</i>, otherwise <code>false</code>.
72+
*/
73+
public boolean accept(File file) {
74+
if (file.isDirectory()) {
75+
File[] files = file.listFiles();
76+
return (files == null || files.length == 0);
77+
} else {
78+
return (file.length() == 0);
79+
}
80+
}
81+
82+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2005-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 hidden.
22+
* <p>
23+
* Example, showing how to print out a list of the
24+
* current directory's <i>hidden</i> files:
25+
*
26+
* <pre>
27+
* File dir = new File(".");
28+
* String[] files = dir.list( HiddenFileFilter.HIDDEN );
29+
* for ( int i = 0; i &lt; files.length; i++ ) {
30+
* System.out.println(files[i]);
31+
* }
32+
* </pre>
33+
*
34+
* <p>
35+
* Example, showing how to print out a list of the
36+
* current directory's <i>visible</i> (i.e. not hidden) files:
37+
*
38+
* <pre>
39+
* File dir = new File(".");
40+
* String[] files = dir.list( HiddenFileFilter.VISIBLE );
41+
* for ( int i = 0; i &lt; files.length; i++ ) {
42+
* System.out.println(files[i]);
43+
* }
44+
* </pre>
45+
*
46+
* @since Commons IO 1.3
47+
* @version $Revision$
48+
*/
49+
public class HiddenFileFilter extends AbstractFileFilter {
50+
51+
/** Singleton instance of <i>hidden</i> filter */
52+
public static final IOFileFilter HIDDEN = new HiddenFileFilter();
53+
54+
/** Singleton instance of <i>visible</i> filter */
55+
public static final IOFileFilter VISIBLE = new NotFileFilter(HIDDEN);
56+
57+
/**
58+
* Restrictive consructor.
59+
*/
60+
protected HiddenFileFilter() {
61+
}
62+
63+
/**
64+
* Checks to see if the file is hidden.
65+
*
66+
* @param file the File to check
67+
* @return <code>true</code> if the file is
68+
* <i>hidden</i>, otherwise <code>false</code>.
69+
*/
70+
public boolean accept(File file) {
71+
return file.isHidden();
72+
}
73+
74+
}

0 commit comments

Comments
 (0)