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.FileFilter; 021 import java.io.FilenameFilter; 022 import java.util.Date; 023 024 /** 025 * Useful utilities for working with file filters. It provides access to all 026 * file filter implementations in this package so you don't have to import 027 * every class you use. 028 * 029 * @since Commons IO 1.0 030 * @version $Id: FileFilterUtils.java 609286 2008-01-06 10:01:26Z scolebourne $ 031 * 032 * @author Stephen Colebourne 033 * @author Jeremias Maerki 034 * @author Masato Tezuka 035 * @author Rahul Akolkar 036 */ 037 public class FileFilterUtils { 038 039 /** 040 * FileFilterUtils is not normally instantiated. 041 */ 042 public FileFilterUtils() { 043 } 044 045 //----------------------------------------------------------------------- 046 /** 047 * Returns a filter that returns true if the filename starts with the specified text. 048 * 049 * @param prefix the filename prefix 050 * @return a prefix checking filter 051 */ 052 public static IOFileFilter prefixFileFilter(String prefix) { 053 return new PrefixFileFilter(prefix); 054 } 055 056 /** 057 * Returns a filter that returns true if the filename ends with the specified text. 058 * 059 * @param suffix the filename suffix 060 * @return a suffix checking filter 061 */ 062 public static IOFileFilter suffixFileFilter(String suffix) { 063 return new SuffixFileFilter(suffix); 064 } 065 066 /** 067 * Returns a filter that returns true if the filename matches the specified text. 068 * 069 * @param name the filename 070 * @return a name checking filter 071 */ 072 public static IOFileFilter nameFileFilter(String name) { 073 return new NameFileFilter(name); 074 } 075 076 /** 077 * Returns a filter that checks if the file is a directory. 078 * 079 * @return file filter that accepts only directories and not files 080 */ 081 public static IOFileFilter directoryFileFilter() { 082 return DirectoryFileFilter.DIRECTORY; 083 } 084 085 /** 086 * Returns a filter that checks if the file is a file (and not a directory). 087 * 088 * @return file filter that accepts only files and not directories 089 */ 090 public static IOFileFilter fileFileFilter() { 091 return FileFileFilter.FILE; 092 } 093 094 //----------------------------------------------------------------------- 095 /** 096 * Returns a filter that ANDs the two specified filters. 097 * 098 * @param filter1 the first filter 099 * @param filter2 the second filter 100 * @return a filter that ANDs the two specified filters 101 */ 102 public static IOFileFilter andFileFilter(IOFileFilter filter1, IOFileFilter filter2) { 103 return new AndFileFilter(filter1, filter2); 104 } 105 106 /** 107 * Returns a filter that ORs the two specified filters. 108 * 109 * @param filter1 the first filter 110 * @param filter2 the second filter 111 * @return a filter that ORs the two specified filters 112 */ 113 public static IOFileFilter orFileFilter(IOFileFilter filter1, IOFileFilter filter2) { 114 return new OrFileFilter(filter1, filter2); 115 } 116 117 /** 118 * Returns a filter that NOTs the specified filter. 119 * 120 * @param filter the filter to invert 121 * @return a filter that NOTs the specified filter 122 */ 123 public static IOFileFilter notFileFilter(IOFileFilter filter) { 124 return new NotFileFilter(filter); 125 } 126 127 //----------------------------------------------------------------------- 128 /** 129 * Returns a filter that always returns true. 130 * 131 * @return a true filter 132 */ 133 public static IOFileFilter trueFileFilter() { 134 return TrueFileFilter.TRUE; 135 } 136 137 /** 138 * Returns a filter that always returns false. 139 * 140 * @return a false filter 141 */ 142 public static IOFileFilter falseFileFilter() { 143 return FalseFileFilter.FALSE; 144 } 145 146 //----------------------------------------------------------------------- 147 /** 148 * Returns an <code>IOFileFilter</code> that wraps the 149 * <code>FileFilter</code> instance. 150 * 151 * @param filter the filter to be wrapped 152 * @return a new filter that implements IOFileFilter 153 */ 154 public static IOFileFilter asFileFilter(FileFilter filter) { 155 return new DelegateFileFilter(filter); 156 } 157 158 /** 159 * Returns an <code>IOFileFilter</code> that wraps the 160 * <code>FilenameFilter</code> instance. 161 * 162 * @param filter the filter to be wrapped 163 * @return a new filter that implements IOFileFilter 164 */ 165 public static IOFileFilter asFileFilter(FilenameFilter filter) { 166 return new DelegateFileFilter(filter); 167 } 168 169 //----------------------------------------------------------------------- 170 /** 171 * Returns a filter that returns true if the file was last modified after 172 * the specified cutoff time. 173 * 174 * @param cutoff the time threshold 175 * @return an appropriately configured age file filter 176 * @since Commons IO 1.2 177 */ 178 public static IOFileFilter ageFileFilter(long cutoff) { 179 return new AgeFileFilter(cutoff); 180 } 181 182 /** 183 * Returns a filter that filters files based on a cutoff time. 184 * 185 * @param cutoff the time threshold 186 * @param acceptOlder if true, older files get accepted, if false, newer 187 * @return an appropriately configured age file filter 188 * @since Commons IO 1.2 189 */ 190 public static IOFileFilter ageFileFilter(long cutoff, boolean acceptOlder) { 191 return new AgeFileFilter(cutoff, acceptOlder); 192 } 193 194 /** 195 * Returns a filter that returns true if the file was last modified after 196 * the specified cutoff date. 197 * 198 * @param cutoffDate the time threshold 199 * @return an appropriately configured age file filter 200 * @since Commons IO 1.2 201 */ 202 public static IOFileFilter ageFileFilter(Date cutoffDate) { 203 return new AgeFileFilter(cutoffDate); 204 } 205 206 /** 207 * Returns a filter that filters files based on a cutoff date. 208 * 209 * @param cutoffDate the time threshold 210 * @param acceptOlder if true, older files get accepted, if false, newer 211 * @return an appropriately configured age file filter 212 * @since Commons IO 1.2 213 */ 214 public static IOFileFilter ageFileFilter(Date cutoffDate, boolean acceptOlder) { 215 return new AgeFileFilter(cutoffDate, acceptOlder); 216 } 217 218 /** 219 * Returns a filter that returns true if the file was last modified after 220 * the specified reference file. 221 * 222 * @param cutoffReference the file whose last modification 223 * time is usesd as the threshold age of the files 224 * @return an appropriately configured age file filter 225 * @since Commons IO 1.2 226 */ 227 public static IOFileFilter ageFileFilter(File cutoffReference) { 228 return new AgeFileFilter(cutoffReference); 229 } 230 231 /** 232 * Returns a filter that filters files based on a cutoff reference file. 233 * 234 * @param cutoffReference the file whose last modification 235 * time is usesd as the threshold age of the files 236 * @param acceptOlder if true, older files get accepted, if false, newer 237 * @return an appropriately configured age file filter 238 * @since Commons IO 1.2 239 */ 240 public static IOFileFilter ageFileFilter(File cutoffReference, boolean acceptOlder) { 241 return new AgeFileFilter(cutoffReference, acceptOlder); 242 } 243 244 //----------------------------------------------------------------------- 245 /** 246 * Returns a filter that returns true if the file is bigger than a certain size. 247 * 248 * @param threshold the file size threshold 249 * @return an appropriately configured SizeFileFilter 250 * @since Commons IO 1.2 251 */ 252 public static IOFileFilter sizeFileFilter(long threshold) { 253 return new SizeFileFilter(threshold); 254 } 255 256 /** 257 * Returns a filter that filters based on file size. 258 * 259 * @param threshold the file size threshold 260 * @param acceptLarger if true, larger files get accepted, if false, smaller 261 * @return an appropriately configured SizeFileFilter 262 * @since Commons IO 1.2 263 */ 264 public static IOFileFilter sizeFileFilter(long threshold, boolean acceptLarger) { 265 return new SizeFileFilter(threshold, acceptLarger); 266 } 267 268 /** 269 * Returns a filter that accepts files whose size is >= minimum size 270 * and <= maximum size. 271 * 272 * @param minSizeInclusive the minimum file size (inclusive) 273 * @param maxSizeInclusive the maximum file size (inclusive) 274 * @return an appropriately configured IOFileFilter 275 * @since Commons IO 1.3 276 */ 277 public static IOFileFilter sizeRangeFileFilter(long minSizeInclusive, long maxSizeInclusive ) { 278 IOFileFilter minimumFilter = new SizeFileFilter(minSizeInclusive, true); 279 IOFileFilter maximumFilter = new SizeFileFilter(maxSizeInclusive + 1L, false); 280 return new AndFileFilter(minimumFilter, maximumFilter); 281 } 282 283 //----------------------------------------------------------------------- 284 /* Constructed on demand and then cached */ 285 private static IOFileFilter cvsFilter; 286 287 /* Constructed on demand and then cached */ 288 private static IOFileFilter svnFilter; 289 290 /** 291 * Decorates a filter to make it ignore CVS directories. 292 * Passing in <code>null</code> will return a filter that accepts everything 293 * except CVS directories. 294 * 295 * @param filter the filter to decorate, null means an unrestricted filter 296 * @return the decorated filter, never null 297 * @since Commons IO 1.1 (method existed but had bug in 1.0) 298 */ 299 public static IOFileFilter makeCVSAware(IOFileFilter filter) { 300 if (cvsFilter == null) { 301 cvsFilter = notFileFilter( 302 andFileFilter(directoryFileFilter(), nameFileFilter("CVS"))); 303 } 304 if (filter == null) { 305 return cvsFilter; 306 } else { 307 return andFileFilter(filter, cvsFilter); 308 } 309 } 310 311 /** 312 * Decorates a filter to make it ignore SVN directories. 313 * Passing in <code>null</code> will return a filter that accepts everything 314 * except SVN directories. 315 * 316 * @param filter the filter to decorate, null means an unrestricted filter 317 * @return the decorated filter, never null 318 * @since Commons IO 1.1 319 */ 320 public static IOFileFilter makeSVNAware(IOFileFilter filter) { 321 if (svnFilter == null) { 322 svnFilter = notFileFilter( 323 andFileFilter(directoryFileFilter(), nameFileFilter(".svn"))); 324 } 325 if (filter == null) { 326 return svnFilter; 327 } else { 328 return andFileFilter(filter, svnFilter); 329 } 330 } 331 332 //----------------------------------------------------------------------- 333 /** 334 * Decorates a filter so that it only applies to directories and not to files. 335 * 336 * @param filter the filter to decorate, null means an unrestricted filter 337 * @return the decorated filter, never null 338 * @since Commons IO 1.3 339 */ 340 public static IOFileFilter makeDirectoryOnly(IOFileFilter filter) { 341 if (filter == null) { 342 return DirectoryFileFilter.DIRECTORY; 343 } 344 return new AndFileFilter(DirectoryFileFilter.DIRECTORY, filter); 345 } 346 347 /** 348 * Decorates a filter so that it only applies to files and not to directories. 349 * 350 * @param filter the filter to decorate, null means an unrestricted filter 351 * @return the decorated filter, never null 352 * @since Commons IO 1.3 353 */ 354 public static IOFileFilter makeFileOnly(IOFileFilter filter) { 355 if (filter == null) { 356 return FileFileFilter.FILE; 357 } 358 return new AndFileFilter(FileFileFilter.FILE, filter); 359 } 360 361 }