View Javadoc

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   */
17  package org.apache.commons.io.filefilter;
18  
19  import java.io.File;
20  import java.util.List;
21  
22  import org.apache.commons.io.IOCase;
23  
24  /**
25   * Filters filenames for a certain name.
26   * <p>
27   * For example, to print all files and directories in the 
28   * current directory whose name is <code>Test</code>:
29   *
30   * <pre>
31   * File dir = new File(".");
32   * String[] files = dir.list( new NameFileFilter("Test") );
33   * for ( int i = 0; i &lt; files.length; i++ ) {
34   *     System.out.println(files[i]);
35   * }
36   * </pre>
37   *
38   * @since Commons IO 1.0
39   * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
40   * 
41   * @author Stephen Colebourne
42   * @author Federico Barbieri
43   * @author Serge Knystautas
44   * @author Peter Donald
45   */
46  public class NameFileFilter extends AbstractFileFilter {
47      
48      /** The filenames to search for */
49      private String[] names;
50      /** Whether the comparison is case sensitive. */
51      private IOCase caseSensitivity;
52  
53      /**
54       * Constructs a new case-sensitive name file filter for a single name.
55       * 
56       * @param name  the name to allow, must not be null
57       * @throws IllegalArgumentException if the name is null
58       */
59      public NameFileFilter(String name) {
60          this(name, null);
61      }
62  
63      /**
64       * Construct a new name file filter specifying case-sensitivity.
65       *
66       * @param name  the name to allow, must not be null
67       * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
68       * @throws IllegalArgumentException if the name is null
69       */
70      public NameFileFilter(String name, IOCase caseSensitivity) {
71          if (name == null) {
72              throw new IllegalArgumentException("The wildcard must not be null");
73          }
74          this.names = new String[] {name};
75          this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
76      }
77  
78      /**
79       * Constructs a new case-sensitive name file filter for an array of names.
80       * <p>
81       * The array is not cloned, so could be changed after constructing the
82       * instance. This would be inadvisable however.
83       * 
84       * @param names  the names to allow, must not be null
85       * @throws IllegalArgumentException if the names array is null
86       */
87      public NameFileFilter(String[] names) {
88          this(names, null);
89      }
90  
91      /**
92       * Constructs a new name file filter for an array of names specifying case-sensitivity.
93       * <p>
94       * The array is not cloned, so could be changed after constructing the
95       * instance. This would be inadvisable however.
96       * 
97       * @param names  the names to allow, must not be null
98       * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
99       * @throws IllegalArgumentException if the names array is null
100      */
101     public NameFileFilter(String[] names, IOCase caseSensitivity) {
102         if (names == null) {
103             throw new IllegalArgumentException("The array of names must not be null");
104         }
105         this.names = names;
106         this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
107     }
108 
109     /**
110      * Constructs a new case-sensitive name file filter for a list of names.
111      * 
112      * @param names  the names to allow, must not be null
113      * @throws IllegalArgumentException if the name list is null
114      * @throws ClassCastException if the list does not contain Strings
115      */
116     public NameFileFilter(List names) {
117         this(names, null);
118     }
119 
120     /**
121      * Constructs a new name file filter for a list of names specifying case-sensitivity.
122      * 
123      * @param names  the names to allow, must not be null
124      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
125      * @throws IllegalArgumentException if the name list is null
126      * @throws ClassCastException if the list does not contain Strings
127      */
128     public NameFileFilter(List names, IOCase caseSensitivity) {
129         if (names == null) {
130             throw new IllegalArgumentException("The list of names must not be null");
131         }
132         this.names = (String[]) names.toArray(new String[names.size()]);
133         this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
134     }
135 
136     //-----------------------------------------------------------------------
137     /**
138      * Checks to see if the filename matches.
139      * 
140      * @param file  the File to check
141      * @return true if the filename matches
142      */
143     public boolean accept(File file) {
144         String name = file.getName();
145         for (int i = 0; i < this.names.length; i++) {
146             if (caseSensitivity.checkEquals(name, names[i])) {
147                 return true;
148             }
149         }
150         return false;
151     }
152 
153     /**
154      * Checks to see if the filename matches.
155      * 
156      * @param file  the File directory
157      * @param name  the filename
158      * @return true if the filename matches
159      */
160     public boolean accept(File file, String name) {
161         for (int i = 0; i < names.length; i++) {
162             if (caseSensitivity.checkEquals(name, names[i])) {
163                 return true;
164             }
165         }
166         return false;
167     }
168 
169 }