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 */
017package org.apache.commons.io.filefilter;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.Serializable;
022import java.nio.file.FileVisitResult;
023import java.nio.file.Path;
024import java.nio.file.attribute.BasicFileAttributes;
025import java.util.Date;
026
027import org.apache.commons.io.FileUtils;
028import org.apache.commons.io.file.PathUtils;
029
030/**
031 * Filters files based on a cutoff time, can filter either newer files or files equal to or older.
032 * <p>
033 * For example, to print all files and directories in the current directory older than one day:
034 * </p>
035 * <h2>Using Classic IO</h2>
036 * <pre>
037 * Path dir = Paths.get("");
038 * // We are interested in files older than one day
039 * long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
040 * String[] files = dir.list(new AgeFileFilter(cutoff));
041 * for (String file : files) {
042 *     System.out.println(file);
043 * }
044 * </pre>
045 *
046 * <h2>Using NIO</h2>
047 * <pre>
048 * Path dir = Paths.get("");
049 * // We are interested in files older than one day
050 * long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
051 * AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new AgeFileFilter(cutoff));
052 * //
053 * // Walk one dir
054 * Files.<b>walkFileTree</b>(dir, Collections.emptySet(), 1, visitor);
055 * System.out.println(visitor.getPathCounters());
056 * System.out.println(visitor.getFileList());
057 * //
058 * visitor.getPathCounters().reset();
059 * //
060 * // Walk dir tree
061 * Files.<b>walkFileTree</b>(dir, visitor);
062 * System.out.println(visitor.getPathCounters());
063 * System.out.println(visitor.getDirList());
064 * System.out.println(visitor.getFileList());
065 * </pre>
066 *
067 * @see FileFilterUtils#ageFileFilter(Date)
068 * @see FileFilterUtils#ageFileFilter(File)
069 * @see FileFilterUtils#ageFileFilter(long)
070 * @see FileFilterUtils#ageFileFilter(Date, boolean)
071 * @see FileFilterUtils#ageFileFilter(File, boolean)
072 * @see FileFilterUtils#ageFileFilter(long, boolean)
073 * @since 1.2
074 */
075public class AgeFileFilter extends AbstractFileFilter implements Serializable {
076
077    private static final long serialVersionUID = -2132740084016138541L;
078
079    /** Whether the files accepted will be older or newer. */
080    private final boolean acceptOlder;
081
082    /** The cutoff time threshold measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970). */
083    private final long cutoffMillis;
084
085    /**
086     * Constructs a new age file filter for files older than (at or before) a certain cutoff date.
087     *
088     * @param cutoffDate the threshold age of the files
089     */
090    public AgeFileFilter(final Date cutoffDate) {
091        this(cutoffDate, true);
092    }
093
094    /**
095     * Constructs a new age file filter for files on any one side of a certain cutoff date.
096     *
097     * @param cutoffDate the threshold age of the files
098     * @param acceptOlder if true, older files (at or before the cutoff) are accepted, else newer ones (after the
099     *        cutoff).
100     */
101    public AgeFileFilter(final Date cutoffDate, final boolean acceptOlder) {
102        this(cutoffDate.getTime(), acceptOlder);
103    }
104
105    /**
106     * Constructs a new age file filter for files older than (at or before) a certain File (whose last modification time
107     * will be used as reference).
108     *
109     * @param cutoffReference the file whose last modification time is used as the threshold age of the files
110     */
111    public AgeFileFilter(final File cutoffReference) {
112        this(cutoffReference, true);
113    }
114
115    /**
116     * Constructs a new age file filter for files on any one side of a certain File (whose last modification time will
117     * be used as reference).
118     *
119     * @param cutoffReference the file whose last modification time is used as the threshold age of the files
120     * @param acceptOlder if true, older files (at or before the cutoff) are accepted, else newer ones (after the
121     *        cutoff).
122     */
123    public AgeFileFilter(final File cutoffReference, final boolean acceptOlder) {
124        this(FileUtils.lastModifiedUnchecked(cutoffReference), acceptOlder);
125    }
126
127    /**
128     * Constructs a new age file filter for files equal to or older than a certain cutoff
129     *
130     * @param cutoffMillis The cutoff time threshold measured in milliseconds since the epoch (00:00:00 GMT, January 1,
131     *        1970).
132     */
133    public AgeFileFilter(final long cutoffMillis) {
134        this(cutoffMillis, true);
135    }
136
137    /**
138     * Constructs a new age file filter for files on any one side of a certain cutoff.
139     *
140     * @param cutoffMillis The cutoff time threshold measured in milliseconds since the epoch (00:00:00 GMT, January 1,
141     *        1970).
142     * @param acceptOlder if true, older files (at or before the cutoff) are accepted, else newer ones (after the
143     *        cutoff).
144     */
145    public AgeFileFilter(final long cutoffMillis, final boolean acceptOlder) {
146        this.acceptOlder = acceptOlder;
147        this.cutoffMillis = cutoffMillis;
148    }
149
150    /**
151     * Checks to see if the last modification of the file matches cutoff favorably.
152     * <p>
153     * If last modification time equals cutoff and newer files are required, file <b>IS NOT</b> selected. If last
154     * modification time equals cutoff and older files are required, file <b>IS</b> selected.
155     * </p>
156     *
157     * @param file the File to check
158     * @return true if the file name matches
159     */
160    @Override
161    public boolean accept(final File file) {
162        final boolean newer = FileUtils.isFileNewer(file, cutoffMillis);
163        return acceptOlder != newer;
164    }
165
166    /**
167     * Checks to see if the last modification of the file matches cutoff favorably.
168     * <p>
169     * If last modification time equals cutoff and newer files are required, file <b>IS NOT</b> selected. If last
170     * modification time equals cutoff and older files are required, file <b>IS</b> selected.
171     * </p>
172     * @param file the File to check
173     *
174     * @return true if the file name matches
175     * @since 2.9.0
176     */
177    @Override
178    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
179        final boolean newer;
180        try {
181            newer = PathUtils.isNewer(file, cutoffMillis);
182        } catch (final IOException e) {
183            return handle(e);
184        }
185        return toFileVisitResult(acceptOlder != newer, file);
186    }
187
188    /**
189     * Provide a String representation of this file filter.
190     *
191     * @return a String representation
192     */
193    @Override
194    public String toString() {
195        final String condition = acceptOlder ? "<=" : ">";
196        return super.toString() + "(" + condition + cutoffMillis + ")";
197    }
198}