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.vfs2.filter; 18 19 import java.io.Serializable; 20 import java.util.ArrayList; 21 import java.util.Arrays; 22 import java.util.List; 23 24 import org.apache.commons.vfs2.FileFilter; 25 import org.apache.commons.vfs2.FileSelectInfo; 26 27 /** 28 * Filters file names for a certain name. 29 * <p> 30 * For example, to print all files and directories in the current directory 31 * whose name is {@code Test}: 32 * </p> 33 * 34 * <pre> 35 * FileSystemManager fsManager = VFS.getManager(); 36 * FileObject dir = fsManager.toFileObject(new File(".")); 37 * FileObject[] files = dir.findFiles(new FileFilterSelector(new NameFileFilter("Test"))); 38 * for (int i = 0; i < files.length; i++) { 39 * System.out.println(files[i]); 40 * } 41 * </pre> 42 * 43 * @author This code was originally ported from Apache Commons IO File Filter 44 * @see "http://commons.apache.org/proper/commons-io/" 45 * @since 2.4 46 */ 47 public class NameFileFilter implements FileFilter, Serializable { 48 49 private static final long serialVersionUID = 1L; 50 51 /** Whether the comparison is case sensitive. */ 52 private final IOCase caseSensitivity; 53 54 /** The file names to search for. */ 55 private final List<String> names; 56 57 /** 58 * Constructs a new case-sensitive name file filter for a list of names. 59 * 60 * @param names the names to allow, must not be null 61 */ 62 public NameFileFilter(final List<String> names) { 63 this((IOCase) null, names); 64 } 65 66 /** 67 * Constructs a new name file filter for a list of names specifying 68 * case-sensitivity. 69 * 70 * @param caseSensitivity how to handle case sensitivity, null means 71 * case-sensitive 72 * @param names the names to allow, must not be null 73 */ 74 public NameFileFilter(final IOCase caseSensitivity, final List<String> names) { 75 if (names == null) { 76 throw new IllegalArgumentException("The list of names must not be null"); 77 } 78 this.names = new ArrayList<>(names); 79 this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; 80 } 81 82 /** 83 * Constructs a new case-sensitive name file filter for an array of names. 84 * <p> 85 * The array is not cloned, so could be changed after constructing the instance. 86 * This would be inadvisable however. 87 * </p> 88 * 89 * @param names the names to allow, must not be null 90 */ 91 public NameFileFilter(final String... names) { 92 this((IOCase) null, names); 93 } 94 95 /** 96 * Constructs a new name file filter for an array of names specifying 97 * case-sensitivity. 98 * 99 * @param caseSensitivity how to handle case sensitivity, null means 100 * case-sensitive 101 * @param names the names to allow, must not be null 102 */ 103 public NameFileFilter(final IOCase caseSensitivity, final String... names) { 104 if (names == null) { 105 throw new IllegalArgumentException("The array of names must not be null"); 106 } 107 this.names = new ArrayList<>(Arrays.asList(names)); 108 this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity; 109 } 110 111 /** 112 * Checks to see if the file name matches. 113 * 114 * @param fileSelectInfo the File to check 115 * 116 * @return true if the file name matches 117 */ 118 @Override 119 public boolean accept(final FileSelectInfo fileSelectInfo) { 120 final String name = fileSelectInfo.getFile().getName().getBaseName(); 121 for (final String name2 : this.names) { 122 if (caseSensitivity.checkEquals(name, name2)) { 123 return true; 124 } 125 } 126 return false; 127 } 128 129 /** 130 * Provide a String representation of this file filter. 131 * 132 * @return a String representation 133 */ 134 @Override 135 public String toString() { 136 final StringBuilder buffer = new StringBuilder(); 137 buffer.append(super.toString()); 138 buffer.append("("); 139 if (names != null) { 140 for (int i = 0; i < names.size(); i++) { 141 if (i > 0) { 142 buffer.append(","); 143 } 144 buffer.append(names.get(i)); 145 } 146 } 147 buffer.append(")"); 148 return buffer.toString(); 149 } 150 151 }