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.io.filefilter; 18 19 import java.io.File; 20 import java.util.List; 21 22 /** 23 * Filters files based on the suffix (what the filename ends with). 24 * This is used in retrieving all the files of a particular type. 25 * <p> 26 * For example, to retrieve and print all <code>*.java</code> files 27 * in the current directory: 28 * 29 * <pre> 30 * File dir = new File("."); 31 * String[] files = dir.list( new SuffixFileFilter(".java") ); 32 * for (int i = 0; i < files.length; i++) { 33 * System.out.println(files[i]); 34 * } 35 * </pre> 36 * 37 * @since Commons IO 1.0 38 * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $ 39 * 40 * @author Stephen Colebourne 41 * @author Federico Barbieri 42 * @author Serge Knystautas 43 * @author Peter Donald 44 */ 45 public class SuffixFileFilter extends AbstractFileFilter { 46 47 /** The filename suffixes to search for */ 48 private String[] suffixes; 49 50 /** 51 * Constructs a new Suffix file filter for a single extension. 52 * 53 * @param suffix the suffix to allow, must not be null 54 * @throws IllegalArgumentException if the suffix is null 55 */ 56 public SuffixFileFilter(String suffix) { 57 if (suffix == null) { 58 throw new IllegalArgumentException("The suffix must not be null"); 59 } 60 this.suffixes = new String[] {suffix}; 61 } 62 63 /** 64 * Constructs a new Suffix file filter for an array of suffixs. 65 * <p> 66 * The array is not cloned, so could be changed after constructing the 67 * instance. This would be inadvisable however. 68 * 69 * @param suffixes the suffixes to allow, must not be null 70 * @throws IllegalArgumentException if the suffix array is null 71 */ 72 public SuffixFileFilter(String[] suffixes) { 73 if (suffixes == null) { 74 throw new IllegalArgumentException("The array of suffixes must not be null"); 75 } 76 this.suffixes = suffixes; 77 } 78 79 /** 80 * Constructs a new Suffix file filter for a list of suffixes. 81 * 82 * @param suffixes the suffixes to allow, must not be null 83 * @throws IllegalArgumentException if the suffix list is null 84 * @throws ClassCastException if the list does not contain Strings 85 */ 86 public SuffixFileFilter(List suffixes) { 87 if (suffixes == null) { 88 throw new IllegalArgumentException("The list of suffixes must not be null"); 89 } 90 this.suffixes = (String[]) suffixes.toArray(new String[suffixes.size()]); 91 } 92 93 /** 94 * Checks to see if the filename ends with the suffix. 95 * 96 * @param file the File to check 97 * @return true if the filename ends with one of our suffixes 98 */ 99 public boolean accept(File file) { 100 String name = file.getName(); 101 for (int i = 0; i < this.suffixes.length; i++) { 102 if (name.endsWith(this.suffixes[i])) { 103 return true; 104 } 105 } 106 return false; 107 } 108 109 /** 110 * Checks to see if the filename ends with the suffix. 111 * 112 * @param file the File directory 113 * @param name the filename 114 * @return true if the filename ends with one of our suffixes 115 */ 116 public boolean accept(File file, String name) { 117 for (int i = 0; i < this.suffixes.length; i++) { 118 if (name.endsWith(this.suffixes[i])) { 119 return true; 120 } 121 } 122 return false; 123 } 124 125 }