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 name.
027 * <p>
028 * For example, to print all files and directories in the
029 * current directory whose name is <code>Test</code>:
030 *
031 * <pre>
032 * File dir = new File(".");
033 * String[] files = dir.list( new NameFileFilter("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#nameFileFilter(String)
047 * @see FileFilterUtils#nameFileFilter(String, IOCase)
048 */
049 public class NameFileFilter extends AbstractFileFilter implements Serializable {
050
051 /** The filenames to search for */
052 private final String[] names;
053 /** Whether the comparison is case sensitive. */
054 private final IOCase caseSensitivity;
055
056 /**
057 * Constructs a new case-sensitive name file filter for a single name.
058 *
059 * @param name the name to allow, must not be null
060 * @throws IllegalArgumentException if the name is null
061 */
062 public NameFileFilter(String name) {
063 this(name, null);
064 }
065
066 /**
067 * Construct a new name file filter specifying case-sensitivity.
068 *
069 * @param name the name to allow, must not be null
070 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
071 * @throws IllegalArgumentException if the name is null
072 */
073 public NameFileFilter(String name, IOCase caseSensitivity) {
074 if (name == null) {
075 throw new IllegalArgumentException("The wildcard must not be null");
076 }
077 this.names = new String[] {name};
078 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
079 }
080
081 /**
082 * Constructs a new case-sensitive name file filter for an array of names.
083 * <p>
084 * The array is not cloned, so could be changed after constructing the
085 * instance. This would be inadvisable however.
086 *
087 * @param names the names to allow, must not be null
088 * @throws IllegalArgumentException if the names array is null
089 */
090 public NameFileFilter(String[] names) {
091 this(names, null);
092 }
093
094 /**
095 * Constructs a new name file filter for an array of names specifying case-sensitivity.
096 * <p>
097 * The array is not cloned, so could be changed after constructing the
098 * instance. This would be inadvisable however.
099 *
100 * @param names the names to allow, must not be null
101 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
102 * @throws IllegalArgumentException if the names array is null
103 */
104 public NameFileFilter(String[] names, IOCase caseSensitivity) {
105 if (names == null) {
106 throw new IllegalArgumentException("The array of names must not be null");
107 }
108 this.names = new String[names.length];
109 System.arraycopy(names, 0, this.names, 0, names.length);
110 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
111 }
112
113 /**
114 * Constructs a new case-sensitive name file filter for a list of names.
115 *
116 * @param names the names to allow, must not be null
117 * @throws IllegalArgumentException if the name list is null
118 * @throws ClassCastException if the list does not contain Strings
119 */
120 public NameFileFilter(List<String> names) {
121 this(names, null);
122 }
123
124 /**
125 * Constructs a new name file filter for a list of names specifying case-sensitivity.
126 *
127 * @param names the names to allow, must not be null
128 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
129 * @throws IllegalArgumentException if the name list is null
130 * @throws ClassCastException if the list does not contain Strings
131 */
132 public NameFileFilter(List<String> names, IOCase caseSensitivity) {
133 if (names == null) {
134 throw new IllegalArgumentException("The list of names must not be null");
135 }
136 this.names = names.toArray(new String[names.size()]);
137 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
138 }
139
140 //-----------------------------------------------------------------------
141 /**
142 * Checks to see if the filename matches.
143 *
144 * @param file the File to check
145 * @return true if the filename matches
146 */
147 @Override
148 public boolean accept(File file) {
149 String name = file.getName();
150 for (String name2 : this.names) {
151 if (caseSensitivity.checkEquals(name, name2)) {
152 return true;
153 }
154 }
155 return false;
156 }
157
158 /**
159 * Checks to see if the filename matches.
160 *
161 * @param file the File directory
162 * @param name the filename
163 * @return true if the filename matches
164 */
165 @Override
166 public boolean accept(File file, String name) {
167 for (String name2 : names) {
168 if (caseSensitivity.checkEquals(name, name2)) {
169 return true;
170 }
171 }
172 return false;
173 }
174
175 /**
176 * Provide a String representaion of this file filter.
177 *
178 * @return a String representaion
179 */
180 @Override
181 public String toString() {
182 StringBuilder buffer = new StringBuilder();
183 buffer.append(super.toString());
184 buffer.append("(");
185 if (names != null) {
186 for (int i = 0; i < names.length; i++) {
187 if (i > 0) {
188 buffer.append(",");
189 }
190 buffer.append(names[i]);
191 }
192 }
193 buffer.append(")");
194 return buffer.toString();
195 }
196
197 }