    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.Serializable;
    021import java.util.Date;
    022
    023import 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 &lt; files.length; i++ ) {
    038 *     System.out.println(files[i]);
    039 * }
    040 * </pre>
    041 *
    042 * @see FileFilterUtils#ageFileFilter(Date)
    043 * @see FileFilterUtils#ageFileFilter(File)
    044 * @see FileFilterUtils#ageFileFilter(long)
    045 * @see FileFilterUtils#ageFileFilter(Date, boolean)
    046 * @see FileFilterUtils#ageFileFilter(File, boolean)
    047 * @see FileFilterUtils#ageFileFilter(long, boolean)
    048 * @since 1.2
    049 */
    050public class AgeFileFilter extends AbstractFileFilter implements Serializable {
    051
    052    private static final long serialVersionUID = -2132740084016138541L;
    053
    054    /** The cutoff time threshold. */
    055    private final long cutoff;
    056
    057    /** Whether the files accepted will be older or newer. */
    058    private final boolean acceptOlder;
    059
    060    /**
    061     * Constructs a new age file filter for files equal to or older than
    062     * a certain cutoff
    063     *
    064     * @param cutoff  the threshold age of the files
    065     */
    066    public AgeFileFilter(final long cutoff) {
    067        this(cutoff, true);
    068    }
    069
    070    /**
    071     * Constructs a new age file filter for files on any one side
    072     * of a certain cutoff.
    073     *
    074     * @param cutoff  the threshold age of the files
    075     * @param acceptOlder  if true, older files (at or before the cutoff)
    076     * are accepted, else newer ones (after the cutoff).
    077     */
    078    public AgeFileFilter(final long cutoff, final boolean acceptOlder) {
    079        this.acceptOlder = acceptOlder;
    080        this.cutoff = cutoff;
    081    }
    082
    083    /**
    084     * Constructs a new age file filter for files older than (at or before)
    085     * a certain cutoff date.
    086     *
    087     * @param cutoffDate  the threshold age of the files
    088     */
    089    public AgeFileFilter(final Date cutoffDate) {
    090        this(cutoffDate, true);
    091    }
    092
    093    /**
    094     * Constructs a new age file filter for files on any one side
    095     * 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)
    099     * are accepted, else newer ones (after the 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)
    107     * a certain File (whose last modification time will be used as reference).
    108     *
    109     * @param cutoffReference  the file whose last modification
    110     *        time is used as the threshold age of the files
    111     */
    112    public AgeFileFilter(final File cutoffReference) {
    113        this(cutoffReference, true);
    114    }
    115
    116    /**
    117     * Constructs a new age file filter for files on any one side
    118     * of a certain File (whose last modification time will be used as
    119     * reference).
    120     *
    121     * @param cutoffReference  the file whose last modification
    122     *        time is used as the threshold age of the files
    123     * @param acceptOlder  if true, older files (at or before the cutoff)
    124     * are accepted, else newer ones (after the cutoff).
    125     */
    126    public AgeFileFilter(final File cutoffReference, final boolean acceptOlder) {
    127        this(cutoffReference.lastModified(), acceptOlder);
    128    }
    129
    130    //-----------------------------------------------------------------------
    131    /**
    132     * Checks to see if the last modification of the file matches cutoff
    133     * favorably.
    134     * <p>
    135     * If last modification time equals cutoff and newer files are required,
    136     * file <b>IS NOT</b> selected.
    137     * If last modification time equals cutoff and older files are required,
    138     * file <b>IS</b> selected.
    139     *
    140     * @param file  the File to check
    141     * @return true if the file name matches
    142     */
    143    @Override
    144    public boolean accept(final File file) {
    145        final boolean newer = FileUtils.isFileNewer(file, cutoff);
    146        return acceptOlder != newer;
    147    }
    148
    149    /**
    150     * Provide a String representation of this file filter.
    151     *
    152     * @return a String representation
    153     */
    154    @Override
    155    public String toString() {
    156        final String condition = acceptOlder ? "<=" : ">";
    157        return super.toString() + "(" + condition + cutoff + ")";
    158    }
    159}