You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2010/10/08 03:26:55 UTC

svn commit: r1005693 - in /commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor: FilesystemEntry.java FilesystemObserver.java

Author: niallp
Date: Fri Oct  8 01:26:55 2010
New Revision: 1005693

URL: http://svn.apache.org/viewvc?rev=1005693&view=rev
Log:
IO-132 Detect changes on File's size and type as well as last modified and "refresh" the values that were compared

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java?rev=1005693&r1=1005692&r2=1005693&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java Fri Oct  8 01:26:55 2010
@@ -48,6 +48,7 @@ public class FilesystemEntry implements 
     private boolean exists;
     private boolean directory;
     private long lastModified;
+    private long length;
 
     /**
      * Construct a new monitor for a specified {@link File}.
@@ -74,18 +75,39 @@ public class FilesystemEntry implements 
     }
 
     /**
-     * Refresh the attributes from the underlying {@link File}.
+     * Refresh the attributes from the {@link File}, indicating
+     * whether the file has changed.
      * <p>
-     * This implementation refreshes the <code>name</code>, <code>exists</code>
-     * <code>directory</code> and <code>lastModified</code> properties.
+     * This implementation refreshes the <code>name</code>, <code>exists</code>,
+     * <code>directory</code>, <code>lastModified</code> and <code>length</code>
+     * properties.
+     * <p>
+     * The <code>exists</code>, <code>directory</code>, <code>lastModified</code>
+     * and <code>length</code> properties are compared for changes
+     *
+     * @param file the file instance to compare to
+     * @return <code>true</code> if the file has changed, otherwise <code>false</code>
      */
-    public void refresh(File file) {
-        name = file.getName();
-        exists = file.exists();
-        if (exists) {
-            directory = file.isDirectory();
-            lastModified = file.lastModified();
-        }
+    public boolean refresh(File file) {
+
+        // cache original values
+        boolean origExists       = exists;
+        long    origLastModified = lastModified;
+        boolean origDirectory    = directory;
+        long    origLength       = length;
+
+        // refresh the values
+        name         = file.getName();
+        exists       = file.exists();
+        directory    = (exists ? file.isDirectory() : false);
+        lastModified = (exists ? file.lastModified() : 0);
+        length       = (exists && !directory ? file.length() : 0);
+
+        // Return if there are changes
+        return (exists != origExists ||
+                lastModified != origLastModified ||
+                directory != origDirectory ||
+                length != origLength);
     }
 
     /**
@@ -102,18 +124,6 @@ public class FilesystemEntry implements 
     }
 
     /**
-     * Indicate whether the file has changed or not.
-     * <p>
-     * This implementation compares the <code>lastModified<code>
-     * value of the {@link File} with the stored value.
-     *
-     * @return whether the file has changed or not
-     */
-    public boolean hasChanged(File file) {
-        return (lastModified != file.lastModified());
-    }
-
-    /**
      * Return the parent entry.
      *
      * @return the parent entry
@@ -199,6 +209,24 @@ public class FilesystemEntry implements 
     }
 
     /**
+     * Return the length.
+     *
+     * @return the length
+     */
+    public long getLength() {
+        return length;
+    }
+
+    /**
+     * Set the length.
+     *
+     * @param length the length
+     */
+    public void setLength(long length) {
+        this.length = length;
+    }
+
+    /**
      * Indicate whether the file existed the last time it
      * was checked.
      *

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java?rev=1005693&r1=1005692&r2=1005693&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java Fri Oct  8 01:26:55 2010
@@ -428,7 +428,7 @@ public class FilesystemObserver implemen
      * @param file The current file
      */
     private void doMatch(FilesystemEntry entry, File file) {
-        if (entry.hasChanged(file)) {
+        if (entry.refresh(file)) {
             for (FilesystemListener listener : listeners) {
                 if (entry.isDirectory()) {
                     listener.onDirectoryChange(file);
@@ -436,7 +436,6 @@ public class FilesystemObserver implemen
                     listener.onFileChange(file);
                 }
             }
-            entry.refresh(file);
         }
     }