1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package de.fu_berlin.ties.io;
23
24 import java.io.File;
25 import java.io.FileFilter;
26 import java.util.Collections;
27 import java.util.Set;
28
29 import org.apache.commons.lang.builder.ToStringBuilder;
30
31 /***
32 * A file filter that checks the extension to decide whether to accept a file.
33 * Don't prepend a dot when specifying extensions -- this happens automatically.
34 *
35 * @author Christian Siefkes
36 * @version $Revision: 1.2 $, $Date: 2004/09/06 17:23:31 $, $Author: siefkes $
37 */
38 public class ExtensionFilter implements FileFilter {
39
40 /***
41 * Whether case is ignored when matching the extension.
42 */
43 private final boolean caseIgnored;
44
45 /***
46 * The immutable set of extensions (Strings) to accept.
47 */
48 private final Set extensions;
49
50 /***
51 * Creates a new instance that accepts only a single extension.
52 *
53 * @param extension the extension to accept
54 * @param ignoreCase whether case is ignored when matching the extension;
55 * if <code>true</code>, <code>extension</code> must be lower-case
56 * or matching will fail (file extensions are converted to lower-case
57 * prior to comparing)
58 */
59 public ExtensionFilter(final String extension, final boolean ignoreCase) {
60 this(Collections.singleton(extension), ignoreCase);
61 }
62
63 /***
64 * Creates a new instance.
65 *
66 * @param extensionSet the set of extensions (Strings) to accept
67 * @param ignoreCase whether case is ignored when matching the extension;
68 * if <code>true</code>, <code>extensionSet</code> must contain only
69 * lower-case entries or matching will fail (file extensions are converted
70 * to lower-case, but the contents of the set are used as is)
71 */
72 public ExtensionFilter(final Set<String> extensionSet,
73 final boolean ignoreCase) {
74 super();
75 caseIgnored = ignoreCase;
76
77
78 extensions = Collections.unmodifiableSet(extensionSet);
79 }
80
81 /***
82 * Tests whether or not the specified abstract pathname should be included
83 * in a pathname list.
84 *
85 * @param pathname the abstract pathname to be tested
86 * @return <code>true</code> iff the {@linkplain IOUtils#getExtension(File)
87 * extension} of the <code>pathname</code> is contained in the
88 */
89 public boolean accept(final File pathname) {
90 String extension = IOUtils.getExtension(pathname);
91
92 if (caseIgnored) {
93
94 extension = extension.toLowerCase();
95 }
96
97 return extensions.contains(extension);
98 }
99
100 /***
101 * The immutable set of extensions (Strings) accepted by this filter.
102 * @return the value of the attribute
103 */
104 public Set getExtensions() {
105 return extensions;
106 }
107
108 /***
109 * Whether case is ignored when matching the extension.
110 * If <code>true</code>, file extensions are converted to lower case prior
111 * to comparing them.
112 *
113 * @return the value of the attribute
114 */
115 public boolean isCaseIgnored() {
116 return caseIgnored;
117 }
118
119 /***
120 * Returns a string representation of this object.
121 *
122 * @return a textual representation
123 */
124 public String toString() {
125 return new ToStringBuilder(this)
126 .append("extension set", extensions)
127 .append("ignore case", caseIgnored)
128 .toString();
129 }
130
131 }