KickJava   Java API By Example, From Geeks To Geeks.

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


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 files based on the suffix (what the filename ends with).
23  * This is used in retrieving all the files of a particular type.
24  * <p>
25  * For example, to retrieve and print all <code>*.java</code> files
26  * in the current directory:
27  *
28  * <pre>
29  * File dir = new File(".");
30  * String[] files = dir.list( new SuffixFileFilter(".java") );
31  * for (int i = 0; i &lt; files.length; i++) {
32  * System.out.println(files[i]);
33  * }
34  * </pre>
35  *
36  * @since Commons IO 1.0
37  * @version $Revision: 1.6 $ $Date: 2004/02/23 04:37:57 $
38  *
39  * @author Henri Yandell
40  * @author Stephen Colebourne
41  * @author Federico Barbieri
42  * @author Serge Knystautas
43  * @author Peter Donald
44  */

45 public class SuffixFileFilter extends AbstractFileFilter {
46     
47     /** The filename suffixes to search for */
48     private String JavaDoc[] suffixes;
49
50     /**
51      * Constructs a new Suffix file filter for a single extension.
52      *
53      * @param suffix the suffix to allow, must not be null
54      * @throws IllegalArgumentException if the suffix is null
55      */

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

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

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

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

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