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