| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SizeFilter |
|
| 5.25;5.25 |
| 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 | 1 | private long factor = 1L; |
| 32 | 1 | private char comparator = '='; |
| 33 | 1 | 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 | 1 | this(size, true); |
| 44 | 1 | } |
| 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 | 1 | super(); |
| 55 | 1 | this.roundUp = roundUp; |
| 56 | 1 | this.origSize = origSize; |
| 57 | 1 | String parseSize = (size == null ? "" : size.trim()); |
| 58 | 1 | if (parseSize.length() > 0) { |
| 59 | 1 | if (parseSize.charAt(0) == '+') { |
| 60 | 0 | comparator = '>'; |
| 61 | 0 | parseSize = parseSize.substring(1); |
| 62 | 0 | } else if (parseSize.charAt(0) == '-') { |
| 63 | 0 | comparator = '<'; |
| 64 | 0 | parseSize = parseSize.substring(1); |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | 1 | if (parseSize.length() > 0) { |
| 69 | 1 | int lastIdx = parseSize.length() - 1; |
| 70 | 1 | char lastChar = parseSize.charAt(lastIdx); |
| 71 | 1 | lastChar = Character.isLetter(lastChar) ? Character.toLowerCase(lastChar) : lastChar; |
| 72 | 1 | if (lastChar == 'k') { |
| 73 | 1 | factor = 1024L; |
| 74 | 1 | } else if (lastChar == 'm') { |
| 75 | 0 | factor = 1024L * 1024L; |
| 76 | 0 | } else if (lastChar == 'g') { |
| 77 | 0 | factor = 1024L * 1024L * 1024L; |
| 78 | } | |
| 79 | 1 | if (factor > 1L) { |
| 80 | 1 | parseSize = parseSize.substring(0, lastIdx); |
| 81 | } | |
| 82 | } | |
| 83 | try { | |
| 84 | 1 | this.size = (Long.parseLong(parseSize) * factor); |
| 85 | 0 | } catch(NumberFormatException nfe) { |
| 86 | 0 | throw new IllegalArgumentException("Argument " + size + " must be an integer."); |
| 87 | 1 | } |
| 88 | ||
| 89 | 1 | } |
| 90 | ||
| 91 | /** | |
| 92 | * Return a String representation of this class. | |
| 93 | * @return a String representation of the class. | |
| 94 | */ | |
| 95 | public String toString() { | |
| 96 | 0 | StringBuffer buffer = new StringBuffer(super.toString()); |
| 97 | 0 | buffer.append(", size "); |
| 98 | 0 | buffer.append(comparator); |
| 99 | 0 | buffer.append(" "); |
| 100 | 0 | buffer.append(origSize); |
| 101 | 0 | if (comparator == '=' && factor > 1L) { |
| 102 | 0 | buffer.append(", roundUp="); |
| 103 | 0 | buffer.append(roundUp); |
| 104 | } | |
| 105 | 0 | 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 | 15 | long fileSize = file.length(); |
| 119 | 15 | switch (comparator) { |
| 120 | case '>': | |
| 121 | 0 | return (fileSize > size); |
| 122 | case '<': | |
| 123 | 0 | return (fileSize < size); |
| 124 | default: | |
| 125 | 15 | if (roundUp) { |
| 126 | 15 | return (fileSize > (size - factor) && fileSize <= size); |
| 127 | } else { | |
| 128 | 0 | return (fileSize >= size && fileSize < (size + factor)); |
| 129 | } | |
| 130 | } | |
| 131 | } | |
| 132 | } |