001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io.filefilter;
018:
019: import java.io.File;
020: import java.io.FileFilter;
021: import java.io.FilenameFilter;
022: import java.util.Date;
023:
024: /**
025: * Useful utilities for working with file filters. It provides access to all
026: * file filter implementations in this package so you don't have to import
027: * every class you use.
028: *
029: * @since Commons IO 1.0
030: * @version $Id: FileFilterUtils.java 471628 2006-11-06 04:06:45Z bayard $
031: *
032: * @author Stephen Colebourne
033: * @author Jeremias Maerki
034: * @author Masato Tezuka
035: * @author Rahul Akolkar
036: */
037: public class FileFilterUtils {
038:
039: /**
040: * FileFilterUtils is not normally instantiated.
041: */
042: public FileFilterUtils() {
043: }
044:
045: //-----------------------------------------------------------------------
046: /**
047: * Returns a filter that returns true if the filename starts with the specified text.
048: *
049: * @param prefix the filename prefix
050: * @return a prefix checking filter
051: */
052: public static IOFileFilter prefixFileFilter(String prefix) {
053: return new PrefixFileFilter(prefix);
054: }
055:
056: /**
057: * Returns a filter that returns true if the filename ends with the specified text.
058: *
059: * @param suffix the filename suffix
060: * @return a suffix checking filter
061: */
062: public static IOFileFilter suffixFileFilter(String suffix) {
063: return new SuffixFileFilter(suffix);
064: }
065:
066: /**
067: * Returns a filter that returns true if the filename matches the specified text.
068: *
069: * @param name the filename
070: * @return a name checking filter
071: */
072: public static IOFileFilter nameFileFilter(String name) {
073: return new NameFileFilter(name);
074: }
075:
076: /**
077: * Returns a filter that checks if the file is a directory.
078: *
079: * @return file filter that accepts only directories and not files
080: */
081: public static IOFileFilter directoryFileFilter() {
082: return DirectoryFileFilter.DIRECTORY;
083: }
084:
085: /**
086: * Returns a filter that checks if the file is a file (and not a directory).
087: *
088: * @return file filter that accepts only files and not directories
089: */
090: public static IOFileFilter fileFileFilter() {
091: return FileFileFilter.FILE;
092: }
093:
094: //-----------------------------------------------------------------------
095: /**
096: * Returns a filter that ANDs the two specified filters.
097: *
098: * @param filter1 the first filter
099: * @param filter2 the second filter
100: * @return a filter that ANDs the two specified filters
101: */
102: public static IOFileFilter andFileFilter(IOFileFilter filter1,
103: IOFileFilter filter2) {
104: return new AndFileFilter(filter1, filter2);
105: }
106:
107: /**
108: * Returns a filter that ORs the two specified filters.
109: *
110: * @param filter1 the first filter
111: * @param filter2 the second filter
112: * @return a filter that ORs the two specified filters
113: */
114: public static IOFileFilter orFileFilter(IOFileFilter filter1,
115: IOFileFilter filter2) {
116: return new OrFileFilter(filter1, filter2);
117: }
118:
119: /**
120: * Returns a filter that NOTs the specified filter.
121: *
122: * @param filter the filter to invert
123: * @return a filter that NOTs the specified filter
124: */
125: public static IOFileFilter notFileFilter(IOFileFilter filter) {
126: return new NotFileFilter(filter);
127: }
128:
129: //-----------------------------------------------------------------------
130: /**
131: * Returns a filter that always returns true.
132: *
133: * @return a true filter
134: */
135: public static IOFileFilter trueFileFilter() {
136: return TrueFileFilter.TRUE;
137: }
138:
139: /**
140: * Returns a filter that always returns false.
141: *
142: * @return a false filter
143: */
144: public static IOFileFilter falseFileFilter() {
145: return FalseFileFilter.FALSE;
146: }
147:
148: //-----------------------------------------------------------------------
149: /**
150: * Returns an <code>IOFileFilter</code> that wraps the
151: * <code>FileFilter</code> instance.
152: *
153: * @param filter the filter to be wrapped
154: * @return a new filter that implements IOFileFilter
155: */
156: public static IOFileFilter asFileFilter(FileFilter filter) {
157: return new DelegateFileFilter(filter);
158: }
159:
160: /**
161: * Returns an <code>IOFileFilter</code> that wraps the
162: * <code>FilenameFilter</code> instance.
163: *
164: * @param filter the filter to be wrapped
165: * @return a new filter that implements IOFileFilter
166: */
167: public static IOFileFilter asFileFilter(FilenameFilter filter) {
168: return new DelegateFileFilter(filter);
169: }
170:
171: //-----------------------------------------------------------------------
172: /**
173: * Returns a filter that returns true if the file was last modified after
174: * the specified cutoff time.
175: *
176: * @param cutoff the time threshold
177: * @return an appropriately configured age file filter
178: * @since Commons IO 1.2
179: */
180: public static IOFileFilter ageFileFilter(long cutoff) {
181: return new AgeFileFilter(cutoff);
182: }
183:
184: /**
185: * Returns a filter that filters files based on a cutoff time.
186: *
187: * @param cutoff the time threshold
188: * @param acceptOlder if true, older files get accepted, if false, newer
189: * @return an appropriately configured age file filter
190: * @since Commons IO 1.2
191: */
192: public static IOFileFilter ageFileFilter(long cutoff,
193: boolean acceptOlder) {
194: return new AgeFileFilter(cutoff, acceptOlder);
195: }
196:
197: /**
198: * Returns a filter that returns true if the file was last modified after
199: * the specified cutoff date.
200: *
201: * @param cutoffDate the time threshold
202: * @return an appropriately configured age file filter
203: * @since Commons IO 1.2
204: */
205: public static IOFileFilter ageFileFilter(Date cutoffDate) {
206: return new AgeFileFilter(cutoffDate);
207: }
208:
209: /**
210: * Returns a filter that filters files based on a cutoff date.
211: *
212: * @param cutoffDate the time threshold
213: * @param acceptOlder if true, older files get accepted, if false, newer
214: * @return an appropriately configured age file filter
215: * @since Commons IO 1.2
216: */
217: public static IOFileFilter ageFileFilter(Date cutoffDate,
218: boolean acceptOlder) {
219: return new AgeFileFilter(cutoffDate, acceptOlder);
220: }
221:
222: /**
223: * Returns a filter that returns true if the file was last modified after
224: * the specified reference file.
225: *
226: * @param cutoffReference the file whose last modification
227: * time is usesd as the threshold age of the files
228: * @return an appropriately configured age file filter
229: * @since Commons IO 1.2
230: */
231: public static IOFileFilter ageFileFilter(File cutoffReference) {
232: return new AgeFileFilter(cutoffReference);
233: }
234:
235: /**
236: * Returns a filter that filters files based on a cutoff reference file.
237: *
238: * @param cutoffReference the file whose last modification
239: * time is usesd as the threshold age of the files
240: * @param acceptOlder if true, older files get accepted, if false, newer
241: * @return an appropriately configured age file filter
242: * @since Commons IO 1.2
243: */
244: public static IOFileFilter ageFileFilter(File cutoffReference,
245: boolean acceptOlder) {
246: return new AgeFileFilter(cutoffReference, acceptOlder);
247: }
248:
249: //-----------------------------------------------------------------------
250: /**
251: * Returns a filter that returns true if the file is bigger than a certain size.
252: *
253: * @param threshold the file size threshold
254: * @return an appropriately configured SizeFileFilter
255: * @since Commons IO 1.2
256: */
257: public static IOFileFilter sizeFileFilter(long threshold) {
258: return new SizeFileFilter(threshold);
259: }
260:
261: /**
262: * Returns a filter that filters based on file size.
263: *
264: * @param threshold the file size threshold
265: * @param acceptLarger if true, larger files get accepted, if false, smaller
266: * @return an appropriately configured SizeFileFilter
267: * @since Commons IO 1.2
268: */
269: public static IOFileFilter sizeFileFilter(long threshold,
270: boolean acceptLarger) {
271: return new SizeFileFilter(threshold, acceptLarger);
272: }
273:
274: /**
275: * Returns a filter that accepts files whose size is >= minimum size
276: * and <= maximum size.
277: *
278: * @param minSizeInclusive the minimum file size (inclusive)
279: * @param maxSizeInclusive the maximum file size (inclusive)
280: * @return an appropriately configured IOFileFilter
281: * @since Commons IO 1.3
282: */
283: public static IOFileFilter sizeRangeFileFilter(
284: long minSizeInclusive, long maxSizeInclusive) {
285: IOFileFilter minimumFilter = new SizeFileFilter(
286: minSizeInclusive, true);
287: IOFileFilter maximumFilter = new SizeFileFilter(
288: maxSizeInclusive + 1L, false);
289: return new AndFileFilter(minimumFilter, maximumFilter);
290: }
291:
292: //-----------------------------------------------------------------------
293: /* Constructed on demand and then cached */
294: private static IOFileFilter cvsFilter;
295:
296: /* Constructed on demand and then cached */
297: private static IOFileFilter svnFilter;
298:
299: /**
300: * Decorates a filter to make it ignore CVS directories.
301: * Passing in <code>null</code> will return a filter that accepts everything
302: * except CVS directories.
303: *
304: * @param filter the filter to decorate, null means an unrestricted filter
305: * @return the decorated filter, never null
306: * @since 1.1 (method existed but had bug in 1.0)
307: */
308: public static IOFileFilter makeCVSAware(IOFileFilter filter) {
309: if (cvsFilter == null) {
310: cvsFilter = notFileFilter(andFileFilter(
311: directoryFileFilter(), nameFileFilter("CVS")));
312: }
313: if (filter == null) {
314: return cvsFilter;
315: } else {
316: return andFileFilter(filter, cvsFilter);
317: }
318: }
319:
320: /**
321: * Decorates a filter to make it ignore SVN directories.
322: * Passing in <code>null</code> will return a filter that accepts everything
323: * except SVN directories.
324: *
325: * @param filter the filter to decorate, null means an unrestricted filter
326: * @return the decorated filter, never null
327: * @since 1.1
328: */
329: public static IOFileFilter makeSVNAware(IOFileFilter filter) {
330: if (svnFilter == null) {
331: svnFilter = notFileFilter(andFileFilter(
332: directoryFileFilter(), nameFileFilter(".svn")));
333: }
334: if (filter == null) {
335: return svnFilter;
336: } else {
337: return andFileFilter(filter, svnFilter);
338: }
339: }
340:
341: //-----------------------------------------------------------------------
342: /**
343: * Decorates a filter so that it only applies to directories and not to files.
344: *
345: * @param filter the filter to decorate, null means an unrestricted filter
346: * @return the decorated filter, never null
347: * @since 1.3
348: */
349: public static IOFileFilter makeDirectoryOnly(IOFileFilter filter) {
350: if (filter == null) {
351: return DirectoryFileFilter.DIRECTORY;
352: }
353: return new AndFileFilter(DirectoryFileFilter.DIRECTORY, filter);
354: }
355:
356: /**
357: * Decorates a filter so that it only applies to files and not to directories.
358: *
359: * @param filter the filter to decorate, null means an unrestricted filter
360: * @return the decorated filter, never null
361: * @since 1.3
362: */
363: public static IOFileFilter makeFileOnly(IOFileFilter filter) {
364: if (filter == null) {
365: return FileFileFilter.FILE;
366: }
367: return new AndFileFilter(FileFileFilter.FILE, filter);
368: }
369:
370: }
|