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 &lt; files.length; i++) {
036     *     System.out.println(files[i]);
037     * }
038     * </pre>
039     *
040     * @since Commons IO 1.0
041     * @version $Revision: 1005099 $ $Date: 2010-10-06 12:13:01 -0400 (Wed, 06 Oct 2010) $
042     * 
043     * @author Stephen Colebourne
044     * @author Federico Barbieri
045     * @author Serge Knystautas
046     * @author Peter Donald
047     * @see FileFilterUtils#suffixFileFilter(String)
048     * @see FileFilterUtils#suffixFileFilter(String, IOCase)
049     */
050    public class SuffixFileFilter extends AbstractFileFilter implements Serializable {
051        
052        /** The filename suffixes to search for */
053        private final String[] suffixes;
054    
055        /** Whether the comparison is case sensitive. */
056        private final IOCase caseSensitivity;
057    
058        /**
059         * Constructs a new Suffix file filter for a single extension.
060         * 
061         * @param suffix  the suffix to allow, must not be null
062         * @throws IllegalArgumentException if the suffix is null
063         */
064        public SuffixFileFilter(String suffix) {
065            this(suffix, IOCase.SENSITIVE);
066        }
067    
068        /**
069         * Constructs a new Suffix file filter for a single extension
070         * specifying case-sensitivity.
071         *
072         * @param suffix  the suffix to allow, must not be null
073         * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
074         * @throws IllegalArgumentException if the suffix is null
075         * @since Commons IO 1.4
076         */
077        public SuffixFileFilter(String suffix, IOCase caseSensitivity) {
078            if (suffix == null) {
079                throw new IllegalArgumentException("The suffix must not be null");
080            }
081            this.suffixes = new String[] {suffix};
082            this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
083        }
084    
085        /**
086         * Constructs a new Suffix file filter for an array of suffixs.
087         * <p>
088         * The array is not cloned, so could be changed after constructing the
089         * instance. This would be inadvisable however.
090         * 
091         * @param suffixes  the suffixes to allow, must not be null
092         * @throws IllegalArgumentException if the suffix array is null
093         */
094        public SuffixFileFilter(String[] suffixes) {
095            this(suffixes, IOCase.SENSITIVE);
096        }
097    
098        /**
099         * Constructs a new Suffix file filter for an array of suffixs
100         * specifying case-sensitivity.
101         * <p>
102         * The array is not cloned, so could be changed after constructing the
103         * instance. This would be inadvisable however.
104         * 
105         * @param suffixes  the suffixes to allow, must not be null
106         * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
107         * @throws IllegalArgumentException if the suffix array is null
108         * @since Commons IO 1.4
109         */
110        public SuffixFileFilter(String[] suffixes, IOCase caseSensitivity) {
111            if (suffixes == null) {
112                throw new IllegalArgumentException("The array of suffixes must not be null");
113            }
114            this.suffixes = new String[suffixes.length];
115            System.arraycopy(suffixes, 0, this.suffixes, 0, suffixes.length);
116            this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
117        }
118    
119        /**
120         * Constructs a new Suffix file filter for a list of suffixes.
121         * 
122         * @param suffixes  the suffixes to allow, must not be null
123         * @throws IllegalArgumentException if the suffix list is null
124         * @throws ClassCastException if the list does not contain Strings
125         */
126        public SuffixFileFilter(List<String> suffixes) {
127            this(suffixes, IOCase.SENSITIVE);
128        }
129    
130        /**
131         * Constructs a new Suffix file filter for a list of suffixes
132         * specifying case-sensitivity.
133         * 
134         * @param suffixes  the suffixes to allow, must not be null
135         * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
136         * @throws IllegalArgumentException if the suffix list is null
137         * @throws ClassCastException if the list does not contain Strings
138         * @since Commons IO 1.4
139         */
140        public SuffixFileFilter(List<String> suffixes, IOCase caseSensitivity) {
141            if (suffixes == null) {
142                throw new IllegalArgumentException("The list of suffixes must not be null");
143            }
144            this.suffixes = suffixes.toArray(new String[suffixes.size()]);
145            this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
146        }
147    
148        /**
149         * Checks to see if the filename ends with the suffix.
150         * 
151         * @param file  the File to check
152         * @return true if the filename ends with one of our suffixes
153         */
154        @Override
155        public boolean accept(File file) {
156            String name = file.getName();
157            for (String suffix : this.suffixes) {
158                if (caseSensitivity.checkEndsWith(name, suffix)) {
159                    return true;
160                }
161            }
162            return false;
163        }
164        
165        /**
166         * Checks to see if the filename ends with the suffix.
167         * 
168         * @param file  the File directory
169         * @param name  the filename
170         * @return true if the filename ends with one of our suffixes
171         */
172        @Override
173        public boolean accept(File file, String name) {
174            for (String suffix : this.suffixes) {
175                if (caseSensitivity.checkEndsWith(name, suffix)) {
176                    return true;
177                }
178            }
179            return false;
180        }
181    
182        /**
183         * Provide a String representaion of this file filter.
184         *
185         * @return a String representaion
186         */
187        @Override
188        public String toString() {
189            StringBuilder buffer = new StringBuilder();
190            buffer.append(super.toString());
191            buffer.append("(");
192            if (suffixes != null) {
193                for (int i = 0; i < suffixes.length; i++) {
194                    if (i > 0) {
195                        buffer.append(",");
196                    }
197                    buffer.append(suffixes[i]);
198                }
199            }
200            buffer.append(")");
201            return buffer.toString();
202        }
203        
204    }