Home | History | Annotate | Download | only in filefilter
      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: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $
     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     @Override
    152     public boolean accept(File file) {
    153         String name = file.getName();
    154         for (int i = 0; i < this.suffixes.length; i++) {
    155             if (caseSensitivity.checkEndsWith(name, suffixes[i])) {
    156                 return true;
    157             }
    158         }
    159         return false;
    160     }
    161 
    162     /**
    163      * Checks to see if the filename ends with the suffix.
    164      *
    165      * @param file  the File directory
    166      * @param name  the filename
    167      * @return true if the filename ends with one of our suffixes
    168      */
    169     @Override
    170     public boolean accept(File file, String name) {
    171         for (int i = 0; i < this.suffixes.length; i++) {
    172             if (caseSensitivity.checkEndsWith(name, suffixes[i])) {
    173                 return true;
    174             }
    175         }
    176         return false;
    177     }
    178 
    179     /**
    180      * Provide a String representaion of this file filter.
    181      *
    182      * @return a String representaion
    183      */
    184     @Override
    185     public String toString() {
    186         StringBuffer buffer = new StringBuffer();
    187         buffer.append(super.toString());
    188         buffer.append("(");
    189         if (suffixes != null) {
    190             for (int i = 0; i < suffixes.length; i++) {
    191                 if (i > 0) {
    192                     buffer.append(",");
    193                 }
    194                 buffer.append(suffixes[i]);
    195             }
    196         }
    197         buffer.append(")");
    198         return buffer.toString();
    199     }
    200 
    201 }
    202