001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.util;
008:
009: import java.io.File;
010: import java.io.FileFilter;
011: import java.util.regex.Pattern;
012:
013: /**
014: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
015: */
016:
017: public class FileFilterUtils {
018:
019: /**
020: * FileFilterUtils is not normally instantiated.
021: */
022: private FileFilterUtils() {
023:
024: }
025:
026: public static FileFilter prefixFileFilter(final String... prefixes) {
027: return new FileFilter() {
028: public boolean accept(File pathname) {
029: for (String prefix : prefixes) {
030: if (pathname.getName().startsWith(prefix)) {
031: return true;
032: }
033: }
034: return false;
035: }
036: };
037:
038: }
039:
040: public static FileFilter suffixFileFilter(final String... suffixes) {
041: return new FileFilter() {
042: public boolean accept(File pathname) {
043: for (String suffix : suffixes) {
044: if (pathname.getName().endsWith(suffix)) {
045: return true;
046: }
047: }
048: return false;
049: }
050: };
051: }
052:
053: /**
054: * Returns a filter that returns true if the filename matches the specified
055: * text.
056: *
057: * @param name the filename
058: * @return a name checking filter
059: */
060: public static FileFilter nameFileFilter(final String name) {
061: return new FileFilter() {
062: public boolean accept(File pathname) {
063: return pathname.getName().equals(name);
064: }
065: };
066: }
067:
068: public static FileFilter regexFileFile(final String regex) {
069: return new FileFilter() {
070: public boolean accept(File pathname) {
071: return Pattern.matches(regex, pathname.getName());
072: }
073: };
074: }
075:
076: /**
077: * Returns a filter that checks if the file is a directory.
078: *
079: * @return directory file filter
080: */
081: public static FileFilter directoryFileFilter() {
082: return new FileFilter() {
083: public boolean accept(File pathname) {
084: return pathname.isDirectory();
085: }
086: };
087: }
088:
089: public static FileFilter sizeLtFileFilter(final long size) {
090: return new FileFilter() {
091: public boolean accept(File pathname) {
092: return pathname.length() < size;
093: }
094: };
095: }
096:
097: public static FileFilter sizeGtFileFilter(final long size) {
098: return new FileFilter() {
099: public boolean accept(File pathname) {
100: return pathname.length() > size;
101: }
102: };
103: }
104:
105: public static FileFilter sizeEqualFileFilter(final long size) {
106: return new FileFilter() {
107: public boolean accept(File pathname) {
108: return pathname.length() == size;
109: }
110: };
111: }
112:
113: // -----------------------------------------------------------------------
114:
115: /**
116: * Returns a filter that ANDs the two specified filters.
117: *
118: * @param filters and filters
119: * @return a filter that ANDs the two specified filters
120: */
121: public static FileFilter and(final FileFilter... filters) {
122: return new FileFilter() {
123: public boolean accept(File pathname) {
124: boolean result = true;
125: for (FileFilter filter : filters) {
126: result = result && filter.accept(pathname);
127: }
128: return result;
129: }
130: };
131: }
132:
133: /**
134: * Returns a filter that ORs the two specified filters.
135: *
136: * @param filters or filters
137: * @return a filter that ORs the two specified filters
138: */
139: public static FileFilter or(final FileFilter... filters) {
140: return new FileFilter() {
141: public boolean accept(File pathname) {
142: boolean result = false;
143: for (FileFilter filter : filters) {
144: result = (result || filter.accept(pathname));
145: }
146: return result;
147: }
148: };
149: }
150:
151: /**
152: * Returns a filter that NOTs the specified filter.
153: *
154: * @param filter the filter to invert
155: * @return a filter that NOTs the specified filter
156: */
157: public static FileFilter not(final FileFilter filter) {
158: return new FileFilter() {
159: public boolean accept(File pathname) {
160: return !filter.accept(pathname);
161: }
162: };
163: }
164:
165: public static void main(String[] args) {
166:
167: }
168: }
|