1 | |
package org.jtheque.utils.io; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import java.io.File; |
20 | |
import java.io.FileFilter; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.List; |
23 | |
import java.util.Locale; |
24 | |
import java.util.StringTokenizer; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class SimpleFilter implements FileFilter { |
32 | |
private final String description; |
33 | 16 | private final List<String> acceptedExtensions = new ArrayList<String>(5); |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public SimpleFilter(String description, String extensions) { |
42 | 16 | super(); |
43 | |
|
44 | 16 | if (description == null || extensions == null) { |
45 | 6 | throw new IllegalArgumentException("Neither the description or the extension list can be null. "); |
46 | |
} |
47 | |
|
48 | 10 | this.description = description; |
49 | |
|
50 | 10 | StringTokenizer token = new StringTokenizer(extensions, ","); |
51 | |
|
52 | 26 | while (token.hasMoreTokens()) { |
53 | 16 | acceptedExtensions.add(token.nextToken()); |
54 | |
} |
55 | 10 | } |
56 | |
|
57 | |
@Override |
58 | |
public final boolean accept(File file) { |
59 | 28 | String fileName = file.getName().toLowerCase(Locale.getDefault()); |
60 | |
|
61 | 28 | if (file.isDirectory()) { |
62 | 4 | return true; |
63 | |
} |
64 | |
|
65 | 24 | for (String extension : acceptedExtensions) { |
66 | 36 | if (fileName.endsWith(extension)) { |
67 | 10 | return true; |
68 | |
} |
69 | |
} |
70 | |
|
71 | 14 | return fileName.endsWith(acceptedExtensions.get(acceptedExtensions.size() - 1)); |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public final String getDescription() { |
80 | 2 | return description; |
81 | |
} |
82 | |
} |