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.io.FileFilter;
21  import java.io.FilenameFilter;
22  import java.util.Date;
23  
24  /**
25   * Useful utilities for working with file filters. It provides access to all
26   * file filter implementations in this package so you don't have to import
27   * every class you use.
28   * 
29   * @since Commons IO 1.0
30   * @version $Id: FileFilterUtils.java 471628 2006-11-06 04:06:45Z bayard $
31   * 
32   * @author Stephen Colebourne
33   * @author Jeremias Maerki
34   * @author Masato Tezuka
35   * @author Rahul Akolkar
36   */
37  public class FileFilterUtils {
38      
39      /**
40       * FileFilterUtils is not normally instantiated.
41       */
42      public FileFilterUtils() {
43      }
44  
45      //-----------------------------------------------------------------------
46      /**
47       * Returns a filter that returns true if the filename starts with the specified text.
48       * 
49       * @param prefix  the filename prefix
50       * @return a prefix checking filter
51       */
52      public static IOFileFilter prefixFileFilter(String prefix) {
53          return new PrefixFileFilter(prefix);
54      }
55  
56      /**
57       * Returns a filter that returns true if the filename ends with the specified text.
58       * 
59       * @param suffix  the filename suffix
60       * @return a suffix checking filter
61       */
62      public static IOFileFilter suffixFileFilter(String suffix) {
63          return new SuffixFileFilter(suffix);
64      }
65  
66      /**
67       * Returns a filter that returns true if the filename matches the specified text.
68       * 
69       * @param name  the filename
70       * @return a name checking filter
71       */
72      public static IOFileFilter nameFileFilter(String name) {
73          return new NameFileFilter(name);
74      }
75  
76      /**
77       * Returns a filter that checks if the file is a directory.
78       * 
79       * @return file filter that accepts only directories and not files
80       */
81      public static IOFileFilter directoryFileFilter() {
82          return DirectoryFileFilter.DIRECTORY;
83      }
84  
85      /**
86       * Returns a filter that checks if the file is a file (and not a directory).
87       * 
88       * @return file filter that accepts only files and not directories
89       */
90      public static IOFileFilter fileFileFilter() {
91          return FileFileFilter.FILE;
92      }
93  
94      //-----------------------------------------------------------------------
95      /**
96       * Returns a filter that ANDs the two specified filters.
97       * 
98       * @param filter1  the first filter
99       * @param filter2  the second filter
100      * @return a filter that ANDs the two specified filters
101      */
102     public static IOFileFilter andFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
103         return new AndFileFilter(filter1, filter2);
104     }
105 
106     /**
107      * Returns a filter that ORs the two specified filters.
108      * 
109      * @param filter1  the first filter
110      * @param filter2  the second filter
111      * @return a filter that ORs the two specified filters
112      */
113     public static IOFileFilter orFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
114         return new OrFileFilter(filter1, filter2);
115     }
116 
117     /**
118      * Returns a filter that NOTs the specified filter.
119      * 
120      * @param filter  the filter to invert
121      * @return a filter that NOTs the specified filter
122      */
123     public static IOFileFilter notFileFilter(IOFileFilter filter) {
124         return new NotFileFilter(filter);
125     }
126 
127     //-----------------------------------------------------------------------
128     /**
129      * Returns a filter that always returns true.
130      * 
131      * @return a true filter
132      */
133     public static IOFileFilter trueFileFilter() {
134         return TrueFileFilter.TRUE;
135     }
136 
137     /**
138      * Returns a filter that always returns false.
139      * 
140      * @return a false filter
141      */
142     public static IOFileFilter falseFileFilter() {
143         return FalseFileFilter.FALSE;
144     }
145     
146     //-----------------------------------------------------------------------
147     /**
148      * Returns an <code>IOFileFilter</code> that wraps the
149      * <code>FileFilter</code> instance.
150      * 
151      * @param filter  the filter to be wrapped
152      * @return a new filter that implements IOFileFilter
153      */
154     public static IOFileFilter asFileFilter(FileFilter filter) {
155         return new DelegateFileFilter(filter);
156     }
157 
158     /**
159      * Returns an <code>IOFileFilter</code> that wraps the
160      * <code>FilenameFilter</code> instance.
161      * 
162      * @param filter  the filter to be wrapped
163      * @return a new filter that implements IOFileFilter
164      */
165     public static IOFileFilter asFileFilter(FilenameFilter filter) {
166         return new DelegateFileFilter(filter);
167     }
168 
169     //-----------------------------------------------------------------------
170     /**
171      * Returns a filter that returns true if the file was last modified after
172      * the specified cutoff time.
173      *
174      * @param cutoff  the time threshold
175      * @return an appropriately configured age file filter
176      * @since Commons IO 1.2
177      */
178     public static IOFileFilter ageFileFilter(long cutoff) {
179         return new AgeFileFilter(cutoff);
180     }
181 
182     /**
183      * Returns a filter that filters files based on a cutoff time.
184      *
185      * @param cutoff  the time threshold
186      * @param acceptOlder  if true, older files get accepted, if false, newer
187      * @return an appropriately configured age file filter
188      * @since Commons IO 1.2
189      */
190     public static IOFileFilter ageFileFilter(long cutoff, boolean acceptOlder) {
191         return new AgeFileFilter(cutoff, acceptOlder);
192     }
193 
194     /**
195      * Returns a filter that returns true if the file was last modified after
196      * the specified cutoff date.
197      *
198      * @param cutoffDate  the time threshold
199      * @return an appropriately configured age file filter
200      * @since Commons IO 1.2
201      */
202     public static IOFileFilter ageFileFilter(Date cutoffDate) {
203         return new AgeFileFilter(cutoffDate);
204     }
205 
206     /**
207      * Returns a filter that filters files based on a cutoff date.
208      *
209      * @param cutoffDate  the time threshold
210      * @param acceptOlder  if true, older files get accepted, if false, newer
211      * @return an appropriately configured age file filter
212      * @since Commons IO 1.2
213      */
214     public static IOFileFilter ageFileFilter(Date cutoffDate, boolean acceptOlder) {
215         return new AgeFileFilter(cutoffDate, acceptOlder);
216     }
217 
218     /**
219      * Returns a filter that returns true if the file was last modified after
220      * the specified reference file.
221      *
222      * @param cutoffReference  the file whose last modification
223      *        time is usesd as the threshold age of the files
224      * @return an appropriately configured age file filter
225      * @since Commons IO 1.2
226      */
227     public static IOFileFilter ageFileFilter(File cutoffReference) {
228         return new AgeFileFilter(cutoffReference);
229     }
230 
231     /**
232      * Returns a filter that filters files based on a cutoff reference file.
233      *
234      * @param cutoffReference  the file whose last modification
235      *        time is usesd as the threshold age of the files
236      * @param acceptOlder  if true, older files get accepted, if false, newer
237      * @return an appropriately configured age file filter
238      * @since Commons IO 1.2
239      */
240     public static IOFileFilter ageFileFilter(File cutoffReference, boolean acceptOlder) {
241         return new AgeFileFilter(cutoffReference, acceptOlder);
242     }
243 
244     //-----------------------------------------------------------------------
245     /**
246      * Returns a filter that returns true if the file is bigger than a certain size.
247      *
248      * @param threshold  the file size threshold
249      * @return an appropriately configured SizeFileFilter
250      * @since Commons IO 1.2
251      */
252     public static IOFileFilter sizeFileFilter(long threshold) {
253         return new SizeFileFilter(threshold);
254     }
255 
256     /**
257      * Returns a filter that filters based on file size.
258      *
259      * @param threshold  the file size threshold
260      * @param acceptLarger  if true, larger files get accepted, if false, smaller
261      * @return an appropriately configured SizeFileFilter
262      * @since Commons IO 1.2
263      */
264     public static IOFileFilter sizeFileFilter(long threshold, boolean acceptLarger) {
265         return new SizeFileFilter(threshold, acceptLarger);
266     }
267 
268     /**
269      * Returns a filter that accepts files whose size is &gt;= minimum size
270      * and &lt;= maximum size.
271      *
272      * @param minSizeInclusive the minimum file size (inclusive)
273      * @param maxSizeInclusive the maximum file size (inclusive)
274      * @return an appropriately configured IOFileFilter
275      * @since Commons IO 1.3
276      */
277     public static IOFileFilter sizeRangeFileFilter(long minSizeInclusive, long maxSizeInclusive ) {
278         IOFileFilter minimumFilter = new SizeFileFilter(minSizeInclusive, true);
279         IOFileFilter maximumFilter = new SizeFileFilter(maxSizeInclusive + 1L, false);
280         return new AndFileFilter(minimumFilter, maximumFilter);
281     }
282 
283     //-----------------------------------------------------------------------
284     /* Constructed on demand and then cached */
285     private static IOFileFilter cvsFilter;
286 
287     /* Constructed on demand and then cached */
288     private static IOFileFilter svnFilter;
289 
290     /**
291      * Decorates a filter to make it ignore CVS directories.
292      * Passing in <code>null</code> will return a filter that accepts everything
293      * except CVS directories.
294      * 
295      * @param filter  the filter to decorate, null means an unrestricted filter
296      * @return the decorated filter, never null
297      * @since 1.1 (method existed but had bug in 1.0)
298      */
299     public static IOFileFilter makeCVSAware(IOFileFilter filter) {
300         if (cvsFilter == null) {
301             cvsFilter = notFileFilter(
302                 andFileFilter(directoryFileFilter(), nameFileFilter("CVS")));
303         }
304         if (filter == null) {
305             return cvsFilter;
306         } else {
307             return andFileFilter(filter, cvsFilter);
308         }
309     }
310 
311     /**
312      * Decorates a filter to make it ignore SVN directories.
313      * Passing in <code>null</code> will return a filter that accepts everything
314      * except SVN directories.
315      * 
316      * @param filter  the filter to decorate, null means an unrestricted filter
317      * @return the decorated filter, never null
318      * @since 1.1
319      */
320     public static IOFileFilter makeSVNAware(IOFileFilter filter) {
321         if (svnFilter == null) {
322             svnFilter = notFileFilter(
323                 andFileFilter(directoryFileFilter(), nameFileFilter(".svn")));
324         }
325         if (filter == null) {
326             return svnFilter;
327         } else {
328             return andFileFilter(filter, svnFilter);
329         }
330     }
331 
332     //-----------------------------------------------------------------------
333     /**
334      * Decorates a filter so that it only applies to directories and not to files.
335      * 
336      * @param filter  the filter to decorate, null means an unrestricted filter
337      * @return the decorated filter, never null
338      * @since 1.3
339      */
340     public static IOFileFilter makeDirectoryOnly(IOFileFilter filter) {
341         if (filter == null) {
342             return DirectoryFileFilter.DIRECTORY;
343         }
344         return new AndFileFilter(DirectoryFileFilter.DIRECTORY, filter);
345     }
346 
347     /**
348      * Decorates a filter so that it only applies to files and not to directories.
349      * 
350      * @param filter  the filter to decorate, null means an unrestricted filter
351      * @return the decorated filter, never null
352      * @since 1.3
353      */
354     public static IOFileFilter makeFileOnly(IOFileFilter filter) {
355         if (filter == null) {
356             return FileFileFilter.FILE;
357         }
358         return new AndFileFilter(FileFileFilter.FILE, filter);
359     }
360 
361 }