AgeFileFilter.java

  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. import java.io.File;
  19. import java.io.Serializable;
  20. import java.nio.file.FileVisitResult;
  21. import java.nio.file.Path;
  22. import java.nio.file.attribute.BasicFileAttributes;
  23. import java.time.Instant;
  24. import java.util.Date;

  25. import org.apache.commons.io.FileUtils;
  26. import org.apache.commons.io.file.PathUtils;

  27. /**
  28.  * Filters files based on a cutoff time, can filter either newer files or files equal to or older.
  29.  * <p>
  30.  * For example, to print all files and directories in the current directory older than one day:
  31.  * </p>
  32.  * <h2>Using Classic IO</h2>
  33.  * <pre>
  34.  * Path dir = PathUtils.current();
  35.  * // We are interested in files older than one day
  36.  * Instant cutoff = Instant.now().minus(Duration.ofDays(1));
  37.  * String[] files = dir.list(new AgeFileFilter(cutoff));
  38.  * for (String file : files) {
  39.  *     System.out.println(file);
  40.  * }
  41.  * </pre>
  42.  *
  43.  * <h2>Using NIO</h2>
  44.  * <pre>
  45.  * Path dir = PathUtils.current();
  46.  * // We are interested in files older than one day
  47.  * Instant cutoff = Instant.now().minus(Duration.ofDays(1));
  48.  * AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new AgeFileFilter(cutoff));
  49.  * //
  50.  * // Walk one directoryectory
  51.  * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
  52.  * System.out.println(visitor.getPathCounters());
  53.  * System.out.println(visitor.getFileList());
  54.  * //
  55.  * visitor.getPathCounters().reset();
  56.  * //
  57.  * // Walk directory tree
  58.  * Files.<strong>walkFileTree</strong>(dir, visitor);
  59.  * System.out.println(visitor.getPathCounters());
  60.  * System.out.println(visitor.getDirList());
  61.  * System.out.println(visitor.getFileList());
  62.  * </pre>
  63.  * <h2>Deprecating Serialization</h2>
  64.  * <p>
  65.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  66.  * </p>
  67.  *
  68.  * @see FileFilterUtils#ageFileFilter(Date)
  69.  * @see FileFilterUtils#ageFileFilter(File)
  70.  * @see FileFilterUtils#ageFileFilter(long)
  71.  * @see FileFilterUtils#ageFileFilter(Date, boolean)
  72.  * @see FileFilterUtils#ageFileFilter(File, boolean)
  73.  * @see FileFilterUtils#ageFileFilter(long, boolean)
  74.  * @since 1.2
  75.  */
  76. public class AgeFileFilter extends AbstractFileFilter implements Serializable {

  77.     private static final long serialVersionUID = -2132740084016138541L;

  78.     /** Whether the files accepted will be older or newer. */
  79.     private final boolean acceptOlder;

  80.     /** The cutoff time threshold measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970). */
  81.     private final Instant cutoffInstant;

  82.     /**
  83.      * Constructs a new age file filter for files older than (at or before) a certain cutoff date.
  84.      *
  85.      * @param cutoffDate the threshold age of the files
  86.      */
  87.     public AgeFileFilter(final Date cutoffDate) {
  88.         this(cutoffDate, true);
  89.     }

  90.     /**
  91.      * Constructs a new age file filter for files on any one side of a certain cutoff date.
  92.      *
  93.      * @param cutoffDate the threshold age of the files
  94.      * @param acceptOlder if true, older files (at or before the cutoff) are accepted, else newer ones (after the
  95.      *        cutoff).
  96.      */
  97.     public AgeFileFilter(final Date cutoffDate, final boolean acceptOlder) {
  98.         this(cutoffDate.toInstant(), acceptOlder);
  99.     }

  100.     /**
  101.      * Constructs a new age file filter for files older than (at or before) a certain File (whose last modification time
  102.      * will be used as reference).
  103.      *
  104.      * @param cutoffReference the file whose last modification time is used as the threshold age of the files
  105.      */
  106.     public AgeFileFilter(final File cutoffReference) {
  107.         this(cutoffReference, true);
  108.     }

  109.     /**
  110.      * Constructs a new age file filter for files on any one side of a certain File (whose last modification time will
  111.      * be used as reference).
  112.      *
  113.      * @param cutoffReference the file whose last modification time is used as the threshold age of the files
  114.      * @param acceptOlder if true, older files (at or before the cutoff) are accepted, else newer ones (after the
  115.      *        cutoff).
  116.      */
  117.     public AgeFileFilter(final File cutoffReference, final boolean acceptOlder) {
  118.         this(FileUtils.lastModifiedUnchecked(cutoffReference), acceptOlder);
  119.     }

  120.     /**
  121.      * Constructs a new age file filter for files equal to or older than a certain cutoff.
  122.      *
  123.      * @param cutoffInstant The cutoff time threshold since the epoch (00:00:00 GMT, January 1, 1970).
  124.      * @since 2.12.0
  125.      */
  126.     public AgeFileFilter(final Instant cutoffInstant) {
  127.         this(cutoffInstant, true);
  128.     }

  129.     /**
  130.      * Constructs a new age file filter for files on any one side of a certain cutoff.
  131.      *
  132.      * @param cutoffInstant The cutoff time threshold since the epoch (00:00:00 GMT, January 1, 1970).
  133.      * @param acceptOlder if true, older files (at or before the cutoff) are accepted, else newer ones (after the cutoff).
  134.      * @since 2.12.0
  135.      */
  136.     public AgeFileFilter(final Instant cutoffInstant, final boolean acceptOlder) {
  137.         this.acceptOlder = acceptOlder;
  138.         this.cutoffInstant = cutoffInstant;
  139.     }

  140.     /**
  141.      * Constructs a new age file filter for files equal to or older than a certain cutoff
  142.      *
  143.      * @param cutoffMillis The cutoff time threshold measured in milliseconds since the epoch (00:00:00 GMT, January 1,
  144.      *        1970).
  145.      */
  146.     public AgeFileFilter(final long cutoffMillis) {
  147.         this(Instant.ofEpochMilli(cutoffMillis), true);
  148.     }

  149.     /**
  150.      * Constructs a new age file filter for files on any one side of a certain cutoff.
  151.      *
  152.      * @param cutoffMillis The cutoff time threshold measured in milliseconds since the epoch (00:00:00 GMT, January 1,
  153.      *        1970).
  154.      * @param acceptOlder if true, older files (at or before the cutoff) are accepted, else newer ones (after the
  155.      *        cutoff).
  156.      */
  157.     public AgeFileFilter(final long cutoffMillis, final boolean acceptOlder) {
  158.         this(Instant.ofEpochMilli(cutoffMillis), acceptOlder);
  159.     }

  160.     /**
  161.      * Checks to see if the last modification of the file matches cutoff favorably.
  162.      * <p>
  163.      * If last modification time equals cutoff and newer files are required, file <strong>IS NOT</strong> selected. If last
  164.      * modification time equals cutoff and older files are required, file <strong>IS</strong> selected.
  165.      * </p>
  166.      *
  167.      * @param file the File to check
  168.      * @return true if the file name matches
  169.      */
  170.     @Override
  171.     public boolean accept(final File file) {
  172.         return acceptOlder != FileUtils.isFileNewer(file, cutoffInstant);
  173.     }

  174.     /**
  175.      * Checks to see if the last modification of the file matches cutoff favorably.
  176.      * <p>
  177.      * If last modification time equals cutoff and newer files are required, file <strong>IS NOT</strong> selected. If last
  178.      * modification time equals cutoff and older files are required, file <strong>IS</strong> selected.
  179.      * </p>
  180.      *
  181.      * @param file the File to check
  182.      * @param attributes the path's basic attributes (may be null).
  183.      * @return true if the file name matches
  184.      * @since 2.9.0
  185.      */
  186.     @Override
  187.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  188.         return get(() -> toFileVisitResult(acceptOlder != PathUtils.isNewer(file, cutoffInstant)));
  189.     }

  190.     /**
  191.      * Provide a String representation of this file filter.
  192.      *
  193.      * @return a String representation
  194.      */
  195.     @Override
  196.     public String toString() {
  197.         final String condition = acceptOlder ? "<=" : ">";
  198.         return super.toString() + "(" + condition + cutoffInstant + ")";
  199.     }
  200. }