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