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