    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 *      https://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.IOException;
    021import java.io.Serializable;
    022import java.nio.file.FileVisitResult;
    023import java.nio.file.Files;
    024import java.nio.file.Path;
    025import java.nio.file.attribute.BasicFileAttributes;
    026
    027/**
    028 * Filters files based on size, can filter either smaller files or
    029 * files equal to or larger than a given threshold.
    030 * <p>
    031 * For example, to print all files and directories in the
    032 * current directory whose size is greater than 1 MB:
    033 * </p>
    034 * <h2>Using Classic IO</h2>
    035 * <pre>
    036 * File dir = FileUtils.current();
    037 * String[] files = dir.list(new SizeFileFilter(1024 * 1024));
    038 * for (String file : files) {
    039 *     System.out.println(file);
    040 * }
    041 * </pre>
    042 *
    043 * <h2>Using NIO</h2>
    044 * <pre>
    045 * final Path dir = PathUtils.current();
    046 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new SizeFileFilter(1024 * 1024));
    047 * //
    048 * // Walk one directory
    049 * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
    050 * System.out.println(visitor.getPathCounters());
    051 * System.out.println(visitor.getFileList());
    052 * //
    053 * visitor.getPathCounters().reset();
    054 * //
    055 * // Walk directory tree
    056 * Files.<strong>walkFileTree</strong>(dir, visitor);
    057 * System.out.println(visitor.getPathCounters());
    058 * System.out.println(visitor.getDirList());
    059 * System.out.println(visitor.getFileList());
    060 * </pre>
    061 * <h2>Deprecating Serialization</h2>
    062 * <p>
    063 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    064 * </p>
    065 *
    066 * @since 1.2
    067 * @see FileFilterUtils#sizeFileFilter(long)
    068 * @see FileFilterUtils#sizeFileFilter(long, boolean)
    069 * @see FileFilterUtils#sizeRangeFileFilter(long, long)
    070 */
    071public class SizeFileFilter extends AbstractFileFilter implements Serializable {
    072
    073    private static final long serialVersionUID = 7388077430788600069L;
    074
    075    /** Whether the files accepted will be larger or smaller. */
    076    private final boolean acceptLarger;
    077
    078    /** The size threshold. */
    079    private final long size;
    080
    081    /**
    082     * Constructs a new size file filter for files equal to or
    083     * larger than a certain size.
    084     *
    085     * @param size  the threshold size of the files.
    086     * @throws IllegalArgumentException if the size is negative.
    087     */
    088    public SizeFileFilter(final long size) {
    089        this(size, true);
    090    }
    091
    092    /**
    093     * Constructs a new size file filter for files based on a certain size
    094     * threshold.
    095     *
    096     * @param size  the threshold size of the files.
    097     * @param acceptLarger  if true, files equal to or larger are accepted,
    098     * otherwise smaller ones (but not equal to).
    099     * @throws IllegalArgumentException if the size is negative.
    100     */
    101    public SizeFileFilter(final long size, final boolean acceptLarger) {
    102        if (size < 0) {
    103            throw new IllegalArgumentException("The size must be non-negative");
    104        }
    105        this.size = size;
    106        this.acceptLarger = acceptLarger;
    107    }
    108
    109    /**
    110     * Tests to see if the size of the file is favorable.
    111     * <p>
    112     * If size equals threshold and smaller files are required,
    113     * file <strong>IS NOT</strong> selected.
    114     * If size equals threshold and larger files are required,
    115     * file <strong>IS</strong> selected.
    116     * </p>
    117     *
    118     * @param file  the File to check.
    119     * @return true if the file name matches.
    120     */
    121    @Override
    122    public boolean accept(final File file) {
    123        return accept(file != null ? file.length() : 0);
    124    }
    125
    126    private boolean accept(final long length) {
    127        return acceptLarger != length < size;
    128    }
    129
    130    /**
    131     * Tests to see if the size of the file is favorable.
    132     * <p>
    133     * If size equals threshold and smaller files are required, file <strong>IS NOT</strong> selected. If size equals threshold and larger files are required,
    134     * file <strong>IS</strong> selected.
    135     * </p>
    136     *
    137     * @param file       the File to check.
    138     * @param attributes the path's basic attributes (may be null).
    139     * @return true if the file name matches.
    140     */
    141    @Override
    142    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    143        return get(() -> toFileVisitResult(accept(Files.size(file))));
    144    }
    145
    146    /**
    147     * Provide a String representation of this file filter.
    148     *
    149     * @return a String representation.
    150     */
    151    @Override
    152    public String toString() {
    153        final String condition = acceptLarger ? ">=" : "<";
    154        return super.toString() + "(" + condition + size + ")";
    155    }
    156
    157    @Override
    158    public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
    159        return toFileVisitResult(accept(Files.size(file)));
    160    }
    161
    162}