Login | Register
My pages Projects Community openCollabNet

Discussions > commits > svn commit: r22 - trunk/src/main/java/com/artilekt/modules/utils/io/FileSystemMonitor.java

jetmailer
Discussion topic

Back to topic list

svn commit: r22 - trunk/src/main/java/com/artilekt/modules/utils/io/FileSystemMonitor.java

Reply

Author volenin
Full name Vladimir Olenin
Date 2006-08-22 14:25:52 PDT
Message Author: volenin
Date: 2006-08-22 14:25:52-0700
New Revision: 22

Added:
   trunk/src/main/java/​com/artilekt/modules​/utils/io/FileSystem​Monitor.java
      - copied, changed from r20, /trunk/src/main/java​/com/artilekt/module​s/utils/io/Recursive​DirectoryLister.java​

Log:


Copied: trunk/src/main/java/​com/artilekt/modules​/utils/io/FileSystem​Monitor.java (from r20, /trunk/src/main/java​/com/artilekt/module​s/utils/io/Recursive​DirectoryLister.java​)
Url: http://jetmailer.tig​ris.org/source/brows​e/jetmailer/trunk/sr​c/main/java/com/arti​lekt/modules/utils/i​o/FileSystemMonitor.​java?view=diff&r​ev=22&p1=/trunk/​src/main/java/com/ar​tilekt/modules/utils​/io/RecursiveDirecto​ryLister.java&p2​=trunk/src/main/java​/com/artilekt/module​s/utils/io/FileSyste​mMonitor.java&r1​=20&r2=22
====================​====================​====================​==================
--- /trunk/src/main/java​/com/artilekt/module​s/utils/io/Recursive​DirectoryLister.java​ (original)
+++ trunk/src/main/java/​com/artilekt/modules​/utils/io/FileSystem​Monitor.java 2006-08-22 14:25:52-0700
@@ -20,10 +20,7 @@
 
 import java.io.File;
 import java.io.FileFilter;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
+import java.util.*;
 
 /**
  * Created by IntelliJ IDEA.
@@ -31,7 +28,7 @@
  * Date: Jun 30, 2006
  * Time: 2:24:50 PM
  */
-public class RecursiveDirectoryLister {
+public class FileSystemMonitor {
   File root;
   public static FileFilter ACCEPT_ALL_FILTER =
     new FileFilter() {
@@ -40,24 +37,26 @@
   private FileFilter filter = ACCEPT_ALL_FILTER;
 
   private long size = 0;
- private Collection files = new ArrayList(0);
- private Collection newFiles = new ArrayList(0);
+ private Collection files = Collections.EMPTY_SET;
+ private Collection newFiles = Collections.EMPTY_SET;
+ private Collection deletedFiles = Collections.EMPTY_SET;
+ private Listener listener = Listener.STUB;
 
 
- public RecursiveDirectoryLister(String root) {
+ public FileSystemMonitor(String root) {
     this(new File(root));
   }
 
- public RecursiveDirectoryLister(File root) {
+ public FileSystemMonitor(File root) {
     this.root = root;
   }
 
- public RecursiveDirectoryLister(File root, FileFilter filter) {
+ public FileSystemMonitor(File root, FileFilter filter) {
     this.root = root;
     this.filter = filter;
   }
 
- public RecursiveDirectoryLister(String root, FileFilter filter) {
+ public FileSystemMonitor(String root, FileFilter filter) {
     this(new File(root), filter);
   }
 
@@ -65,17 +64,39 @@
   public boolean refresh() {
     HashSet fileSet = new HashSet(64);
     size = listFiles(root, filter, fileSet);
- HashSet newFileSet = (HashSet)fileSet.clone();
- newFileSet.removeAll(files);
- files = fileSet;
- newFiles = newFileSet;
- return isNewFilesFound();
+
+ boolean dirUpdated = !fileSet.equals(files);
+
+ if (dirUpdated) {
+ HashSet curFileSet = (HashSet)fileSet.clone();
+ curFileSet.removeAll(files);
+
+ HashSet prevFileSet = (files instanceof HashSet) ? (HashSet)((HashSet)f​iles).clone() : new HashSet(0);
+ prevFileSet.removeAll(fileSet);
+
+ newFiles = curFileSet;
+ deletedFiles = prevFileSet;
+ files = fileSet;
+ }
+ else {
+ newFiles = Collections.EMPTY_SET;
+ deletedFiles = Collections.EMPTY_SET;
+ }
+
+ if (isDeletedFilesFound()) listener.deletedFile​sFound(this);
+ if (isNewFilesFound()) listener.newFilesFound(this);
+
+ return dirUpdated;
   }
 
   public boolean isNewFilesFound() {
     return newFiles.size() > 0;
   }
 
+ public boolean isDeletedFilesFound() {
+ return deletedFiles.size() > 0;
+ }
+
   public Iterator filesIterator() {
     return files.iterator();
   }
@@ -84,7 +105,11 @@
     return newFiles.iterator();
   }
 
- public long size() {
+ public Iterator deletedFilesIterator() {
+ return deletedFiles.iterator();
+ }
+
+ public long getDirectorySize() {
     return size;
   }
 
@@ -96,6 +121,14 @@
     return newFiles.size();
   }
 
+ public int getDeletedFileCount() {
+ return deletedFiles.size();
+ }
+
+ public boolean isDirectoryUpdated() {
+ return isNewFilesFound() || isDeletedFilesFound();
+ }
+
   /**
    * Recursively list files matching a filter
    *
@@ -117,4 +150,18 @@
     return size;
   }
 
+
+ //========= Listener =============//
+ public interface Listener {
+ public void newFilesFound(FileSystemMonitor fsMonitor);
+ public void deletedFilesFound(Fi​leSystemMonitor fsMonitor);
+ public static Listener STUB = new Listener() {
+ public void newFilesFound(FileSystemMonitor fsMonitor) {}
+ public void deletedFilesFound(Fi​leSystemMonitor fsMonitor) {}
+ };
+ }
+
+ public void setListener(Listener listener) {
+ this.listener = (listener == null) ? Listener.STUB : listener;
+ }
 }

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

svn commit: r22 - trunk/src/main/java/com/artilekt/modules/utils/io/FileSystemMonitor.java volenin Vladimir Olenin 2006-08-22 14:25:52 PDT
Messages per page: