001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.io.filefilter; 018 019 import java.io.File; 020 import java.io.Serializable; 021 import java.util.List; 022 023 import org.apache.commons.io.IOCase; 024 025 /** 026 * Filters files based on the suffix (what the filename ends with). 027 * This is used in retrieving all the files of a particular type. 028 * <p> 029 * For example, to retrieve and print all <code>*.java</code> files 030 * in the current directory: 031 * 032 * <pre> 033 * File dir = new File("."); 034 * String[] files = dir.list( new SuffixFileFilter(".java") ); 035 * for (int i = 0; i < files.length; i++) { 036 * System.out.println(files[i]); 037 * } 038 * </pre> 039 * 040 * @since Commons IO 1.0 041 * @version $Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $ 042 * 043 * @author Stephen Colebourne 044 * @author Federico Barbieri 045 * @author Serge Knystautas 046 * @author Peter Donald 047 */ 048 public class SuffixFileFilter extends AbstractFileFilter implements Serializable { 049 050 /** The filename suffixes to search for */ 051 private final String[] suffixes; 052 053 /** Whether the comparison is case sensitive. */ 054 private final IOCase caseSensitivity; 055 056 /** 057 * Constructs a new Suffix file filter for a single extension. 058 * 059 * @param suffix the suffix to allow, must not be null 060 * @throws IllegalArgumentException if the suffix is null 061 */ 062 public SuffixFileFilter(String suffix) { 063 this(suffix, IOCase.SENSITIVE); 064 } 065 066 /** 067 * Constructs a new Suffix file filter for a single extension 068 * specifying case-sensitivity. 069 * 070 * @param suffix the suffix to allow, must not be null 071 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive 072 * @throws IllegalArgumentException if the suffix is null 073 * @since Commons IO 1.4 074 */ 075 public SuffixFileFilter(String suffix, IOCase caseSensitivity) { 076 if (suffix == null) { 077 throw new IllegalArgumentException("The suffix must not be null"); 078 } 079 this.suffixes = new String[] {suffix}; 080 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity); 081 } 082 083 /** 084 * Constructs a new Suffix file filter for an array of suffixs. 085 * <p> 086 * The array is not cloned, so could be changed after constructing the 087 * instance. This would be inadvisable however. 088 * 089 * @param suffixes the suffixes to allow, must not be null 090 * @throws IllegalArgumentException if the suffix array is null 091 */ 092 public SuffixFileFilter(String[] suffixes) { 093 this(suffixes, IOCase.SENSITIVE); 094 } 095 096 /** 097 * Constructs a new Suffix file filter for an array of suffixs 098 * specifying case-sensitivity. 099 * <p> 100 * The array is not cloned, so could be changed after constructing the 101 * instance. This would be inadvisable however. 102 * 103 * @param suffixes the suffixes to allow, must not be null 104 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive 105 * @throws IllegalArgumentException if the suffix array is null 106 * @since Commons IO 1.4 107 */ 108 public SuffixFileFilter(String[] suffixes, IOCase caseSensitivity) { 109 if (suffixes == null) { 110 throw new IllegalArgumentException("The array of suffixes must not be null"); 111 } 112 this.suffixes = suffixes; 113 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity); 114 } 115 116 /** 117 * Constructs a new Suffix file filter for a list of suffixes. 118 * 119 * @param suffixes the suffixes to allow, must not be null 120 * @throws IllegalArgumentException if the suffix list is null 121 * @throws ClassCastException if the list does not contain Strings 122 */ 123 public SuffixFileFilter(List suffixes) { 124 this(suffixes, IOCase.SENSITIVE); 125 } 126 127 /** 128 * Constructs a new Suffix file filter for a list of suffixes 129 * specifying case-sensitivity. 130 * 131 * @param suffixes the suffixes to allow, must not be null 132 * @param caseSensitivity how to handle case sensitivity, null means case-sensitive 133 * @throws IllegalArgumentException if the suffix list is null 134 * @throws ClassCastException if the list does not contain Strings 135 * @since Commons IO 1.4 136 */ 137 public SuffixFileFilter(List suffixes, IOCase caseSensitivity) { 138 if (suffixes == null) { 139 throw new IllegalArgumentException("The list of suffixes must not be null"); 140 } 141 this.suffixes = (String[]) suffixes.toArray(new String[suffixes.size()]); 142 this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity); 143 } 144 145 /** 146 * Checks to see if the filename ends with the suffix. 147 * 148 * @param file the File to check 149 * @return true if the filename ends with one of our suffixes 150 */ 151 public boolean accept(File file) { 152 String name = file.getName(); 153 for (int i = 0; i < this.suffixes.length; i++) { 154 if (caseSensitivity.checkEndsWith(name, suffixes[i])) { 155 return true; 156 } 157 } 158 return false; 159 } 160 161 /** 162 * Checks to see if the filename ends with the suffix. 163 * 164 * @param file the File directory 165 * @param name the filename 166 * @return true if the filename ends with one of our suffixes 167 */ 168 public boolean accept(File file, String name) { 169 for (int i = 0; i < this.suffixes.length; i++) { 170 if (caseSensitivity.checkEndsWith(name, suffixes[i])) { 171 return true; 172 } 173 } 174 return false; 175 } 176 177 /** 178 * Provide a String representaion of this file filter. 179 * 180 * @return a String representaion 181 */ 182 public String toString() { 183 StringBuffer buffer = new StringBuffer(); 184 buffer.append(super.toString()); 185 buffer.append("("); 186 if (suffixes != null) { 187 for (int i = 0; i < suffixes.length; i++) { 188 if (i > 0) { 189 buffer.append(","); 190 } 191 buffer.append(suffixes[i]); 192 } 193 } 194 buffer.append(")"); 195 return buffer.toString(); 196 } 197 198 }