f486360c11d894eaec966f39d3a119707b0cf2a1
[vamsas.git] / src / org / vamsas / client / simpleclient / FileWatcher.java
1 /**
2  * 
3  */
4 package org.vamsas.client.simpleclient;
5
6 import java.io.File;
7
8 /**
9  * @author jim
10  * Watches a particular file for its creation, deletion, or modification.
11  */
12 public class FileWatcher {
13   private File subject=null;
14   private long lastStat;
15   boolean exists=false;
16   /**
17    * Make a watcher for a particular file.
18    * If the file doesn't exist, the watcher will watch
19    * for its creation (and indicate a change of state)
20    * @param subject
21    */
22   private boolean check() {
23     if (subject!=null) {
24       if (!subject.exists()) {
25         if (exists) {
26         exists=false;
27         return true;
28       } 
29       return false;
30     } else {
31       long newStat=subject.lastModified();
32       if (exists && lastStat==newStat) {
33         return false;
34       }
35       lastStat=newStat;
36       exists=true;
37       return true;
38     }
39   }
40     return false;
41   }
42   
43   
44   public FileWatcher(File subject) {
45     // TODO Auto-generated constructor stub
46     this.subject = subject;
47     check();
48   }
49   public boolean hasChanged() {
50     return check();
51   }
52 }