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