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    
022    /**
023     * Filters files based on size, can filter either smaller files or
024     * files equal to or larger than a given threshold.
025     * <p>
026     * For example, to print all files and directories in the
027     * current directory whose size is greater than 1 MB:
028     *
029     * <pre>
030     * File dir = new File(".");
031     * String[] files = dir.list( new SizeFileFilter(1024 * 1024) );
032     * for ( int i = 0; i &lt; files.length; i++ ) {
033     *     System.out.println(files[i]);
034     * }
035     * </pre>
036     *
037     * @author Rahul Akolkar
038     * @version $Id: SizeFileFilter.java 591058 2007-11-01 15:47:05Z niallp $
039     * @since Commons IO 1.2
040     */
041    public class SizeFileFilter extends AbstractFileFilter implements Serializable {
042    
043        /** The size threshold. */
044        private final long size;
045        /** Whether the files accepted will be larger or smaller. */
046        private final boolean acceptLarger;
047    
048        /**
049         * Constructs a new size file filter for files equal to or 
050         * larger than a certain size.
051         *
052         * @param size  the threshold size of the files
053         * @throws IllegalArgumentException if the size is negative
054         */
055        public SizeFileFilter(long size) {
056            this(size, true);
057        }
058    
059        /**
060         * Constructs a new size file filter for files based on a certain size
061         * threshold.
062         *
063         * @param size  the threshold size of the files
064         * @param acceptLarger  if true, files equal to or larger are accepted,
065         * otherwise smaller ones (but not equal to)
066         * @throws IllegalArgumentException if the size is negative
067         */
068        public SizeFileFilter(long size, boolean acceptLarger) {
069            if (size < 0) {
070                throw new IllegalArgumentException("The size must be non-negative");
071            }
072            this.size = size;
073            this.acceptLarger = acceptLarger;
074        }
075    
076        //-----------------------------------------------------------------------
077        /**
078         * Checks to see if the size of the file is favorable.
079         * <p>
080         * If size equals threshold and smaller files are required,
081         * file <b>IS NOT</b> selected.
082         * If size equals threshold and larger files are required,
083         * file <b>IS</b> selected.
084         *
085         * @param file  the File to check
086         * @return true if the filename matches
087         */
088        public boolean accept(File file) {
089            boolean smaller = file.length() < size;
090            return acceptLarger ? !smaller : smaller;
091        }
092    
093        /**
094         * Provide a String representaion of this file filter.
095         *
096         * @return a String representaion
097         */
098        public String toString() {
099            String condition = acceptLarger ? ">=" : "<";
100            return super.toString() + "(" + condition + size + ")";
101        }
102    
103    }