KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileFilter JavaDoc;
19 import java.io.FilenameFilter JavaDoc;
20
21 /**
22  * Useful utilities for working with file filters. It provides access to all
23  * file filter implementations in this package so you don't have to import
24  * every class you use.
25  *
26  * @since Commons IO 1.0
27  * @version $Revision: 1.8 $ $Date: 2004/02/23 04:37:57 $
28  *
29  * @author Henri Yandell
30  * @author Stephen Colebourne
31  * @author Jeremias Maerki
32  */

33 public class FileFilterUtils {
34     
35     /**
36      * FileFilterUtils is not normally instantiated.
37      */

38     public FileFilterUtils() {
39     }
40
41     //-----------------------------------------------------------------------
42
/**
43      * Returns a filter that returns true if the filename starts with the specified text.
44      *
45      * @param prefix the filename prefix
46      * @return a prefix checking filter
47      */

48     public static IOFileFilter prefixFileFilter(String JavaDoc prefix) {
49         return new PrefixFileFilter(prefix);
50     }
51
52     /**
53      * Returns a filter that returns true if the filename ends with the specified text.
54      *
55      * @param suffix the filename suffix
56      * @return a suffix checking filter
57      */

58     public static IOFileFilter suffixFileFilter(String JavaDoc suffix) {
59         return new SuffixFileFilter(suffix);
60     }
61
62     /**
63      * Returns a filter that returns true if the filename matches the specified text.
64      *
65      * @param name the filename
66      * @return a name checking filter
67      */

68     public static IOFileFilter nameFileFilter(String JavaDoc name) {
69         return new NameFileFilter(name);
70     }
71
72     /**
73      * Returns a filter that checks if the file is a directory.
74      *
75      * @return directory file filter
76      */

77     public static IOFileFilter directoryFileFilter() {
78         return DirectoryFileFilter.INSTANCE;
79     }
80     
81     //-----------------------------------------------------------------------
82
/**
83      * Returns a filter that ANDs the two specified filters.
84      *
85      * @param filter1 the first filter
86      * @param filter2 the second filter
87      * @return a filter that ANDs the two specified filters
88      */

89     public static IOFileFilter andFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
90         return new AndFileFilter(filter1, filter2);
91     }
92
93     /**
94      * Returns a filter that ORs the two specified filters.
95      *
96      * @param filter1 the first filter
97      * @param filter2 the second filter
98      * @return a filter that ORs the two specified filters
99      */

100     public static IOFileFilter orFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
101         return new OrFileFilter(filter1, filter2);
102     }
103
104     /**
105      * Returns a filter that NOTs the specified filter.
106      *
107      * @param filter the filter to invert
108      * @return a filter that NOTs the specified filter
109      */

110     public static IOFileFilter notFileFilter(IOFileFilter filter) {
111         return new NotFileFilter(filter);
112     }
113
114     //-----------------------------------------------------------------------
115
/**
116      * Returns a filter that always returns true.
117      *
118      * @return a true filter
119      */

120     public static IOFileFilter trueFileFilter() {
121         return TrueFileFilter.INSTANCE;
122     }
123
124     /**
125      * Returns a filter that always returns false.
126      *
127      * @return a false filter
128      */

129     public static IOFileFilter falseFileFilter() {
130         return FalseFileFilter.INSTANCE;
131     }
132     
133     //-----------------------------------------------------------------------
134
/**
135      * Returns an <code>IOFileFilter</code> that wraps the
136      * <code>FileFilter</code> instance.
137      *
138      * @param filter the filter to be wrapped
139      * @return a new filter that implements IOFileFilter
140      */

141     public static IOFileFilter asFileFilter(FileFilter JavaDoc filter) {
142         return new DelegateFileFilter(filter);
143     }
144
145     /**
146      * Returns an <code>IOFileFilter</code> that wraps the
147      * <code>FilenameFilter</code> instance.
148      *
149      * @param filter the filter to be wrapped
150      * @return a new filter that implements IOFileFilter
151      */

152     public static IOFileFilter asFileFilter(FilenameFilter JavaDoc filter) {
153         return new DelegateFileFilter(filter);
154     }
155
156     //-----------------------------------------------------------------------
157

158     /* Constructed on demand and then cached */
159     private static IOFileFilter cvsFilter = null;
160
161     /**
162      * Resturns an IOFileFilter that ignores CVS directories. You may optionally
163      * pass in an existing IOFileFilter in which case it is extended to exclude
164      * CVS directories.
165      * @param filter IOFileFilter to modify, null if a new IOFileFilter
166      * should be created
167      * @return the requested (combined) filter
168      */

169     public static IOFileFilter makeCVSAware(IOFileFilter filter) {
170         if (cvsFilter == null) {
171             cvsFilter = andFileFilter(directoryFileFilter(),
172                 notFileFilter(nameFileFilter("CVS")));
173         }
174         if (filter == null) {
175             return cvsFilter;
176         } else {
177             return andFileFilter(filter, cvsFilter);
178         }
179     }
180
181 }
182
Popular Tags