Coverage Report - org.apache.commons.io.filefilter.AgeFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
AgeFileFilter
100%
18/18
100%
6/6
1.25
 
 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  
 
 19  
 import java.io.File;
 20  
 import java.io.Serializable;
 21  
 import java.util.Date;
 22  
 
 23  
 import org.apache.commons.io.FileUtils;
 24  
 
 25  
 /**
 26  
  * Filters files based on a cutoff time, can filter either newer
 27  
  * files or files equal to or older.
 28  
  * <p>
 29  
  * For example, to print all files and directories in the
 30  
  * current directory older than one day:
 31  
  *
 32  
  * <pre>
 33  
  * File dir = new File(".");
 34  
  * // We are interested in files older than one day
 35  
  * long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
 36  
  * String[] files = dir.list( new AgeFileFilter(cutoff) );
 37  
  * for ( int i = 0; i &lt; files.length; i++ ) {
 38  
  *     System.out.println(files[i]);
 39  
  * }
 40  
  * </pre>
 41  
  *
 42  
  * @version $Id: AgeFileFilter.java 1415850 2012-11-30 20:51:39Z ggregory $
 43  
  * @see FileFilterUtils#ageFileFilter(Date)
 44  
  * @see FileFilterUtils#ageFileFilter(File)
 45  
  * @see FileFilterUtils#ageFileFilter(long)
 46  
  * @see FileFilterUtils#ageFileFilter(Date, boolean)
 47  
  * @see FileFilterUtils#ageFileFilter(File, boolean)
 48  
  * @see FileFilterUtils#ageFileFilter(long, boolean)
 49  
  * @since 1.2
 50  
  */
 51  
 public class AgeFileFilter extends AbstractFileFilter implements Serializable {
 52  
 
 53  
     /** The cutoff time threshold. */
 54  
     private final long cutoff;
 55  
     /** Whether the files accepted will be older or newer. */
 56  
     private final boolean acceptOlder;
 57  
 
 58  
     /**
 59  
      * Constructs a new age file filter for files equal to or older than
 60  
      * a certain cutoff
 61  
      *
 62  
      * @param cutoff  the threshold age of the files
 63  
      */
 64  
     public AgeFileFilter(final long cutoff) {
 65  2
         this(cutoff, true);
 66  2
     }
 67  
 
 68  
     /**
 69  
      * Constructs a new age file filter for files on any one side
 70  
      * of a certain cutoff.
 71  
      *
 72  
      * @param cutoff  the threshold age of the files
 73  
      * @param acceptOlder  if true, older files (at or before the cutoff)
 74  
      * are accepted, else newer ones (after the cutoff).
 75  
      */
 76  18
     public AgeFileFilter(final long cutoff, final boolean acceptOlder) {
 77  18
         this.acceptOlder = acceptOlder;
 78  18
         this.cutoff = cutoff;
 79  18
     }
 80  
 
 81  
     /**
 82  
      * Constructs a new age file filter for files older than (at or before)
 83  
      * a certain cutoff date.
 84  
      *
 85  
      * @param cutoffDate  the threshold age of the files
 86  
      */
 87  
     public AgeFileFilter(final Date cutoffDate) {
 88  2
         this(cutoffDate, true);
 89  2
     }
 90  
 
 91  
     /**
 92  
      * Constructs a new age file filter for files on any one side
 93  
      * of a certain cutoff date.
 94  
      *
 95  
      * @param cutoffDate  the threshold age of the files
 96  
      * @param acceptOlder  if true, older files (at or before the cutoff)
 97  
      * are accepted, else newer ones (after the cutoff).
 98  
      */
 99  
     public AgeFileFilter(final Date cutoffDate, final boolean acceptOlder) {
 100  6
         this(cutoffDate.getTime(), acceptOlder);
 101  6
     }
 102  
 
 103  
     /**
 104  
      * Constructs a new age file filter for files older than (at or before)
 105  
      * a certain File (whose last modification time will be used as reference).
 106  
      *
 107  
      * @param cutoffReference  the file whose last modification
 108  
      *        time is usesd as the threshold age of the files
 109  
      */
 110  
     public AgeFileFilter(final File cutoffReference) {
 111  2
         this(cutoffReference, true);
 112  2
     }
 113  
 
 114  
     /**
 115  
      * Constructs a new age file filter for files on any one side
 116  
      * of a certain File (whose last modification time will be used as
 117  
      * reference).
 118  
      *
 119  
      * @param cutoffReference  the file whose last modification
 120  
      *        time is usesd as the threshold age of the files
 121  
      * @param acceptOlder  if true, older files (at or before the cutoff)
 122  
      * are accepted, else newer ones (after the cutoff).
 123  
      */
 124  
     public AgeFileFilter(final File cutoffReference, final boolean acceptOlder) {
 125  6
         this(cutoffReference.lastModified(), acceptOlder);
 126  6
     }
 127  
 
 128  
     //-----------------------------------------------------------------------
 129  
     /**
 130  
      * Checks to see if the last modification of the file matches cutoff
 131  
      * favorably.
 132  
      * <p>
 133  
      * If last modification time equals cutoff and newer files are required,
 134  
      * file <b>IS NOT</b> selected.
 135  
      * If last modification time equals cutoff and older files are required,
 136  
      * file <b>IS</b> selected.
 137  
      *
 138  
      * @param file  the File to check
 139  
      * @return true if the filename matches
 140  
      */
 141  
     @Override
 142  
     public boolean accept(final File file) {
 143  72
         final boolean newer = FileUtils.isFileNewer(file, cutoff);
 144  72
         return acceptOlder ? !newer : newer;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Provide a String representaion of this file filter.
 149  
      *
 150  
      * @return a String representaion
 151  
      */
 152  
     @Override
 153  
     public String toString() {
 154  36
         final String condition = acceptOlder ? "<=" : ">";
 155  36
         return super.toString() + "(" + condition + cutoff + ")";
 156  
     }
 157  
 }