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 * @since 1.2
038 * @see FileFilterUtils#sizeFileFilter(long)
039 * @see FileFilterUtils#sizeFileFilter(long, boolean)
040 * @see FileFilterUtils#sizeRangeFileFilter(long, long)
041 */
042public class SizeFileFilter extends AbstractFileFilter implements Serializable {
043
044    private static final long serialVersionUID = 7388077430788600069L;
045
046    /** The size threshold. */
047    private final long size;
048
049    /** Whether the files accepted will be larger or smaller. */
050    private final boolean acceptLarger;
051
052    /**
053     * Constructs a new size file filter for files equal to or
054     * larger than a certain size.
055     *
056     * @param size  the threshold size of the files
057     * @throws IllegalArgumentException if the size is negative
058     */
059    public SizeFileFilter(final long size) {
060        this(size, true);
061    }
062
063    /**
064     * Constructs a new size file filter for files based on a certain size
065     * threshold.
066     *
067     * @param size  the threshold size of the files
068     * @param acceptLarger  if true, files equal to or larger are accepted,
069     * otherwise smaller ones (but not equal to)
070     * @throws IllegalArgumentException if the size is negative
071     */
072    public SizeFileFilter(final long size, final boolean acceptLarger) {
073        if (size < 0) {
074            throw new IllegalArgumentException("The size must be non-negative");
075        }
076        this.size = size;
077        this.acceptLarger = acceptLarger;
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     * </p>
088     *
089     * @param file  the File to check
090     * @return true if the file name matches
091     */
092    @Override
093    public boolean accept(final File file) {
094        return acceptLarger != file.length() < size;
095    }
096
097    /**
098     * Provide a String representation of this file filter.
099     *
100     * @return a String representation
101     */
102    @Override
103    public String toString() {
104        final String condition = acceptLarger ? ">=" : "<";
105        return super.toString() + "(" + condition + size + ")";
106    }
107
108}