| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SizeFileFilter |
|
| 2.0;2 |
| 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 < files.length; i++ ) { | |
| 33 | * System.out.println(files[i]); | |
| 34 | * } | |
| 35 | * </pre> | |
| 36 | * | |
| 37 | * @version $Id: SizeFileFilter.java 1415850 2012-11-30 20:51:39Z ggregory $ | |
| 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 | /** The size threshold. */ | |
| 46 | private final long size; | |
| 47 | /** Whether the files accepted will be larger or smaller. */ | |
| 48 | private final boolean acceptLarger; | |
| 49 | ||
| 50 | /** | |
| 51 | * Constructs a new size file filter for files equal to or | |
| 52 | * larger than a certain size. | |
| 53 | * | |
| 54 | * @param size the threshold size of the files | |
| 55 | * @throws IllegalArgumentException if the size is negative | |
| 56 | */ | |
| 57 | public SizeFileFilter(final long size) { | |
| 58 | 2 | this(size, true); |
| 59 | 1 | } |
| 60 | ||
| 61 | /** | |
| 62 | * Constructs a new size file filter for files based on a certain size | |
| 63 | * threshold. | |
| 64 | * | |
| 65 | * @param size the threshold size of the files | |
| 66 | * @param acceptLarger if true, files equal to or larger are accepted, | |
| 67 | * otherwise smaller ones (but not equal to) | |
| 68 | * @throws IllegalArgumentException if the size is negative | |
| 69 | */ | |
| 70 | 14 | public SizeFileFilter(final long size, final boolean acceptLarger) { |
| 71 | 14 | if (size < 0) { |
| 72 | 1 | throw new IllegalArgumentException("The size must be non-negative"); |
| 73 | } | |
| 74 | 13 | this.size = size; |
| 75 | 13 | this.acceptLarger = acceptLarger; |
| 76 | 13 | } |
| 77 | ||
| 78 | //----------------------------------------------------------------------- | |
| 79 | /** | |
| 80 | * Checks to see if the size of the file is favorable. | |
| 81 | * <p> | |
| 82 | * If size equals threshold and smaller files are required, | |
| 83 | * file <b>IS NOT</b> selected. | |
| 84 | * If size equals threshold and larger files are required, | |
| 85 | * file <b>IS</b> selected. | |
| 86 | * | |
| 87 | * @param file the File to check | |
| 88 | * @return true if the filename matches | |
| 89 | */ | |
| 90 | @Override | |
| 91 | public boolean accept(final File file) { | |
| 92 | 44 | final boolean smaller = file.length() < size; |
| 93 | 44 | return acceptLarger ? !smaller : smaller; |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Provide a String representaion of this file filter. | |
| 98 | * | |
| 99 | * @return a String representaion | |
| 100 | */ | |
| 101 | @Override | |
| 102 | public String toString() { | |
| 103 | 24 | final String condition = acceptLarger ? ">=" : "<"; |
| 104 | 24 | return super.toString() + "(" + condition + size + ")"; |
| 105 | } | |
| 106 | ||
| 107 | } |