1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.io.filefilter;
18
19 import java.io.File;
20 import java.util.List;
21
22 /**
23 * Filters filenames for a certain prefix.
24 * <p>
25 * For example, to print all files and directories in the
26 * current directory whose name starts with <code>Test</code>:
27 *
28 * <pre>
29 * File dir = new File(".");
30 * String[] files = dir.list( new PrefixFileFilter("Test") );
31 * for ( int i = 0; i < files.length; i++ ) {
32 * System.out.println(files[i]);
33 * }
34 * </pre>
35 *
36 * @since Commons IO 1.0
37 * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
38 *
39 * @author Stephen Colebourne
40 * @author Federico Barbieri
41 * @author Serge Knystautas
42 * @author Peter Donald
43 */
44 public class PrefixFileFilter extends AbstractFileFilter {
45
46 /** The filename prefixes to search for */
47 private String[] prefixes;
48
49 /**
50 * Constructs a new Prefix file filter for a single prefix.
51 *
52 * @param prefix the prefix to allow, must not be null
53 * @throws IllegalArgumentException if the prefix is null
54 */
55 public PrefixFileFilter(String prefix) {
56 if (prefix == null) {
57 throw new IllegalArgumentException("The prefix must not be null");
58 }
59 this.prefixes = new String[] {prefix};
60 }
61
62 /**
63 * Constructs a new Prefix file filter for any of an array of prefixes.
64 * <p>
65 * The array is not cloned, so could be changed after constructing the
66 * instance. This would be inadvisable however.
67 *
68 * @param prefixes the prefixes to allow, must not be null
69 * @throws IllegalArgumentException if the prefix array is null
70 */
71 public PrefixFileFilter(String[] prefixes) {
72 if (prefixes == null) {
73 throw new IllegalArgumentException("The array of prefixes must not be null");
74 }
75 this.prefixes = prefixes;
76 }
77
78 /**
79 * Constructs a new Prefix file filter for a list of prefixes.
80 *
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
84 */
85 public PrefixFileFilter(List prefixes) {
86 if (prefixes == null) {
87 throw new IllegalArgumentException("The list of prefixes must not be null");
88 }
89 this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);
90 }
91
92 /**
93 * Checks to see if the filename starts with the prefix.
94 *
95 * @param file the File to check
96 * @return true if the filename starts with one of our prefixes
97 */
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])) {
102 return true;
103 }
104 }
105 return false;
106 }
107
108 /**
109 * Checks to see if the filename starts with the prefix.
110 *
111 * @param file the File directory
112 * @param name the filename
113 * @return true if the filename starts with one of our prefixes
114 */
115 public boolean accept(File file, String name) {
116 for (int i = 0; i < prefixes.length; i++) {
117 if (name.startsWith(prefixes[i])) {
118 return true;
119 }
120 }
121 return false;
122 }
123
124 }