| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NameFileFilter |
|
| 3.0;3 |
| 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 | import java.util.List; | |
| 22 | ||
| 23 | import org.apache.commons.io.IOCase; | |
| 24 | ||
| 25 | /** | |
| 26 | * Filters filenames for a certain name. | |
| 27 | * <p> | |
| 28 | * For example, to print all files and directories in the | |
| 29 | * current directory whose name is <code>Test</code>: | |
| 30 | * | |
| 31 | * <pre> | |
| 32 | * File dir = new File("."); | |
| 33 | * String[] files = dir.list( new NameFileFilter("Test") ); | |
| 34 | * for ( int i = 0; i < files.length; i++ ) { | |
| 35 | * System.out.println(files[i]); | |
| 36 | * } | |
| 37 | * </pre> | |
| 38 | * | |
| 39 | * @since 1.0 | |
| 40 | * @version $Id: NameFileFilter.java 1471767 2013-04-24 23:24:19Z sebb $ | |
| 41 | * @see FileFilterUtils#nameFileFilter(String) | |
| 42 | * @see FileFilterUtils#nameFileFilter(String, IOCase) | |
| 43 | */ | |
| 44 | public class NameFileFilter extends AbstractFileFilter implements Serializable { | |
| 45 | ||
| 46 | /** The filenames to search for */ | |
| 47 | private final String[] names; | |
| 48 | /** Whether the comparison is case sensitive. */ | |
| 49 | private final IOCase caseSensitivity; | |
| 50 | ||
| 51 | /** | |
| 52 | * Constructs a new case-sensitive name file filter for a single name. | |
| 53 | * | |
| 54 | * @param name the name to allow, must not be null | |
| 55 | * @throws IllegalArgumentException if the name is null | |
| 56 | */ | |
| 57 | public NameFileFilter(final String name) { | |
| 58 | 28 | this(name, null); |
| 59 | 27 | } |
| 60 | ||
| 61 | /** | |
| 62 | * Construct a new name file filter specifying case-sensitivity. | |
| 63 | * | |
| 64 | * @param name the name to allow, must not be null | |
| 65 | * @param caseSensitivity how to handle case sensitivity, null means case-sensitive | |
| 66 | * @throws IllegalArgumentException if the name is null | |
| 67 | */ | |
| 68 | 30 | public NameFileFilter(final String name, final IOCase caseSensitivity) { |
| 69 | 30 | if (name == null) { |
| 70 | 2 | throw new IllegalArgumentException("The wildcard must not be null"); |
| 71 | } | |
| 72 | 28 | this.names = new String[] {name}; |
| 73 | 28 | this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; |
| 74 | 28 | } |
| 75 | ||
| 76 | /** | |
| 77 | * Constructs a new case-sensitive name file filter for an array of names. | |
| 78 | * <p> | |
| 79 | * The array is not cloned, so could be changed after constructing the | |
| 80 | * instance. This would be inadvisable however. | |
| 81 | * | |
| 82 | * @param names the names to allow, must not be null | |
| 83 | * @throws IllegalArgumentException if the names array is null | |
| 84 | */ | |
| 85 | public NameFileFilter(final String[] names) { | |
| 86 | 9 | this(names, null); |
| 87 | 8 | } |
| 88 | ||
| 89 | /** | |
| 90 | * Constructs a new name file filter for an array of names specifying case-sensitivity. | |
| 91 | * | |
| 92 | * @param names the names to allow, must not be null | |
| 93 | * @param caseSensitivity how to handle case sensitivity, null means case-sensitive | |
| 94 | * @throws IllegalArgumentException if the names array is null | |
| 95 | */ | |
| 96 | 13 | public NameFileFilter(final String[] names, final IOCase caseSensitivity) { |
| 97 | 13 | if (names == null) { |
| 98 | 1 | throw new IllegalArgumentException("The array of names must not be null"); |
| 99 | } | |
| 100 | 12 | this.names = new String[names.length]; |
| 101 | 12 | System.arraycopy(names, 0, this.names, 0, names.length); |
| 102 | 12 | this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; |
| 103 | 12 | } |
| 104 | ||
| 105 | /** | |
| 106 | * Constructs a new case-sensitive name file filter for a list of names. | |
| 107 | * | |
| 108 | * @param names the names to allow, must not be null | |
| 109 | * @throws IllegalArgumentException if the name list is null | |
| 110 | * @throws ClassCastException if the list does not contain Strings | |
| 111 | */ | |
| 112 | public NameFileFilter(final List<String> names) { | |
| 113 | 2 | this(names, null); |
| 114 | 1 | } |
| 115 | ||
| 116 | /** | |
| 117 | * Constructs a new name file filter for a list of names specifying case-sensitivity. | |
| 118 | * | |
| 119 | * @param names the names to allow, must not be null | |
| 120 | * @param caseSensitivity how to handle case sensitivity, null means case-sensitive | |
| 121 | * @throws IllegalArgumentException if the name list is null | |
| 122 | * @throws ClassCastException if the list does not contain Strings | |
| 123 | */ | |
| 124 | 2 | public NameFileFilter(final List<String> names, final IOCase caseSensitivity) { |
| 125 | 2 | if (names == null) { |
| 126 | 1 | throw new IllegalArgumentException("The list of names must not be null"); |
| 127 | } | |
| 128 | 1 | this.names = names.toArray(new String[names.size()]); |
| 129 | 1 | this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; |
| 130 | 1 | } |
| 131 | ||
| 132 | //----------------------------------------------------------------------- | |
| 133 | /** | |
| 134 | * Checks to see if the filename matches. | |
| 135 | * | |
| 136 | * @param file the File to check | |
| 137 | * @return true if the filename matches | |
| 138 | */ | |
| 139 | @Override | |
| 140 | public boolean accept(final File file) { | |
| 141 | 995 | final String name = file.getName(); |
| 142 | 3452 | for (final String name2 : this.names) { |
| 143 | 2564 | if (caseSensitivity.checkEquals(name, name2)) { |
| 144 | 107 | return true; |
| 145 | } | |
| 146 | } | |
| 147 | 888 | return false; |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * Checks to see if the filename matches. | |
| 152 | * | |
| 153 | * @param dir the File directory (ignored) | |
| 154 | * @param name the filename | |
| 155 | * @return true if the filename matches | |
| 156 | */ | |
| 157 | @Override | |
| 158 | public boolean accept(final File dir, final String name) { | |
| 159 | 22 | for (final String name2 : names) { |
| 160 | 14 | if (caseSensitivity.checkEquals(name, name2)) { |
| 161 | 6 | return true; |
| 162 | } | |
| 163 | } | |
| 164 | 8 | return false; |
| 165 | } | |
| 166 | ||
| 167 | /** | |
| 168 | * Provide a String representaion of this file filter. | |
| 169 | * | |
| 170 | * @return a String representaion | |
| 171 | */ | |
| 172 | @Override | |
| 173 | public String toString() { | |
| 174 | 64 | final StringBuilder buffer = new StringBuilder(); |
| 175 | 64 | buffer.append(super.toString()); |
| 176 | 64 | buffer.append("("); |
| 177 | 64 | if (names != null) { |
| 178 | 150 | for (int i = 0; i < names.length; i++) { |
| 179 | 86 | if (i > 0) { |
| 180 | 22 | buffer.append(","); |
| 181 | } | |
| 182 | 86 | buffer.append(names[i]); |
| 183 | } | |
| 184 | } | |
| 185 | 64 | buffer.append(")"); |
| 186 | 64 | return buffer.toString(); |
| 187 | } | |
| 188 | ||
| 189 | } |