1 4 5 package org.jfox.ioc.util; 6 7 import java.io.File ; 8 import java.io.FileFilter ; 9 10 13 14 public class FileFilters { 15 16 19 private FileFilters() { 20 21 } 22 23 30 public static FileFilter prefixFileFilter(final String prefix) { 31 final String _prefix = (prefix == null ? "" : prefix); 32 return new FileFilter () { 33 public boolean accept(File pathname) { 34 return pathname.getName().startsWith(_prefix); 35 } 36 }; 37 } 38 39 public static FileFilter prefixFileFilter(final String [] prefixes) { 40 if(prefixes == null || prefixes.length == 0) { 41 return prefixFileFilter(""); 42 } 43 FileFilter [] filters = new FileFilter [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 56 public static FileFilter suffixFileFilter(final String suffix) { 57 final String _suffix = (suffix == null ? "" : suffix); 58 return new FileFilter () { 59 public boolean accept(File pathname) { 60 return pathname.getName().endsWith(_suffix); 61 } 62 }; 63 64 } 65 66 public static FileFilter suffixFileFilter(final String [] suffixes) { 67 if(suffixes == null || suffixes.length == 0) { 68 return suffixFileFilter(""); 69 } 70 FileFilter [] filters = new FileFilter [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 83 public static FileFilter nameFileFilter(final String name) { 84 return new FileFilter () { 85 public boolean accept(File pathname) { 86 return pathname.getName().equals(name); 87 } 88 }; 89 } 90 91 96 public static FileFilter directoryFileFilter() { 97 return new FileFilter () { 98 public boolean accept(File pathname) { 99 return pathname.exists() && pathname.isDirectory(); 100 } 101 }; 102 } 103 104 110 public static FileFilter and(final FileFilter [] filters) { 111 return new FileFilter () { 112 public boolean accept(File 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 127 public static FileFilter or(final FileFilter [] filters) { 128 return new FileFilter () { 129 public boolean accept(File 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 145 public static FileFilter not(final FileFilter filter) { 146 return new FileFilter () { 147 public boolean accept(File pathname) { 148 return !filter.accept(pathname); 149 } 150 }; 151 } 152 153 public static void main(String [] args) { 154 155 } 156 } 157 158 | Popular Tags |