OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / packages / apps / Email / src / org / apache / commons / io / filefilter / PrefixFileFilter.java
1 /*\r
2  * Licensed to the Apache Software Foundation (ASF) under one or more\r
3  * contributor license agreements.  See the NOTICE file distributed with\r
4  * this work for additional information regarding copyright ownership.\r
5  * The ASF licenses this file to You under the Apache License, Version 2.0\r
6  * (the "License"); you may not use this file except in compliance with\r
7  * the License.  You may obtain a copy of the License at\r
8  * \r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  * \r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 package org.apache.commons.io.filefilter;\r
18 \r
19 import java.io.File;\r
20 import java.io.Serializable;\r
21 import java.util.List;\r
22 \r
23 import org.apache.commons.io.IOCase;\r
24 \r
25 /**\r
26  * Filters filenames for a certain prefix.\r
27  * <p>\r
28  * For example, to print all files and directories in the \r
29  * current directory whose name starts with <code>Test</code>:\r
30  *\r
31  * <pre>\r
32  * File dir = new File(".");\r
33  * String[] files = dir.list( new PrefixFileFilter("Test") );\r
34  * for ( int i = 0; i &lt; files.length; i++ ) {\r
35  *     System.out.println(files[i]);\r
36  * }\r
37  * </pre>\r
38  *\r
39  * @since Commons IO 1.0\r
40  * @version $Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $\r
41  * \r
42  * @author Stephen Colebourne\r
43  * @author Federico Barbieri\r
44  * @author Serge Knystautas\r
45  * @author Peter Donald\r
46  */\r
47 public class PrefixFileFilter extends AbstractFileFilter implements Serializable {\r
48     \r
49     /** The filename prefixes to search for */\r
50     private final String[] prefixes;\r
51 \r
52     /** Whether the comparison is case sensitive. */\r
53     private final IOCase caseSensitivity;\r
54 \r
55     /**\r
56      * Constructs a new Prefix file filter for a single prefix.\r
57      * \r
58      * @param prefix  the prefix to allow, must not be null\r
59      * @throws IllegalArgumentException if the prefix is null\r
60      */\r
61     public PrefixFileFilter(String prefix) {\r
62         this(prefix, IOCase.SENSITIVE);\r
63     }\r
64 \r
65     /**\r
66      * Constructs a new Prefix file filter for a single prefix \r
67      * specifying case-sensitivity.\r
68      * \r
69      * @param prefix  the prefix to allow, must not be null\r
70      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive\r
71      * @throws IllegalArgumentException if the prefix is null\r
72      * @since Commons IO 1.4\r
73      */\r
74     public PrefixFileFilter(String prefix, IOCase caseSensitivity) {\r
75         if (prefix == null) {\r
76             throw new IllegalArgumentException("The prefix must not be null");\r
77         }\r
78         this.prefixes = new String[] {prefix};\r
79         this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);\r
80     }\r
81 \r
82     /**\r
83      * Constructs a new Prefix file filter for any of an array of prefixes.\r
84      * <p>\r
85      * The array is not cloned, so could be changed after constructing the\r
86      * instance. This would be inadvisable however.\r
87      * \r
88      * @param prefixes  the prefixes to allow, must not be null\r
89      * @throws IllegalArgumentException if the prefix array is null\r
90      */\r
91     public PrefixFileFilter(String[] prefixes) {\r
92         this(prefixes, IOCase.SENSITIVE);\r
93     }\r
94 \r
95     /**\r
96      * Constructs a new Prefix file filter for any of an array of prefixes\r
97      * specifying case-sensitivity.\r
98      * <p>\r
99      * The array is not cloned, so could be changed after constructing the\r
100      * instance. This would be inadvisable however.\r
101      * \r
102      * @param prefixes  the prefixes to allow, must not be null\r
103      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive\r
104      * @throws IllegalArgumentException if the prefix is null\r
105      * @since Commons IO 1.4\r
106      */\r
107     public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {\r
108         if (prefixes == null) {\r
109             throw new IllegalArgumentException("The array of prefixes must not be null");\r
110         }\r
111         this.prefixes = prefixes;\r
112         this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);\r
113     }\r
114 \r
115     /**\r
116      * Constructs a new Prefix file filter for a list of prefixes.\r
117      * \r
118      * @param prefixes  the prefixes to allow, must not be null\r
119      * @throws IllegalArgumentException if the prefix list is null\r
120      * @throws ClassCastException if the list does not contain Strings\r
121      */\r
122     public PrefixFileFilter(List prefixes) {\r
123         this(prefixes, IOCase.SENSITIVE);\r
124     }\r
125 \r
126     /**\r
127      * Constructs a new Prefix file filter for a list of prefixes\r
128      * specifying case-sensitivity.\r
129      * \r
130      * @param prefixes  the prefixes to allow, must not be null\r
131      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive\r
132      * @throws IllegalArgumentException if the prefix list is null\r
133      * @throws ClassCastException if the list does not contain Strings\r
134      * @since Commons IO 1.4\r
135      */\r
136     public PrefixFileFilter(List prefixes, IOCase caseSensitivity) {\r
137         if (prefixes == null) {\r
138             throw new IllegalArgumentException("The list of prefixes must not be null");\r
139         }\r
140         this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);\r
141         this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);\r
142     }\r
143 \r
144     /**\r
145      * Checks to see if the filename starts with the prefix.\r
146      * \r
147      * @param file  the File to check\r
148      * @return true if the filename starts with one of our prefixes\r
149      */\r
150     public boolean accept(File file) {\r
151         String name = file.getName();\r
152         for (int i = 0; i < this.prefixes.length; i++) {\r
153             if (caseSensitivity.checkStartsWith(name, prefixes[i])) {\r
154                 return true;\r
155             }\r
156         }\r
157         return false;\r
158     }\r
159     \r
160     /**\r
161      * Checks to see if the filename starts with the prefix.\r
162      * \r
163      * @param file  the File directory\r
164      * @param name  the filename\r
165      * @return true if the filename starts with one of our prefixes\r
166      */\r
167     public boolean accept(File file, String name) {\r
168         for (int i = 0; i < prefixes.length; i++) {\r
169             if (caseSensitivity.checkStartsWith(name, prefixes[i])) {\r
170                 return true;\r
171             }\r
172         }\r
173         return false;\r
174     }\r
175 \r
176     /**\r
177      * Provide a String representaion of this file filter.\r
178      *\r
179      * @return a String representaion\r
180      */\r
181     public String toString() {\r
182         StringBuffer buffer = new StringBuffer();\r
183         buffer.append(super.toString());\r
184         buffer.append("(");\r
185         if (prefixes != null) {\r
186             for (int i = 0; i < prefixes.length; i++) {\r
187                 if (i > 0) {\r
188                     buffer.append(",");\r
189                 }\r
190                 buffer.append(prefixes[i]);\r
191             }\r
192         }\r
193         buffer.append(")");\r
194         return buffer.toString();\r
195     }\r
196     \r
197 }\r