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 */ 017 package org.apache.commons.io.filefilter; 018 019 import java.io.File; 020 import java.io.Serializable; 021 import java.util.Date; 022 023 import 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 * @version $Id: AgeFileFilter.java 1304052 2012-03-22 20:55:29Z ggregory $ 043 * @see FileFilterUtils#ageFileFilter(Date) 044 * @see FileFilterUtils#ageFileFilter(File) 045 * @see FileFilterUtils#ageFileFilter(long) 046 * @see FileFilterUtils#ageFileFilter(Date, boolean) 047 * @see FileFilterUtils#ageFileFilter(File, boolean) 048 * @see FileFilterUtils#ageFileFilter(long, boolean) 049 * @since 1.2 050 */ 051 public class AgeFileFilter extends AbstractFileFilter implements Serializable { 052 053 /** The cutoff time threshold. */ 054 private final long cutoff; 055 /** Whether the files accepted will be older or newer. */ 056 private final boolean acceptOlder; 057 058 /** 059 * Constructs a new age file filter for files equal to or older than 060 * a certain cutoff 061 * 062 * @param cutoff the threshold age of the files 063 */ 064 public AgeFileFilter(long cutoff) { 065 this(cutoff, true); 066 } 067 068 /** 069 * Constructs a new age file filter for files on any one side 070 * of a certain cutoff. 071 * 072 * @param cutoff the threshold age of the files 073 * @param acceptOlder if true, older files (at or before the cutoff) 074 * are accepted, else newer ones (after the cutoff). 075 */ 076 public AgeFileFilter(long cutoff, boolean acceptOlder) { 077 this.acceptOlder = acceptOlder; 078 this.cutoff = cutoff; 079 } 080 081 /** 082 * Constructs a new age file filter for files older than (at or before) 083 * a certain cutoff date. 084 * 085 * @param cutoffDate the threshold age of the files 086 */ 087 public AgeFileFilter(Date cutoffDate) { 088 this(cutoffDate, true); 089 } 090 091 /** 092 * Constructs a new age file filter for files on any one side 093 * of a certain cutoff date. 094 * 095 * @param cutoffDate the threshold age of the files 096 * @param acceptOlder if true, older files (at or before the cutoff) 097 * are accepted, else newer ones (after the cutoff). 098 */ 099 public AgeFileFilter(Date cutoffDate, boolean acceptOlder) { 100 this(cutoffDate.getTime(), acceptOlder); 101 } 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(File cutoffReference) { 111 this(cutoffReference, true); 112 } 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(File cutoffReference, boolean acceptOlder) { 125 this(cutoffReference.lastModified(), acceptOlder); 126 } 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(File file) { 143 boolean newer = FileUtils.isFileNewer(file, cutoff); 144 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 String condition = acceptOlder ? "<=" : ">"; 155 return super.toString() + "(" + condition + cutoff + ")"; 156 } 157 }