001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.io.filefilter;
018
019 import java.io.File;
020 import java.io.Serializable;
021 import java.util.List;
022
023 import org.apache.commons.io.IOCase;
024
025 /**
026 * Filters filenames for a certain prefix.
027 * <p>
028 * For example, to print all files and directories in the
029 * current directory whose name starts with <code>Test</code>:
030 *
031 * <pre>
032 * File dir = new File(".");
033 * String[] files = dir.list( new PrefixFileFilter("Test") );
034 * for ( int i = 0; i < files.length; i++ ) {
035 * System.out.println(files[i]);
036 * }
037 * </pre>
038 *
039 * @since Commons IO 1.0
040 * @version $Revision: 1005099 $ $Date: 2010-10-06 17:13:01 +0100 (Wed, 06 Oct 2010) $
041 *
042 * @author Stephen Colebourne
043 * @author Federico Barbieri
044 * @author Serge Knystautas
045 * @author Peter Donald
046 * @see FileFilterUtils#prefixFileFilter(String)
047 * @see FileFilterUtils#prefixFileFilter(String, IOCase)
048 */
049 public class PrefixFileFilter extends AbstractFileFilter implements Serializable {
050
051 /** The filename prefixes to search for */
052 private final String[] prefixes;
053
054 /** Whether the comparison is case sensitive. */
055 private final IOCase caseSensitivity;
056
057 /**
058 * Constructs a new Prefix file filter for a single prefix.
059 *
060 * @param prefix the prefix to allow, must not be null
061 * @throws IllegalArgumentException if the prefix is null
062 */
063 public PrefixFileFilter(String prefix) {
064 this(prefix, IOCase.SENSITIVE);
065 }
066
067 /**
068 * Constructs a new Prefix file filter for a single prefix
069 * specifying case-sensitivity.
070 *
071 * @param prefix the prefix to allow, must not be null
072 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
073 * @throws IllegalArgumentException if the prefix is null
074 * @since Commons IO 1.4
075 */
076 public PrefixFileFilter(String prefix, IOCase caseSensitivity) {
077 if (prefix == null) {
078 throw new IllegalArgumentException("The prefix must not be null");
079 }
080 this.prefixes = new String[] {prefix};
081 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
082 }
083
084 /**
085 * Constructs a new Prefix file filter for any of an array of prefixes.
086 * <p>
087 * The array is not cloned, so could be changed after constructing the
088 * instance. This would be inadvisable however.
089 *
090 * @param prefixes the prefixes to allow, must not be null
091 * @throws IllegalArgumentException if the prefix array is null
092 */
093 public PrefixFileFilter(String[] prefixes) {
094 this(prefixes, IOCase.SENSITIVE);
095 }
096
097 /**
098 * Constructs a new Prefix file filter for any of an array of prefixes
099 * specifying case-sensitivity.
100 * <p>
101 * The array is not cloned, so could be changed after constructing the
102 * instance. This would be inadvisable however.
103 *
104 * @param prefixes the prefixes to allow, must not be null
105 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
106 * @throws IllegalArgumentException if the prefix is null
107 * @since Commons IO 1.4
108 */
109 public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {
110 if (prefixes == null) {
111 throw new IllegalArgumentException("The array of prefixes must not be null");
112 }
113 this.prefixes = new String[prefixes.length];
114 System.arraycopy(prefixes, 0, this.prefixes, 0, prefixes.length);
115 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
116 }
117
118 /**
119 * Constructs a new Prefix file filter for a list of prefixes.
120 *
121 * @param prefixes the prefixes to allow, must not be null
122 * @throws IllegalArgumentException if the prefix list is null
123 * @throws ClassCastException if the list does not contain Strings
124 */
125 public PrefixFileFilter(List<String> prefixes) {
126 this(prefixes, IOCase.SENSITIVE);
127 }
128
129 /**
130 * Constructs a new Prefix file filter for a list of prefixes
131 * specifying case-sensitivity.
132 *
133 * @param prefixes the prefixes to allow, must not be null
134 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
135 * @throws IllegalArgumentException if the prefix list is null
136 * @throws ClassCastException if the list does not contain Strings
137 * @since Commons IO 1.4
138 */
139 public PrefixFileFilter(List<String> prefixes, IOCase caseSensitivity) {
140 if (prefixes == null) {
141 throw new IllegalArgumentException("The list of prefixes must not be null");
142 }
143 this.prefixes = prefixes.toArray(new String[prefixes.size()]);
144 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
145 }
146
147 /**
148 * Checks to see if the filename starts with the prefix.
149 *
150 * @param file the File to check
151 * @return true if the filename starts with one of our prefixes
152 */
153 @Override
154 public boolean accept(File file) {
155 String name = file.getName();
156 for (String prefix : this.prefixes) {
157 if (caseSensitivity.checkStartsWith(name, prefix)) {
158 return true;
159 }
160 }
161 return false;
162 }
163
164 /**
165 * Checks to see if the filename starts with the prefix.
166 *
167 * @param file the File directory
168 * @param name the filename
169 * @return true if the filename starts with one of our prefixes
170 */
171 @Override
172 public boolean accept(File file, String name) {
173 for (String prefix : prefixes) {
174 if (caseSensitivity.checkStartsWith(name, prefix)) {
175 return true;
176 }
177 }
178 return false;
179 }
180
181 /**
182 * Provide a String representaion of this file filter.
183 *
184 * @return a String representaion
185 */
186 @Override
187 public String toString() {
188 StringBuilder buffer = new StringBuilder();
189 buffer.append(super.toString());
190 buffer.append("(");
191 if (prefixes != null) {
192 for (int i = 0; i < prefixes.length; i++) {
193 if (i > 0) {
194 buffer.append(",");
195 }
196 buffer.append(prefixes[i]);
197 }
198 }
199 buffer.append(")");
200 return buffer.toString();
201 }
202
203 }