All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.vfs2.filter.NameFileFilter Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.vfs2.filter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.vfs2.FileFilter;
import org.apache.commons.vfs2.FileSelectInfo;

/**
 * Filters file names for a certain name.
 * 

* For example, to print all files and directories in the current directory * whose name is {@code Test}: *

* *
 * FileSystemManager fsManager = VFS.getManager();
 * FileObject dir = fsManager.toFileObject(new File("."));
 * FileObject[] files = dir.findFiles(new FileFilterSelector(new NameFileFilter("Test")));
 * for (int i = 0; i < files.length; i++) {
 *     System.out.println(files[i]);
 * }
 * 
* * @author This code was originally ported from Apache Commons IO File Filter * @see "http://commons.apache.org/proper/commons-io/" * @since 2.4 */ public class NameFileFilter implements FileFilter, Serializable { private static final long serialVersionUID = 1L; /** Whether the comparison is case sensitive. */ private final IOCase caseSensitivity; /** The file names to search for. */ private final List names; /** * Constructs a new case-sensitive name file filter for a list of names. * * @param names the names to allow, must not be null */ public NameFileFilter(final List names) { this((IOCase) null, names); } /** * Constructs a new name file filter for a list of names specifying * case-sensitivity. * * @param caseSensitivity how to handle case sensitivity, null means * case-sensitive * @param names the names to allow, must not be null */ public NameFileFilter(final IOCase caseSensitivity, final List names) { if (names == null) { throw new IllegalArgumentException("The list of names must not be null"); } this.names = new ArrayList<>(names); this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; } /** * Constructs a new case-sensitive name file filter for an array of names. *

* 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 */ public NameFileFilter(final String... names) { this((IOCase) null, names); } /** * Constructs a new name file filter for an array of names specifying * case-sensitivity. * * @param caseSensitivity how to handle case sensitivity, null means * case-sensitive * @param names the names to allow, must not be null */ public NameFileFilter(final IOCase caseSensitivity, final String... names) { if (names == null) { throw new IllegalArgumentException("The array of names must not be null"); } this.names = new ArrayList<>(Arrays.asList(names)); this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; } /** * Checks to see if the file name matches. * * @param fileInfo the File to check * * @return true if the file name matches */ @Override public boolean accept(final FileSelectInfo fileInfo) { final String name = fileInfo.getFile().getName().getBaseName(); for (final String name2 : this.names) { if (caseSensitivity.checkEquals(name, name2)) { return true; } } return false; } /** * Provide a String representation of this file filter. * * @return a String representation */ @Override public String toString() { final StringBuilder buffer = new StringBuilder(); buffer.append(super.toString()); buffer.append("("); if (names != null) { for (int i = 0; i < names.size(); i++) { if (i > 0) { buffer.append(","); } buffer.append(names.get(i)); } } buffer.append(")"); return buffer.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy