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