KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > filefilter > NameFileFilter


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.io.filefilter;
17
18 import java.io.File JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * Filters filenames for a certain name.
23  * <p>
24  * For example, to print all files and directories in the
25  * current directory whose name is <code>Test</code>:
26  *
27  * <pre>
28  * File dir = new File(".");
29  * String[] files = dir.list( new NameFileFilter("Test") );
30  * for ( int i = 0; i &lt; files.length; i++ ) {
31  * System.out.println(files[i]);
32  * }
33  * </pre>
34  *
35  * @since Commons IO 1.0
36  * @version $Revision: 1.3 $ $Date: 2004/02/23 04:37:57 $
37  *
38  * @author Henri Yandell
39  * @author Stephen Colebourne
40  * @author Federico Barbieri
41  * @author Serge Knystautas
42  * @author Peter Donald
43  */

44 public class NameFileFilter extends AbstractFileFilter {
45     
46     /** The filenames to search for */
47     private String JavaDoc[] names;
48
49     /**
50      * Constructs a new name file filter for a single name.
51      *
52      * @param name the name to allow, must not be null
53      * @throws IllegalArgumentException if the prefix is null
54      */

55     public NameFileFilter(String JavaDoc name) {
56         if (name == null) {
57             throw new IllegalArgumentException JavaDoc("The name must not be null");
58         }
59         this.names = new String JavaDoc[] {name};
60     }
61
62     /**
63      * Constructs a new name file filter for any of an array of names.
64      * <p>
65      * The array is not cloned, so could be changed after constructing the
66      * instance. This would be inadvisable however.
67      *
68      * @param names the names to allow, must not be null
69      * @throws IllegalArgumentException if the names array is null
70      */

71     public NameFileFilter(String JavaDoc[] names) {
72         if (names == null) {
73             throw new IllegalArgumentException JavaDoc("The array of names must not be null");
74         }
75         this.names = names;
76     }
77
78     /**
79      * Constructs a new name file filter for a list of names.
80      *
81      * @param names the names to allow, must not be null
82      * @throws IllegalArgumentException if the name list is null
83      * @throws ClassCastException if the list does not contain Strings
84      */

85     public NameFileFilter(List JavaDoc names) {
86         if (names == null) {
87             throw new IllegalArgumentException JavaDoc("The list of names must not be null");
88         }
89         this.names = (String JavaDoc[]) names.toArray(new String JavaDoc[names.size()]);
90     }
91
92     /**
93      * Checks to see if the filename matches.
94      *
95      * @param file the File to check
96      * @return true if the filename matches
97      */

98     public boolean accept(File JavaDoc file) {
99         String JavaDoc name = file.getName();
100         for (int i = 0; i < this.names.length; i++) {
101             if (name.equals(this.names[i])) {
102                 return true;
103             }
104         }
105         return false;
106     }
107     
108     /**
109      * Checks to see if the filename matches.
110      *
111      * @param file the File directory
112      * @param name the filename
113      * @return true if the filename matches
114      */

115     public boolean accept(File JavaDoc file, String JavaDoc name) {
116         for (int i = 0; i < this.names.length; i++) {
117             if (name.equals(this.names[i])) {
118                 return true;
119             }
120         }
121         return false;
122     }
123     
124 }
125
Popular Tags