~ubuntu-branches/ubuntu/karmic/commons-io/karmic

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/io/filefilter/FileFilterUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2007-04-13 08:20:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070413082049-2hd3s5ixtxsgvnvm
Tags: 1.3.1.dfsg.1-1
* New upstream (closes: #418973)
* java-gcj-compat-dev and cdbs transition
* Removed junit at the moment (closes: #397567)
* No javadoc at the moment

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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
 
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
7
8
 * 
8
9
 *      http://www.apache.org/licenses/LICENSE-2.0
9
10
 * 
15
16
 */
16
17
package org.apache.commons.io.filefilter;
17
18
 
 
19
import java.io.File;
18
20
import java.io.FileFilter;
19
21
import java.io.FilenameFilter;
 
22
import java.util.Date;
20
23
 
21
24
/**
22
25
 * Useful utilities for working with file filters. It provides access to all
24
27
 * every class you use.
25
28
 * 
26
29
 * @since Commons IO 1.0
27
 
 * @version $Revision: 1.8 $ $Date: 2004/02/23 04:37:57 $
 
30
 * @version $Id: FileFilterUtils.java 471628 2006-11-06 04:06:45Z bayard $
28
31
 * 
29
 
 * @author Henri Yandell
30
32
 * @author Stephen Colebourne
31
33
 * @author Jeremias Maerki
 
34
 * @author Masato Tezuka
 
35
 * @author Rahul Akolkar
32
36
 */
33
37
public class FileFilterUtils {
34
38
    
72
76
    /**
73
77
     * Returns a filter that checks if the file is a directory.
74
78
     * 
75
 
     * @return directory file filter
 
79
     * @return file filter that accepts only directories and not files
76
80
     */
77
81
    public static IOFileFilter directoryFileFilter() {
78
 
        return DirectoryFileFilter.INSTANCE;
79
 
    }
80
 
    
 
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
 
81
94
    //-----------------------------------------------------------------------
82
95
    /**
83
96
     * Returns a filter that ANDs the two specified filters.
118
131
     * @return a true filter
119
132
     */
120
133
    public static IOFileFilter trueFileFilter() {
121
 
        return TrueFileFilter.INSTANCE;
 
134
        return TrueFileFilter.TRUE;
122
135
    }
123
136
 
124
137
    /**
127
140
     * @return a false filter
128
141
     */
129
142
    public static IOFileFilter falseFileFilter() {
130
 
        return FalseFileFilter.INSTANCE;
 
143
        return FalseFileFilter.FALSE;
131
144
    }
132
145
    
133
146
    //-----------------------------------------------------------------------
154
167
    }
155
168
 
156
169
    //-----------------------------------------------------------------------
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
 
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 >= minimum size
 
270
     * and <= 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)
168
298
     */
169
299
    public static IOFileFilter makeCVSAware(IOFileFilter filter) {
170
300
        if (cvsFilter == null) {
171
 
            cvsFilter = andFileFilter(directoryFileFilter(), 
172
 
                notFileFilter(nameFileFilter("CVS")));
 
301
            cvsFilter = notFileFilter(
 
302
                andFileFilter(directoryFileFilter(), nameFileFilter("CVS")));
173
303
        }
174
304
        if (filter == null) {
175
305
            return cvsFilter;
178
308
        }
179
309
    }
180
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
 
181
361
}