1 40 41 package com.sun.jmx.examples.scandir.config; 42 43 import java.io.File ; 44 import java.io.FileFilter ; 45 import java.util.Arrays ; 46 import java.util.Date ; 47 import java.util.logging.Logger ; 48 import javax.xml.bind.annotation.XmlElement; 49 import javax.xml.bind.annotation.XmlRootElement; 50 51 70 @XmlRootElement(name="FileFilter", 71 namespace=XmlConfigUtils.NAMESPACE) 72 public class FileMatch implements FileFilter { 73 74 80 83 private String directoryPattern; 84 85 88 private String filePattern; 89 90 93 private long sizeExceedsMaxBytes; 94 95 99 private Date lastModifiedAfter; 100 101 105 private Date lastModifiedBefore; 106 107 110 public FileMatch() { 111 } 112 113 129 @XmlElement(name="DirectoryPattern",namespace=XmlConfigUtils.NAMESPACE) 130 public String getDirectoryPattern() { 131 return this.directoryPattern; 132 } 133 134 143 public void setDirectoryPattern(String directoryPattern) { 144 this.directoryPattern = directoryPattern; 145 } 146 147 160 @XmlElement(name="FilePattern",namespace=XmlConfigUtils.NAMESPACE) 161 public String getFilePattern() { 162 return this.filePattern; 163 } 164 165 173 public void setFilePattern(String filePattern) { 174 this.filePattern = filePattern; 175 } 176 177 184 @XmlElement(name="SizeExceedsMaxBytes",namespace=XmlConfigUtils.NAMESPACE) 185 public long getSizeExceedsMaxBytes() { 186 return this.sizeExceedsMaxBytes; 187 } 188 189 196 public void setSizeExceedsMaxBytes(long sizeLimitInBytes) { 197 this.sizeExceedsMaxBytes = sizeLimitInBytes; 198 } 199 200 208 @XmlElement(name="LastModifiedAfter",namespace=XmlConfigUtils.NAMESPACE) 209 public Date getLastModifiedAfter() { 210 return (lastModifiedAfter==null)?null:(Date )lastModifiedAfter.clone(); 211 } 212 213 220 public void setLastModifiedAfter(Date lastModifiedAfter) { 221 this.lastModifiedAfter = 222 (lastModifiedAfter==null)?null:(Date )lastModifiedAfter.clone(); 223 } 224 225 233 @XmlElement(name="LastModifiedBefore",namespace=XmlConfigUtils.NAMESPACE) 234 public Date getLastModifiedBefore() { 235 return (lastModifiedBefore==null)?null:(Date )lastModifiedBefore.clone(); 236 } 237 238 245 public void setLastModifiedBefore(Date lastModifiedBefore) { 246 this.lastModifiedBefore = 247 (lastModifiedBefore==null)?null:(Date )lastModifiedBefore.clone(); 248 } 249 250 261 public boolean accept(File f) { 262 263 if (f.isDirectory()) { 266 if (directoryPattern != null 267 && !f.getName().matches(directoryPattern)) 268 return false; 269 else return true; 270 } 271 272 276 if (filePattern != null 278 && !f.getName().matches(filePattern)) 279 return false; 280 281 if (sizeExceedsMaxBytes > 0 && f.length() <= sizeExceedsMaxBytes) 283 return false; 284 285 if (lastModifiedAfter != null && 287 lastModifiedAfter.after(new Date (f.lastModified()))) 288 return false; 289 290 if (lastModifiedBefore != null && 292 lastModifiedBefore.before(new Date (f.lastModified()))) 293 return false; 294 295 return true; 297 } 298 299 private Object [] toArray() { 301 final Object [] thisconfig = { 302 directoryPattern, filePattern, lastModifiedAfter, 303 lastModifiedBefore, sizeExceedsMaxBytes 304 }; 305 return thisconfig; 306 } 307 308 @Override 309 public boolean equals(Object o) { 310 if (o == this) return true; 311 if (!(o instanceof FileMatch)) return false; 312 final FileMatch other = (FileMatch)o; 313 final Object [] thisconfig = toArray(); 314 final Object [] otherconfig = other.toArray(); 315 return Arrays.deepEquals(thisconfig,otherconfig); 316 } 317 318 @Override 319 public int hashCode() { 320 return Arrays.deepHashCode(toArray()); 321 } 322 323 } 324 | Popular Tags |