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 */
17package org.apache.commons.io.filefilter;
18
19import java.io.File;
20import java.io.Serializable;
21import java.util.List;
22
23import org.apache.commons.io.IOCase;
24
25/**
26 * Filters filenames for a certain name.
27 * <p>
28 * For example, to print all files and directories in the
29 * current directory whose name is <code>Test</code>:
30 *
31 * <pre>
32 * File dir = new File(".");
33 * String[] files = dir.list( new NameFileFilter("Test") );
34 * for ( int i = 0; i &lt; files.length; i++ ) {
35 *     System.out.println(files[i]);
36 * }
37 * </pre>
38 *
39 * @since Commons IO 1.0
40 * @version $Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $
41 *
42 * @author Stephen Colebourne
43 * @author Federico Barbieri
44 * @author Serge Knystautas
45 * @author Peter Donald
46 */
47public class NameFileFilter extends AbstractFileFilter implements Serializable {
48
49    /** The filenames to search for */
50    private final String[] names;
51    /** Whether the comparison is case sensitive. */
52    private final IOCase caseSensitivity;
53
54    /**
55     * Constructs a new case-sensitive name file filter for a single name.
56     *
57     * @param name  the name to allow, must not be null
58     * @throws IllegalArgumentException if the name is null
59     */
60    public NameFileFilter(String name) {
61        this(name, null);
62    }
63
64    /**
65     * Construct a new name file filter specifying case-sensitivity.
66     *
67     * @param name  the name to allow, must not be null
68     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
69     * @throws IllegalArgumentException if the name is null
70     */
71    public NameFileFilter(String name, IOCase caseSensitivity) {
72        if (name == null) {
73            throw new IllegalArgumentException("The wildcard must not be null");
74        }
75        this.names = new String[] {name};
76        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
77    }
78
79    /**
80     * Constructs a new case-sensitive name file filter for an array of names.
81     * <p>
82     * The array is not cloned, so could be changed after constructing the
83     * instance. This would be inadvisable however.
84     *
85     * @param names  the names to allow, must not be null
86     * @throws IllegalArgumentException if the names array is null
87     */
88    public NameFileFilter(String[] names) {
89        this(names, null);
90    }
91
92    /**
93     * Constructs a new name file filter for an array of names specifying case-sensitivity.
94     * <p>
95     * The array is not cloned, so could be changed after constructing the
96     * instance. This would be inadvisable however.
97     *
98     * @param names  the names to allow, must not be null
99     * @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<String> 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<String> names, IOCase caseSensitivity) {
130        if (names == null) {
131            throw new IllegalArgumentException("The list of names must not be null");
132        }
133        this.names = 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    @Override
145    public boolean accept(File file) {
146        String name = file.getName();
147        for (int i = 0; i < this.names.length; i++) {
148            if (caseSensitivity.checkEquals(name, names[i])) {
149                return true;
150            }
151        }
152        return false;
153    }
154
155    /**
156     * Checks to see if the filename matches.
157     *
158     * @param file  the File directory
159     * @param name  the filename
160     * @return true if the filename matches
161     */
162    @Override
163    public boolean accept(File file, String name) {
164        for (int i = 0; i < names.length; i++) {
165            if (caseSensitivity.checkEquals(name, names[i])) {
166                return true;
167            }
168        }
169        return false;
170    }
171
172    /**
173     * Provide a String representaion of this file filter.
174     *
175     * @return a String representaion
176     */
177    @Override
178    public String toString() {
179        StringBuffer buffer = new StringBuffer();
180        buffer.append(super.toString());
181        buffer.append("(");
182        if (names != null) {
183            for (int i = 0; i < names.length; i++) {
184                if (i > 0) {
185                    buffer.append(",");
186                }
187                buffer.append(names[i]);
188            }
189        }
190        buffer.append(")");
191        return buffer.toString();
192    }
193
194}
195