2
* Copyright 2002-2004 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package org.apache.commons.io.filefilter;
19
import java.util.List;
22
* Filters filenames for a certain prefix.
24
* For example, to print all files and directories in the
25
* current directory whose name starts with <code>Test</code>:
28
* File dir = new File(".");
29
* String[] files = dir.list( new PrefixFileFilter("Test") );
30
* for ( int i = 0; i < files.length; i++ ) {
31
* System.out.println(files[i]);
35
* @since Commons IO 1.0
36
* @version $Revision: 1.8 $ $Date: 2004/02/23 04:37:57 $
38
* @author Henri Yandell
39
* @author Stephen Colebourne
40
* @author Federico Barbieri
41
* @author Serge Knystautas
42
* @author Peter Donald
44
public class PrefixFileFilter extends AbstractFileFilter {
46
/** The filename prefixes to search for */
47
private String[] prefixes;
50
* Constructs a new Prefix file filter for a single prefix.
52
* @param prefix the prefix to allow, must not be null
53
* @throws IllegalArgumentException if the prefix is null
55
public PrefixFileFilter(String prefix) {
57
throw new IllegalArgumentException("The prefix must not be null");
59
this.prefixes = new String[] {prefix};
63
* Constructs a new Prefix file filter for any of an array of prefixes.
65
* The array is not cloned, so could be changed after constructing the
66
* instance. This would be inadvisable however.
68
* @param prefixes the prefixes to allow, must not be null
69
* @throws IllegalArgumentException if the prefix array is null
71
public PrefixFileFilter(String[] prefixes) {
72
if (prefixes == null) {
73
throw new IllegalArgumentException("The array of prefixes must not be null");
75
this.prefixes = prefixes;
79
* Constructs a new Prefix file filter for a list of prefixes.
81
* @param prefixes the prefixes to allow, must not be null
82
* @throws IllegalArgumentException if the prefix list is null
83
* @throws ClassCastException if the list does not contain Strings
85
public PrefixFileFilter(List prefixes) {
86
if (prefixes == null) {
87
throw new IllegalArgumentException("The list of prefixes must not be null");
89
this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);
93
* Checks to see if the filename starts with the prefix.
95
* @param file the File to check
96
* @return true if the filename starts with one of our prefixes
98
public boolean accept(File file) {
99
String name = file.getName();
100
for (int i = 0; i < this.prefixes.length; i++) {
101
if (name.startsWith(this.prefixes[i])) {
109
* Checks to see if the filename starts with the prefix.
111
* @param file the File directory
112
* @param name the filename
113
* @return true if the filename starts with one of our prefixes
115
public boolean accept(File file, String name) {
116
for (int i = 0; i < prefixes.length; i++) {
117
if (name.startsWith(prefixes[i])) {