AgeFileFilter.java revision bc47398187c6ffd132435e51d8d61e6ec79a79db
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 */
17package org.apache.commons.io.filefilter;
18
19import java.io.File;
20import java.io.Serializable;
21import java.util.Date;
22
23import org.apache.commons.io.FileUtils;
24
25/**
26 * Filters files based on a cutoff time, can filter either newer
27 * files or files equal to or older.
28 * <p>
29 * For example, to print all files and directories in the
30 * current directory older than one day:
31 *
32 * <pre>
33 * File dir = new File(".");
34 * // We are interested in files older than one day
35 * long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
36 * String[] files = dir.list( new AgeFileFilter(cutoff) );
37 * for ( int i = 0; i &lt; files.length; i++ ) {
38 *     System.out.println(files[i]);
39 * }
40 * </pre>
41 *
42 * @author Rahul Akolkar
43 * @version $Id: AgeFileFilter.java 606381 2007-12-22 02:03:16Z ggregory $
44 * @since Commons IO 1.2
45 */
46public class AgeFileFilter extends AbstractFileFilter implements Serializable {
47
48    /** The cutoff time threshold. */
49    private final long cutoff;
50    /** Whether the files accepted will be older or newer. */
51    private final boolean acceptOlder;
52
53    /**
54     * Constructs a new age file filter for files equal to or older than
55     * a certain cutoff
56     *
57     * @param cutoff  the threshold age of the files
58     */
59    public AgeFileFilter(long cutoff) {
60        this(cutoff, true);
61    }
62
63    /**
64     * Constructs a new age file filter for files on any one side
65     * of a certain cutoff.
66     *
67     * @param cutoff  the threshold age of the files
68     * @param acceptOlder  if true, older files (at or before the cutoff)
69     * are accepted, else newer ones (after the cutoff).
70     */
71    public AgeFileFilter(long cutoff, boolean acceptOlder) {
72        this.acceptOlder = acceptOlder;
73        this.cutoff = cutoff;
74    }
75
76    /**
77     * Constructs a new age file filter for files older than (at or before)
78     * a certain cutoff date.
79     *
80     * @param cutoffDate  the threshold age of the files
81     */
82    public AgeFileFilter(Date cutoffDate) {
83        this(cutoffDate, true);
84    }
85
86    /**
87     * Constructs a new age file filter for files on any one side
88     * of a certain cutoff date.
89     *
90     * @param cutoffDate  the threshold age of the files
91     * @param acceptOlder  if true, older files (at or before the cutoff)
92     * are accepted, else newer ones (after the cutoff).
93     */
94    public AgeFileFilter(Date cutoffDate, boolean acceptOlder) {
95        this(cutoffDate.getTime(), acceptOlder);
96    }
97
98    /**
99     * Constructs a new age file filter for files older than (at or before)
100     * a certain File (whose last modification time will be used as reference).
101     *
102     * @param cutoffReference  the file whose last modification
103     *        time is usesd as the threshold age of the files
104     */
105    public AgeFileFilter(File cutoffReference) {
106        this(cutoffReference, true);
107    }
108
109    /**
110     * Constructs a new age file filter for files on any one side
111     * of a certain File (whose last modification time will be used as
112     * reference).
113     *
114     * @param cutoffReference  the file whose last modification
115     *        time is usesd as the threshold age of the files
116     * @param acceptOlder  if true, older files (at or before the cutoff)
117     * are accepted, else newer ones (after the cutoff).
118     */
119    public AgeFileFilter(File cutoffReference, boolean acceptOlder) {
120        this(cutoffReference.lastModified(), acceptOlder);
121    }
122
123    //-----------------------------------------------------------------------
124    /**
125     * Checks to see if the last modification of the file matches cutoff
126     * favorably.
127     * <p>
128     * If last modification time equals cutoff and newer files are required,
129     * file <b>IS NOT</b> selected.
130     * If last modification time equals cutoff and older files are required,
131     * file <b>IS</b> selected.
132     *
133     * @param file  the File to check
134     * @return true if the filename matches
135     */
136    public boolean accept(File file) {
137        boolean newer = FileUtils.isFileNewer(file, cutoff);
138        return acceptOlder ? !newer : newer;
139    }
140
141    /**
142     * Provide a String representaion of this file filter.
143     *
144     * @return a String representaion
145     */
146    public String toString() {
147        String condition = acceptOlder ? "<=" : ">";
148        return super.toString() + "(" + condition + cutoff + ")";
149    }
150}
151