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.finder.filters;
18  
19  import java.io.File;
20  import org.apache.commons.io.filefilter.AbstractFileFilter;
21  
22  /**
23   * <code>IOFileFilter</code> implementation that matches on the file size.
24   * 
25   * @version $Id: SizeFilter.java 437543 2006-08-28 05:47:51Z bayard $
26   * @since 0.1
27   */
28  public class SizeFilter extends AbstractFileFilter {
29  
30      private long size;
31      private long factor = 1L;
32      private char comparator = '=';
33      private boolean roundUp = true;
34      private String origSize;
35  
36      /**
37       * Construct a new <code>IOFileFilter</code> specifying the
38       * file size.
39       * 
40       * @param size The minimum file size.
41       */
42      public SizeFilter(String size) {
43          this(size, true);
44      }
45  
46      /**
47       * Construct a new <code>IOFileFilter</code> specifying the
48       * file size.
49       * 
50       * @param size The minimum file size.
51       * @param roundUp Whether equals compares should round up or down.
52       */
53      public SizeFilter(String size, boolean roundUp) {
54          super();
55          this.roundUp = roundUp;
56          this.origSize = origSize;
57          String parseSize = (size == null ? "" : size.trim());
58          if (parseSize.length() > 0) {
59              if (parseSize.charAt(0) == '+') {
60                  comparator = '>';
61                  parseSize = parseSize.substring(1);
62              } else if (parseSize.charAt(0) == '-') {
63                  comparator = '<';
64                  parseSize = parseSize.substring(1);
65              }
66          }
67  
68          if (parseSize.length() > 0) {
69              int lastIdx = parseSize.length() - 1;
70              char lastChar = parseSize.charAt(lastIdx);
71              lastChar = Character.isLetter(lastChar) ? Character.toLowerCase(lastChar) : lastChar;
72              if (lastChar == 'k') {
73                  factor = 1024L;
74              } else if (lastChar == 'm') {
75                  factor = 1024L * 1024L;
76              } else if (lastChar == 'g') {
77                  factor = 1024L * 1024L * 1024L;
78              }
79              if (factor > 1L) {
80                  parseSize = parseSize.substring(0, lastIdx);
81              }
82          }
83          try {
84              this.size = (Long.parseLong(parseSize) * factor);
85          } catch(NumberFormatException nfe) {
86              throw new IllegalArgumentException("Argument " + size + " must be an integer.");
87          }
88          
89      }
90  
91      /**
92       * Return a String representation of this class.
93       * @return a String representation of the class.
94       */
95      public String toString() {
96          StringBuffer buffer = new StringBuffer(super.toString());
97          buffer.append(", size ");
98          buffer.append(comparator);
99          buffer.append(" ");
100         buffer.append(origSize);
101         if (comparator == '=' && factor > 1L) {
102             buffer.append(", roundUp=");
103             buffer.append(roundUp);
104         }
105         return buffer.toString();
106     }
107 
108     /**
109      * Tests whether the {@link File} is
110      * a specified size.
111      * 
112      * @param file The {@link File} to test.
113      * @return <code>true</code> if the {@link File}
114      * is a specified size, otherwise
115      * <code>false</code>.
116      */
117     public boolean accept(File file) {
118         long fileSize = file.length();
119         switch (comparator) {
120             case '>':
121                 return (fileSize > size);
122             case '<':
123                 return (fileSize < size);
124             default:
125                 if (roundUp) {
126                     return (fileSize > (size - factor) && fileSize <= size);
127                 } else {
128                     return (fileSize >= size && fileSize < (size + factor));
129                 }
130         }
131     }
132 }