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.util.Date;
021:
022: import org.apache.commons.io.FileUtils;
023:
024: /**
025: * Filters files based on a cutoff time, can filter either newer
026: * files or files equal to or older.
027: * <p>
028: * For example, to print all files and directories in the
029: * current directory older than one day:
030: *
031: * <pre>
032: * File dir = new File(".");
033: * // We are interested in files older than one day
034: * long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
035: * String[] files = dir.list( new AgeFileFilter(cutoff) );
036: * for ( int i = 0; i < files.length; i++ ) {
037: * System.out.println(files[i]);
038: * }
039: * </pre>
040: *
041: * @author Rahul Akolkar
042: * @version $Id: AgeFileFilter.java 463570 2006-10-13 06:14:41Z niallp $
043: * @since Commons IO 1.2
044: */
045: public class AgeFileFilter extends AbstractFileFilter {
046:
047: /** The cutoff time threshold. */
048: private long cutoff;
049: /** Whether the files accepted will be older or newer. */
050: private boolean acceptOlder;
051:
052: /**
053: * Constructs a new age file filter for files equal to or older than
054: * a certain cutoff
055: *
056: * @param cutoff the threshold age of the files
057: */
058: public AgeFileFilter(long cutoff) {
059: this (cutoff, true);
060: }
061:
062: /**
063: * Constructs a new age file filter for files on any one side
064: * of a certain cutoff.
065: *
066: * @param cutoff the threshold age of the files
067: * @param acceptOlder if true, older files (at or before the cutoff)
068: * are accepted, else newer ones (after the cutoff).
069: */
070: public AgeFileFilter(long cutoff, boolean acceptOlder) {
071: this .acceptOlder = acceptOlder;
072: this .cutoff = cutoff;
073: }
074:
075: /**
076: * Constructs a new age file filter for files older than (at or before)
077: * a certain cutoff date.
078: *
079: * @param cutoffDate the threshold age of the files
080: */
081: public AgeFileFilter(Date cutoffDate) {
082: this (cutoffDate, true);
083: }
084:
085: /**
086: * Constructs a new age file filter for files on any one side
087: * of a certain cutoff date.
088: *
089: * @param cutoffDate the threshold age of the files
090: * @param acceptOlder if true, older files (at or before the cutoff)
091: * are accepted, else newer ones (after the cutoff).
092: */
093: public AgeFileFilter(Date cutoffDate, boolean acceptOlder) {
094: this (cutoffDate.getTime(), acceptOlder);
095: }
096:
097: /**
098: * Constructs a new age file filter for files older than (at or before)
099: * 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: }
|