2 * Licensed to the Apache Software Foundation (ASF) under one or more
\r
3 * contributor license agreements. See the NOTICE file distributed with
\r
4 * this work for additional information regarding copyright ownership.
\r
5 * The ASF licenses this file to You under the Apache License, Version 2.0
\r
6 * (the "License"); you may not use this file except in compliance with
\r
7 * the License. You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
17 package org.apache.commons.io.filefilter;
\r
19 import java.io.File;
\r
20 import java.io.Serializable;
\r
21 import java.util.List;
\r
23 import org.apache.commons.io.IOCase;
\r
26 * Filters filenames for a certain prefix.
\r
28 * For example, to print all files and directories in the
\r
29 * current directory whose name starts with <code>Test</code>:
\r
32 * File dir = new File(".");
\r
33 * String[] files = dir.list( new PrefixFileFilter("Test") );
\r
34 * for ( int i = 0; i < files.length; i++ ) {
\r
35 * System.out.println(files[i]);
\r
39 * @since Commons IO 1.0
\r
40 * @version $Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $
\r
42 * @author Stephen Colebourne
\r
43 * @author Federico Barbieri
\r
44 * @author Serge Knystautas
\r
45 * @author Peter Donald
\r
47 public class PrefixFileFilter extends AbstractFileFilter implements Serializable {
\r
49 /** The filename prefixes to search for */
\r
50 private final String[] prefixes;
\r
52 /** Whether the comparison is case sensitive. */
\r
53 private final IOCase caseSensitivity;
\r
56 * Constructs a new Prefix file filter for a single prefix.
\r
58 * @param prefix the prefix to allow, must not be null
\r
59 * @throws IllegalArgumentException if the prefix is null
\r
61 public PrefixFileFilter(String prefix) {
\r
62 this(prefix, IOCase.SENSITIVE);
\r
66 * Constructs a new Prefix file filter for a single prefix
\r
67 * specifying case-sensitivity.
\r
69 * @param prefix the prefix to allow, must not be null
\r
70 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
\r
71 * @throws IllegalArgumentException if the prefix is null
\r
72 * @since Commons IO 1.4
\r
74 public PrefixFileFilter(String prefix, IOCase caseSensitivity) {
\r
75 if (prefix == null) {
\r
76 throw new IllegalArgumentException("The prefix must not be null");
\r
78 this.prefixes = new String[] {prefix};
\r
79 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
\r
83 * Constructs a new Prefix file filter for any of an array of prefixes.
\r
85 * The array is not cloned, so could be changed after constructing the
\r
86 * instance. This would be inadvisable however.
\r
88 * @param prefixes the prefixes to allow, must not be null
\r
89 * @throws IllegalArgumentException if the prefix array is null
\r
91 public PrefixFileFilter(String[] prefixes) {
\r
92 this(prefixes, IOCase.SENSITIVE);
\r
96 * Constructs a new Prefix file filter for any of an array of prefixes
\r
97 * specifying case-sensitivity.
\r
99 * The array is not cloned, so could be changed after constructing the
\r
100 * instance. This would be inadvisable however.
\r
102 * @param prefixes the prefixes to allow, must not be null
\r
103 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
\r
104 * @throws IllegalArgumentException if the prefix is null
\r
105 * @since Commons IO 1.4
\r
107 public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {
\r
108 if (prefixes == null) {
\r
109 throw new IllegalArgumentException("The array of prefixes must not be null");
\r
111 this.prefixes = prefixes;
\r
112 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
\r
116 * Constructs a new Prefix file filter for a list of prefixes.
\r
118 * @param prefixes the prefixes to allow, must not be null
\r
119 * @throws IllegalArgumentException if the prefix list is null
\r
120 * @throws ClassCastException if the list does not contain Strings
\r
122 public PrefixFileFilter(List prefixes) {
\r
123 this(prefixes, IOCase.SENSITIVE);
\r
127 * Constructs a new Prefix file filter for a list of prefixes
\r
128 * specifying case-sensitivity.
\r
130 * @param prefixes the prefixes to allow, must not be null
\r
131 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
\r
132 * @throws IllegalArgumentException if the prefix list is null
\r
133 * @throws ClassCastException if the list does not contain Strings
\r
134 * @since Commons IO 1.4
\r
136 public PrefixFileFilter(List prefixes, IOCase caseSensitivity) {
\r
137 if (prefixes == null) {
\r
138 throw new IllegalArgumentException("The list of prefixes must not be null");
\r
140 this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);
\r
141 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
\r
145 * Checks to see if the filename starts with the prefix.
\r
147 * @param file the File to check
\r
148 * @return true if the filename starts with one of our prefixes
\r
150 public boolean accept(File file) {
\r
151 String name = file.getName();
\r
152 for (int i = 0; i < this.prefixes.length; i++) {
\r
153 if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
\r
161 * Checks to see if the filename starts with the prefix.
\r
163 * @param file the File directory
\r
164 * @param name the filename
\r
165 * @return true if the filename starts with one of our prefixes
\r
167 public boolean accept(File file, String name) {
\r
168 for (int i = 0; i < prefixes.length; i++) {
\r
169 if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
\r
177 * Provide a String representaion of this file filter.
\r
179 * @return a String representaion
\r
181 public String toString() {
\r
182 StringBuffer buffer = new StringBuffer();
\r
183 buffer.append(super.toString());
\r
184 buffer.append("(");
\r
185 if (prefixes != null) {
\r
186 for (int i = 0; i < prefixes.length; i++) {
\r
188 buffer.append(",");
\r
190 buffer.append(prefixes[i]);
\r
193 buffer.append(")");
\r
194 return buffer.toString();
\r