|
| 1 | +/* ==================================================================== |
| 2 | + * The Apache Software License, Version 1.1 |
| 3 | + * |
| 4 | + * Copyright (c) 2002-2003 The Apache Software Foundation. All rights |
| 5 | + * reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer in |
| 16 | + * the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * |
| 19 | + * 3. The end-user documentation included with the redistribution, if |
| 20 | + * any, must include the following acknowledgement: |
| 21 | + * "This product includes software developed by the |
| 22 | + * Apache Software Foundation (http://www.apache.org/)." |
| 23 | + * Alternately, this acknowledgement may appear in the software itself, |
| 24 | + * if and wherever such third-party acknowledgements normally appear. |
| 25 | + * |
| 26 | + * 4. The names "The Jakarta Project", "Commons", and "Apache Software |
| 27 | + * Foundation" must not be used to endorse or promote products derived |
| 28 | + * from this software without prior written permission. For written |
| 29 | + * permission, please contact apache@apache.org. |
| 30 | + * |
| 31 | + * 5. Products derived from this software may not be called "Apache" |
| 32 | + * nor may "Apache" appear in their names without prior written |
| 33 | + * permission of the Apache Software Foundation. |
| 34 | + * |
| 35 | + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 36 | + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 37 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 38 | + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| 39 | + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 42 | + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 43 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 44 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 45 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 46 | + * SUCH DAMAGE. |
| 47 | + * ==================================================================== |
| 48 | + * |
| 49 | + * This software consists of voluntary contributions made by many |
| 50 | + * individuals on behalf of the Apache Software Foundation. For more |
| 51 | + * information on the Apache Software Foundation, please see |
| 52 | + * <http://www.apache.org/>. |
| 53 | + */ |
| 54 | +package org.apache.commons.io.filefilter; |
| 55 | + |
| 56 | +import java.io.File; |
| 57 | +import java.util.List; |
| 58 | + |
| 59 | +/** |
| 60 | + * Filters filenames for a certain name. |
| 61 | + * <p> |
| 62 | + * For example, to print all files and directories in the |
| 63 | + * current directory whose name is <code>Test</code>: |
| 64 | + * |
| 65 | + * <pre> |
| 66 | + * File dir = new File("."); |
| 67 | + * String[] files = dir.list( new NameFileFilter("Test") ); |
| 68 | + * for ( int i = 0; i < files.length; i++ ) { |
| 69 | + * System.out.println(files[i]); |
| 70 | + * } |
| 71 | + * </pre> |
| 72 | + * |
| 73 | + * @since Commons IO 1.0 |
| 74 | + * @version $Revision: 1.1 $ $Date: 2003/11/22 20:01:27 $ |
| 75 | + * |
| 76 | + * @author Henri Yandell |
| 77 | + * @author Stephen Colebourne |
| 78 | + * @author Federico Barbieri |
| 79 | + * @author Serge Knystautas |
| 80 | + * @author Peter Donald |
| 81 | + */ |
| 82 | +public class NameFileFilter extends AbstractFileFilter { |
| 83 | + |
| 84 | + /** The filenames to search for */ |
| 85 | + private String[] names; |
| 86 | + |
| 87 | + /** |
| 88 | + * Constructs a new name file filter for a single name. |
| 89 | + * |
| 90 | + * @param name the name to allow, must not be null |
| 91 | + * @throws IllegalArgumentException if the prefix is null |
| 92 | + */ |
| 93 | + public NameFileFilter(final String name) { |
| 94 | + if (name == null) { |
| 95 | + throw new IllegalArgumentException("The name must not be null"); |
| 96 | + } |
| 97 | + this.names = new String[] {name}; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Constructs a new name file filter for any of an array of names. |
| 102 | + * <p> |
| 103 | + * The array is not cloned, so could be changed after constructing the |
| 104 | + * instance. This would be inadvisable however. |
| 105 | + * |
| 106 | + * @param names the names to allow, must not be null |
| 107 | + * @throws IllegalArgumentException if the names array is null |
| 108 | + */ |
| 109 | + public NameFileFilter(final String[] names) { |
| 110 | + if (names == null) { |
| 111 | + throw new IllegalArgumentException("The array of names must not be null"); |
| 112 | + } |
| 113 | + this.names = names; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Constructs a new name file filter for a list of names. |
| 118 | + * |
| 119 | + * @param names the names to allow, must not be null |
| 120 | + * @throws IllegalArgumentException if the name list is null |
| 121 | + * @throws ClassCastException if the list does not contain Strings |
| 122 | + */ |
| 123 | + public NameFileFilter(final List names) { |
| 124 | + if (names == null) { |
| 125 | + throw new IllegalArgumentException("The list of names must not be null"); |
| 126 | + } |
| 127 | + this.names = (String[]) names.toArray(new String[names.size()]); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Checks to see if the filename matches. |
| 132 | + * |
| 133 | + * @param file the File to check |
| 134 | + * @return true if the filename matches |
| 135 | + */ |
| 136 | + public boolean accept(final File file) { |
| 137 | + String name = file.getName(); |
| 138 | + for (int i = 0; i < this.names.length; i++) { |
| 139 | + if (name.equals(this.names[i])) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + } |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Checks to see if the filename matches. |
| 148 | + * |
| 149 | + * @param file the File directory |
| 150 | + * @param name the filename |
| 151 | + * @return true if the filename matches |
| 152 | + */ |
| 153 | + public boolean accept(final File file, final String name) { |
| 154 | + for (int i = 0; i < this.names.length; i++) { |
| 155 | + if (name.equals(this.names[i])) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments