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 */ 17package org.apache.commons.io.filefilter; 18 19import java.io.File; 20import java.io.Serializable; 21 22/** 23 * Filters files based on size, can filter either smaller files or 24 * files equal to or larger than a given threshold. 25 * <p> 26 * For example, to print all files and directories in the 27 * current directory whose size is greater than 1 MB: 28 * 29 * <pre> 30 * File dir = new File("."); 31 * String[] files = dir.list( new SizeFileFilter(1024 * 1024) ); 32 * for ( int i = 0; i < files.length; i++ ) { 33 * System.out.println(files[i]); 34 * } 35 * </pre> 36 * 37 * @author Rahul Akolkar 38 * @version $Id: SizeFileFilter.java 591058 2007-11-01 15:47:05Z niallp $ 39 * @since Commons IO 1.2 40 */ 41public class SizeFileFilter extends AbstractFileFilter implements Serializable { 42 43 /** The size threshold. */ 44 private final long size; 45 /** Whether the files accepted will be larger or smaller. */ 46 private final boolean acceptLarger; 47 48 /** 49 * Constructs a new size file filter for files equal to or 50 * larger than a certain size. 51 * 52 * @param size the threshold size of the files 53 * @throws IllegalArgumentException if the size is negative 54 */ 55 public SizeFileFilter(long size) { 56 this(size, true); 57 } 58 59 /** 60 * Constructs a new size file filter for files based on a certain size 61 * threshold. 62 * 63 * @param size the threshold size of the files 64 * @param acceptLarger if true, files equal to or larger are accepted, 65 * otherwise smaller ones (but not equal to) 66 * @throws IllegalArgumentException if the size is negative 67 */ 68 public SizeFileFilter(long size, boolean acceptLarger) { 69 if (size < 0) { 70 throw new IllegalArgumentException("The size must be non-negative"); 71 } 72 this.size = size; 73 this.acceptLarger = acceptLarger; 74 } 75 76 //----------------------------------------------------------------------- 77 /** 78 * Checks to see if the size of the file is favorable. 79 * <p> 80 * If size equals threshold and smaller files are required, 81 * file <b>IS NOT</b> selected. 82 * If size equals threshold and larger files are required, 83 * file <b>IS</b> selected. 84 * 85 * @param file the File to check 86 * @return true if the filename matches 87 */ 88 public boolean accept(File file) { 89 boolean smaller = file.length() < size; 90 return acceptLarger ? !smaller : smaller; 91 } 92 93 /** 94 * Provide a String representaion of this file filter. 95 * 96 * @return a String representaion 97 */ 98 public String toString() { 99 String condition = acceptLarger ? ">=" : "<"; 100 return super.toString() + "(" + condition + size + ")"; 101 } 102 103} 104