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.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 dir
049 * Files.<b>walkFileTree</b>(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 dir tree
056 * Files.<b>walkFileTree</b>(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     * Checks to see if the size of the file is favorable.
111     * <p>
112     * If size equals threshold and smaller files are required,
113     * file <b>IS NOT</b> selected.
114     * If size equals threshold and larger files are required,
115     * file <b>IS</b> 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     * Checks to see if the size of the file is favorable.
132     * <p>
133     * If size equals threshold and smaller files are required,
134     * file <b>IS NOT</b> selected.
135     * If size equals threshold and larger files are required,
136     * file <b>IS</b> selected.
137     * </p>
138     * @param file  the File to check
139     *
140     * @return true if the file name matches
141     */
142    @Override
143    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
144        return get(() -> toFileVisitResult(accept(Files.size(file))));
145    }
146
147    /**
148     * Provide a String representation of this file filter.
149     *
150     * @return a String representation
151     */
152    @Override
153    public String toString() {
154        final String condition = acceptLarger ? ">=" : "<";
155        return super.toString() + "(" + condition + size + ")";
156    }
157
158    @Override
159    public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
160        return toFileVisitResult(accept(Files.size(file)));
161    }
162
163}