View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.filefilter;
18  
19  import java.io.File;
20  import java.io.Serializable;
21  
22  /**
23   * Filters files based on size, can filter either smaller files or
24   * files equal to or larger than a given threshold.
25   * <p>
26   * For example, to print all files and directories in the
27   * current directory whose size is greater than 1 MB:
28   *
29   * <pre>
30   * File dir = new File(".");
31   * String[] files = dir.list( new SizeFileFilter(1024 * 1024) );
32   * for ( int i = 0; i &lt; files.length; i++ ) {
33   *     System.out.println(files[i]);
34   * }
35   * </pre>
36   *
37   * @version $Id: SizeFileFilter.java 1642757 2014-12-01 21:09:30Z sebb $
38   * @since 1.2
39   * @see FileFilterUtils#sizeFileFilter(long)
40   * @see FileFilterUtils#sizeFileFilter(long, boolean)
41   * @see FileFilterUtils#sizeRangeFileFilter(long, long)
42   */
43  public class SizeFileFilter extends AbstractFileFilter implements Serializable {
44  
45      private static final long serialVersionUID = 7388077430788600069L;
46      /** The size threshold. */
47      private final long size;
48      /** Whether the files accepted will be larger or smaller. */
49      private final boolean acceptLarger;
50  
51      /**
52       * Constructs a new size file filter for files equal to or 
53       * larger than a certain size.
54       *
55       * @param size  the threshold size of the files
56       * @throws IllegalArgumentException if the size is negative
57       */
58      public SizeFileFilter(final long size) {
59          this(size, true);
60      }
61  
62      /**
63       * Constructs a new size file filter for files based on a certain size
64       * threshold.
65       *
66       * @param size  the threshold size of the files
67       * @param acceptLarger  if true, files equal to or larger are accepted,
68       * otherwise smaller ones (but not equal to)
69       * @throws IllegalArgumentException if the size is negative
70       */
71      public SizeFileFilter(final long size, final boolean acceptLarger) {
72          if (size < 0) {
73              throw new IllegalArgumentException("The size must be non-negative");
74          }
75          this.size = size;
76          this.acceptLarger = acceptLarger;
77      }
78  
79      //-----------------------------------------------------------------------
80      /**
81       * Checks to see if the size of the file is favorable.
82       * <p>
83       * If size equals threshold and smaller files are required,
84       * file <b>IS NOT</b> selected.
85       * If size equals threshold and larger files are required,
86       * file <b>IS</b> selected.
87       *
88       * @param file  the File to check
89       * @return true if the filename matches
90       */
91      @Override
92      public boolean accept(final File file) {
93          final boolean smaller = file.length() < size;
94          return acceptLarger ? !smaller : smaller;
95      }
96  
97      /**
98       * Provide a String representaion of this file filter.
99       *
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 }