14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/*
24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one or more
34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * contributor license agreements.  See the NOTICE file distributed with
44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * this work for additional information regarding copyright ownership.
54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * The ASF licenses this file to You under the Apache License, Version 2.0
64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (the "License"); you may not use this file except in compliance with
74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * the License.  You may obtain a copy of the License at
84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing, software
124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See the License for the specific language governing permissions and
154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * limitations under the License.
164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.commons.io.filefilter;
184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.File;
204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.Serializable;
214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/**
234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Filters files based on size, can filter either smaller files or
244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * files equal to or larger than a given threshold.
254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * For example, to print all files and directories in the
274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * current directory whose size is greater than 1 MB:
284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <pre>
304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * File dir = new File(".");
314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * String[] files = dir.list( new SizeFileFilter(1024 * 1024) );
324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * for ( int i = 0; i &lt; files.length; i++ ) {
334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *     System.out.println(files[i]);
344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * }
354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * </pre>
364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @author Rahul Akolkar
384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Id: SizeFileFilter.java 591058 2007-11-01 15:47:05Z niallp $
394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @since Commons IO 1.2
404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic class SizeFileFilter extends AbstractFileFilter implements Serializable {
424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** The size threshold. */
444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private final long size;
454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** Whether the files accepted will be larger or smaller. */
464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private final boolean acceptLarger;
474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Constructs a new size file filter for files equal to or
504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * larger than a certain size.
514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param size  the threshold size of the files
534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IllegalArgumentException if the size is negative
544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public SizeFileFilter(long size) {
564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this(size, true);
574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Constructs a new size file filter for files based on a certain size
614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * threshold.
624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param size  the threshold size of the files
644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param acceptLarger  if true, files equal to or larger are accepted,
654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * otherwise smaller ones (but not equal to)
664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IllegalArgumentException if the size is negative
674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public SizeFileFilter(long size, boolean acceptLarger) {
694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (size < 0) {
704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            throw new IllegalArgumentException("The size must be non-negative");
714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.size = size;
734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.acceptLarger = acceptLarger;
744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    //-----------------------------------------------------------------------
774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Checks to see if the size of the file is favorable.
794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * <p>
804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * If size equals threshold and smaller files are required,
814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * file <b>IS NOT</b> selected.
824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * If size equals threshold and larger files are required,
834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * file <b>IS</b> selected.
844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param file  the File to check
864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return true if the filename matches
874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public boolean accept(File file) {
894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        boolean smaller = file.length() < size;
904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return acceptLarger ? !smaller : smaller;
914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Provide a String representaion of this file filter.
954fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
964fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return a String representaion
974fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
984fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public String toString() {
994fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        String condition = acceptLarger ? ">=" : "<";
1004fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return super.toString() + "(" + condition + size + ")";
1014fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1024fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1034fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy}
104