View Javadoc

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.io.Serializable;
21  import java.util.List;
22  
23  import org.apache.commons.io.IOCase;
24  
25  /**
26   * Filters files based on the suffix (what the filename ends with).
27   * This is used in retrieving all the files of a particular type.
28   * <p>
29   * For example, to retrieve and print all <code>*.java</code> files 
30   * in the current directory:
31   *
32   * <pre>
33   * File dir = new File(".");
34   * String[] files = dir.list( new SuffixFileFilter(".java") );
35   * for (int i = 0; i &lt; files.length; i++) {
36   *     System.out.println(files[i]);
37   * }
38   * </pre>
39   *
40   * @since Commons IO 1.0
41   * @version $Revision: 619112 $ $Date: 2008-02-06 19:25:47 +0000 (Wed, 06 Feb 2008) $
42   * 
43   * @author Stephen Colebourne
44   * @author Federico Barbieri
45   * @author Serge Knystautas
46   * @author Peter Donald
47   */
48  public class SuffixFileFilter extends AbstractFileFilter implements Serializable {
49      
50      /** The filename suffixes to search for */
51      private final String[] suffixes;
52  
53      /** Whether the comparison is case sensitive. */
54      private final IOCase caseSensitivity;
55  
56      /**
57       * Constructs a new Suffix file filter for a single extension.
58       * 
59       * @param suffix  the suffix to allow, must not be null
60       * @throws IllegalArgumentException if the suffix is null
61       */
62      public SuffixFileFilter(String suffix) {
63          this(suffix, IOCase.SENSITIVE);
64      }
65  
66      /**
67       * Constructs a new Suffix file filter for a single extension
68       * specifying case-sensitivity.
69       *
70       * @param suffix  the suffix to allow, must not be null
71       * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
72       * @throws IllegalArgumentException if the suffix is null
73       * @since Commons IO 1.4
74       */
75      public SuffixFileFilter(String suffix, IOCase caseSensitivity) {
76          if (suffix == null) {
77              throw new IllegalArgumentException("The suffix must not be null");
78          }
79          this.suffixes = new String[] {suffix};
80          this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
81      }
82  
83      /**
84       * Constructs a new Suffix file filter for an array of suffixs.
85       * <p>
86       * The array is not cloned, so could be changed after constructing the
87       * instance. This would be inadvisable however.
88       * 
89       * @param suffixes  the suffixes to allow, must not be null
90       * @throws IllegalArgumentException if the suffix array is null
91       */
92      public SuffixFileFilter(String[] suffixes) {
93          this(suffixes, IOCase.SENSITIVE);
94      }
95  
96      /**
97       * Constructs a new Suffix file filter for an array of suffixs
98       * specifying case-sensitivity.
99       * <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<String> 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<String> suffixes, IOCase caseSensitivity) {
138         if (suffixes == null) {
139             throw new IllegalArgumentException("The list of suffixes must not be null");
140         }
141         this.suffixes = 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         StringBuilder buffer = new StringBuilder();
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 }