KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > util > FileFilters


1 /*
2  * CopyrightPlugin (c) 2004 Your Corporation. All Rights Reserved.
3  */

4
5 package org.jfox.ioc.util;
6
7 import java.io.File JavaDoc;
8 import java.io.FileFilter JavaDoc;
9
10 /**
11  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
12  */

13
14 public class FileFilters {
15
16     /**
17      * FileFilterUtils is not normally instantiated.
18      */

19     private FileFilters() {
20
21     }
22
23     //-----------------------------------------------------------------------
24
/**
25      * Returns a filter that returns true if the filename starts with the specified text.
26      *
27      * @param prefix the filename prefix
28      * @return a prefix checking filter
29      */

30     public static FileFilter JavaDoc prefixFileFilter(final String JavaDoc prefix) {
31         final String JavaDoc _prefix = (prefix == null ? "" : prefix);
32         return new FileFilter JavaDoc() {
33             public boolean accept(File JavaDoc pathname) {
34                 return pathname.getName().startsWith(_prefix);
35             }
36         };
37     }
38
39     public static FileFilter JavaDoc prefixFileFilter(final String JavaDoc[] prefixes) {
40         if(prefixes == null || prefixes.length == 0) {
41             return prefixFileFilter("");
42         }
43         FileFilter JavaDoc[] filters = new FileFilter JavaDoc[prefixes.length];
44         for(int i = 0; i < prefixes.length; i++) {
45             filters[i] = prefixFileFilter(prefixes[i]);
46         }
47         return or(filters);
48     }
49
50     /**
51      * Returns a filter that returns true if the filename ends with the specified text.
52      *
53      * @param suffix the filename suffix
54      * @return a suffix checking filter
55      */

56     public static FileFilter JavaDoc suffixFileFilter(final String JavaDoc suffix) {
57         final String JavaDoc _suffix = (suffix == null ? "" : suffix);
58         return new FileFilter JavaDoc() {
59             public boolean accept(File JavaDoc pathname) {
60                 return pathname.getName().endsWith(_suffix);
61             }
62         };
63
64     }
65
66     public static FileFilter JavaDoc suffixFileFilter(final String JavaDoc[] suffixes) {
67         if(suffixes == null || suffixes.length == 0) {
68             return suffixFileFilter("");
69         }
70         FileFilter JavaDoc[] filters = new FileFilter JavaDoc[suffixes.length];
71         for(int i = 0; i < suffixes.length; i++) {
72             filters[i] = suffixFileFilter(suffixes[i]);
73         }
74         return or(filters);
75     }
76
77     /**
78      * Returns a filter that returns true if the filename matches the specified text.
79      *
80      * @param name the filename
81      * @return a name checking filter
82      */

83     public static FileFilter JavaDoc nameFileFilter(final String JavaDoc name) {
84         return new FileFilter JavaDoc() {
85             public boolean accept(File JavaDoc pathname) {
86                 return pathname.getName().equals(name);
87             }
88         };
89     }
90
91     /**
92      * Returns a filter that checks if the file is a directory.
93      *
94      * @return directory file filter
95      */

96     public static FileFilter JavaDoc directoryFileFilter() {
97         return new FileFilter JavaDoc() {
98             public boolean accept(File JavaDoc pathname) {
99                 return pathname.exists() && pathname.isDirectory();
100             }
101         };
102     }
103
104     //-----------------------------------------------------------------------
105
/**
106      * Returns a filter that ANDs the two specified filters.
107      *
108      * @return a filter that ANDs the two specified filters
109      */

110     public static FileFilter JavaDoc and(final FileFilter JavaDoc[] filters) {
111         return new FileFilter JavaDoc() {
112             public boolean accept(File JavaDoc pathname) {
113                 boolean result = true;
114                 for(int i = 0; i < filters.length; i++) {
115                     result = result && filters[i].accept(pathname);
116                 }
117                 return result;
118             }
119         };
120     }
121
122     /**
123      * Returns a filter that ORs the two specified filters.
124      *
125      * @return a filter that ORs the two specified filters
126      */

127     public static FileFilter JavaDoc or(final FileFilter JavaDoc[] filters) {
128         return new FileFilter JavaDoc() {
129             public boolean accept(File JavaDoc pathname) {
130                 boolean result = false;
131                 for(int i = 0; i < filters.length; i++) {
132                     result = (result || filters[i].accept(pathname));
133                 }
134                 return result;
135             }
136         };
137     }
138
139     /**
140      * Returns a filter that NOTs the specified filter.
141      *
142      * @param filter the filter to invert
143      * @return a filter that NOTs the specified filter
144      */

145     public static FileFilter JavaDoc not(final FileFilter JavaDoc filter) {
146         return new FileFilter JavaDoc() {
147             public boolean accept(File JavaDoc pathname) {
148                 return !filter.accept(pathname);
149             }
150         };
151     }
152
153     public static void main(String JavaDoc[] args) {
154
155     }
156 }
157
158
Popular Tags