/Users/lyon/j4p/src/futils/WildFilter.java
|
1 package futils;
2
3 import java.io.File;
4 import java.io.FilenameFilter;
5
6 public class WildFilter implements FilenameFilter {
7 private String suffix = null;
8 private String prefix = null;
9 private String midfix = null;
10
11 /**
12 * Accept only those <code>File</code> instances that
13 * are of type file (i.e. not directory) and end with the
14 * suffix
15 *
16 * @param suffix a string that all file names must end with.
17 */
18 public WildFilter(String suffix) {
19 this.suffix = suffix;
20 }
21
22 /**
23 * Accept from the interface <code>FilenameFilter</code> will return
24 * true if the file name begins with the prefix and ends with the
25 * suffix.
26 * <p/>
27 * Like:
28 * <br>
29 * dir f*.java
30 * <br>
31 * This returns only those files that start with "f" and end with ".java"
32 *
33 * @param prefix string passed in to describe the file names beginning
34 * @param suffix ending part of the file name.
35 */
36 public WildFilter(String prefix, String suffix) {
37 this.suffix = suffix;
38 this.prefix = prefix;
39 }
40
41 /**
42 * Accept from the interface <code>FilenameFilter</code> will return
43 * true if the file name begins with the prefix and ends with the
44 * suffix.
45 * <p/>
46 * Like:
47 * <br>
48 * dir f*.java
49 * <br>
50 * This returns only those files that start with "f" and end with ".java"
51 * The midfix has to be contained in the filename in order for this to return true;
52 *
53 * @param prefix string passed in to describe the file names beginning
54 * @param suffix ending part of the file name.
55 * @param midfix contained in the file name.
56 */
57 public WildFilter(String prefix, String midfix, String suffix) {
58 this.suffix = suffix;
59 this.prefix = prefix;
60 this.midfix = midfix;
61 }
62
63 public boolean accept(File dir, String name) {
64 if (prefix == null) return name.endsWith(suffix);
65 if (suffix == null) return name.startsWith(prefix);
66 if (midfix == null) return name.endsWith(suffix) && name.startsWith(prefix);
67 return name.endsWith(suffix) && name.startsWith(prefix) &&
68 (name.indexOf(midfix) != -1);
69 }
70 }
71
72