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