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;
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 * @version $Id: SizeFileFilter.java 1415850 2012-11-30 20:51:39Z ggregory $
038 * @since 1.2
039 * @see FileFilterUtils#sizeFileFilter(long)
040 * @see FileFilterUtils#sizeFileFilter(long, boolean)
041 * @see FileFilterUtils#sizeRangeFileFilter(long, long)
042 */
043public class SizeFileFilter extends AbstractFileFilter implements Serializable {
044
045    /** The size threshold. */
046    private final long size;
047    /** Whether the files accepted will be larger or smaller. */
048    private final boolean acceptLarger;
049
050    /**
051     * Constructs a new size file filter for files equal to or 
052     * larger than a certain size.
053     *
054     * @param size  the threshold size of the files
055     * @throws IllegalArgumentException if the size is negative
056     */
057    public SizeFileFilter(final long size) {
058        this(size, true);
059    }
060
061    /**
062     * Constructs a new size file filter for files based on a certain size
063     * threshold.
064     *
065     * @param size  the threshold size of the files
066     * @param acceptLarger  if true, files equal to or larger are accepted,
067     * otherwise smaller ones (but not equal to)
068     * @throws IllegalArgumentException if the size is negative
069     */
070    public SizeFileFilter(final long size, final boolean acceptLarger) {
071        if (size < 0) {
072            throw new IllegalArgumentException("The size must be non-negative");
073        }
074        this.size = size;
075        this.acceptLarger = acceptLarger;
076    }
077
078    //-----------------------------------------------------------------------
079    /**
080     * Checks to see if the size of the file is favorable.
081     * <p>
082     * If size equals threshold and smaller files are required,
083     * file <b>IS NOT</b> selected.
084     * If size equals threshold and larger files are required,
085     * file <b>IS</b> selected.
086     *
087     * @param file  the File to check
088     * @return true if the filename matches
089     */
090    @Override
091    public boolean accept(final File file) {
092        final boolean smaller = file.length() < size;
093        return acceptLarger ? !smaller : smaller;
094    }
095
096    /**
097     * Provide a String representaion of this file filter.
098     *
099     * @return a String representaion
100     */
101    @Override
102    public String toString() {
103        final String condition = acceptLarger ? ">=" : "<";
104        return super.toString() + "(" + condition + size + ")";
105    }
106
107}