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 < 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 /** Whether the files accepted will be older or newer. */ 057 private final boolean acceptOlder; 058 059 /** 060 * Constructs a new age file filter for files equal to or older than 061 * a certain cutoff 062 * 063 * @param cutoff the threshold age of the files 064 */ 065 public AgeFileFilter(final long cutoff) { 066 this(cutoff, true); 067 } 068 069 /** 070 * Constructs a new age file filter for files on any one side 071 * of a certain cutoff. 072 * 073 * @param cutoff the threshold age of the files 074 * @param acceptOlder if true, older files (at or before the cutoff) 075 * are accepted, else newer ones (after the cutoff). 076 */ 077 public AgeFileFilter(final long cutoff, final boolean acceptOlder) { 078 this.acceptOlder = acceptOlder; 079 this.cutoff = cutoff; 080 } 081 082 /** 083 * Constructs a new age file filter for files older than (at or before) 084 * a certain cutoff date. 085 * 086 * @param cutoffDate the threshold age of the files 087 */ 088 public AgeFileFilter(final Date cutoffDate) { 089 this(cutoffDate, true); 090 } 091 092 /** 093 * Constructs a new age file filter for files on any one side 094 * of a certain cutoff date. 095 * 096 * @param cutoffDate the threshold age of the files 097 * @param acceptOlder if true, older files (at or before the cutoff) 098 * are accepted, else newer ones (after the cutoff). 099 */ 100 public AgeFileFilter(final Date cutoffDate, final boolean acceptOlder) { 101 this(cutoffDate.getTime(), acceptOlder); 102 } 103 104 /** 105 * Constructs a new age file filter for files older than (at or before) 106 * a certain File (whose last modification time will be used as reference). 107 * 108 * @param cutoffReference the file whose last modification 109 * 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 117 * of a certain File (whose last modification time will be used as 118 * reference). 119 * 120 * @param cutoffReference the file whose last modification 121 * time is used as the threshold age of the files 122 * @param acceptOlder if true, older files (at or before the cutoff) 123 * are accepted, else newer ones (after the cutoff). 124 */ 125 public AgeFileFilter(final File cutoffReference, final boolean acceptOlder) { 126 this(cutoffReference.lastModified(), acceptOlder); 127 } 128 129 //----------------------------------------------------------------------- 130 /** 131 * Checks to see if the last modification of the file matches cutoff 132 * favorably. 133 * <p> 134 * If last modification time equals cutoff and newer files are required, 135 * file <b>IS NOT</b> selected. 136 * If last modification time equals cutoff and older files are required, 137 * file <b>IS</b> selected. 138 * 139 * @param file the File to check 140 * @return true if the file name matches 141 */ 142 @Override 143 public boolean accept(final File file) { 144 final boolean newer = FileUtils.isFileNewer(file, cutoff); 145 return acceptOlder != newer; 146 } 147 148 /** 149 * Provide a String representation of this file filter. 150 * 151 * @return a String representation 152 */ 153 @Override 154 public String toString() { 155 final String condition = acceptOlder ? "<=" : ">"; 156 return super.toString() + "(" + condition + cutoff + ")"; 157 } 158}