You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by je...@apache.org on 2003/11/22 21:01:27 UTC
cvs commit: jakarta-commons-sandbox/io/src/java/org/apache/commons/io/filefilter NameFileFilter.java
jeremias 2003/11/22 12:01:27
Added: io/src/java/org/apache/commons/io/filefilter
NameFileFilter.java
Log:
Filters for the whole name, not just the prefix or postfix.
Revision Changes Path
1.1 jakarta-commons-sandbox/io/src/java/org/apache/commons/io/filefilter/NameFileFilter.java
Index: NameFileFilter.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.io.filefilter;
import java.io.File;
import java.util.List;
/**
* Filters filenames for a certain name.
* <p>
* For example, to print all files and directories in the
* current directory whose name is <code>Test</code>:
*
* <pre>
* File dir = new File(".");
* String[] files = dir.list( new NameFileFilter("Test") );
* for ( int i = 0; i < files.length; i++ ) {
* System.out.println(files[i]);
* }
* </pre>
*
* @since Commons IO 1.0
* @version $Revision: 1.1 $ $Date: 2003/11/22 20:01:27 $
*
* @author Henri Yandell
* @author Stephen Colebourne
* @author Federico Barbieri
* @author Serge Knystautas
* @author Peter Donald
*/
public class NameFileFilter extends AbstractFileFilter {
/** The filenames to search for */
private String[] names;
/**
* Constructs a new name file filter for a single name.
*
* @param name the name to allow, must not be null
* @throws IllegalArgumentException if the prefix is null
*/
public NameFileFilter(final String name) {
if (name == null) {
throw new IllegalArgumentException("The name must not be null");
}
this.names = new String[] {name};
}
/**
* Constructs a new name file filter for any of an array of names.
* <p>
* The array is not cloned, so could be changed after constructing the
* instance. This would be inadvisable however.
*
* @param names the names to allow, must not be null
* @throws IllegalArgumentException if the names array is null
*/
public NameFileFilter(final String[] names) {
if (names == null) {
throw new IllegalArgumentException("The array of names must not be null");
}
this.names = names;
}
/**
* Constructs a new name file filter for a list of names.
*
* @param names the names to allow, must not be null
* @throws IllegalArgumentException if the name list is null
* @throws ClassCastException if the list does not contain Strings
*/
public NameFileFilter(final List names) {
if (names == null) {
throw new IllegalArgumentException("The list of names must not be null");
}
this.names = (String[]) names.toArray(new String[names.size()]);
}
/**
* Checks to see if the filename matches.
*
* @param file the File to check
* @return true if the filename matches
*/
public boolean accept(final File file) {
String name = file.getName();
for (int i = 0; i < this.names.length; i++) {
if (name.equals(this.names[i])) {
return true;
}
}
return false;
}
/**
* Checks to see if the filename matches.
*
* @param file the File directory
* @param name the filename
* @return true if the filename matches
*/
public boolean accept(final File file, final String name) {
for (int i = 0; i < this.names.length; i++) {
if (name.equals(this.names[i])) {
return true;
}
}
return false;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org