further fixes to the change reporting framework
[mir.git] / source / mir / changetracker / ChangeTracker.java
index 3474b26..3df943e 100644 (file)
@@ -33,6 +33,9 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Calendar;
 
 /**
  * Change tracker, tracks changes to a path based repository.
@@ -41,6 +44,11 @@ import java.util.Collection;
 
 public class ChangeTracker {
   private final List changes = new ArrayList();
+  private Calendar calendar = Calendar.getInstance();
+
+  // to prevent memory issues with the number of recorded changes,
+  //   we'll adhere to this maximum
+  private static final int MAX_CHANGES = 10000;
 
   /**
    * Add a single change. A change is represented by the full path
@@ -48,7 +56,7 @@ public class ChangeTracker {
    */
   public void addChange(String aPath, ChangeType aChangeType) {
     synchronized (changes) {
-      changes.add(new Change(aPath, aChangeType));
+      changes.add(new Change(aPath, aChangeType, calendar.getTime()));
     }
   }
 
@@ -76,10 +84,12 @@ public class ChangeTracker {
   public class Change {
     private ChangeType type;
     private String path;
+    private Date date;
 
-    Change(String anAbsolutePath, ChangeType aType) {
+    Change(String anAbsolutePath, ChangeType aType, Date aDate) {
       type = aType;
       path = anAbsolutePath;
+      date = aDate;
     }
 
     public String getPath() {
@@ -90,14 +100,18 @@ public class ChangeTracker {
       return type;
     }
 
+    public Date getDate() {
+      return date;
+    }
+
   }
 
   /**
    * Returns a <code>Collection</code> of {@link Change}s within a base
    *     path, and removes them from the tracker.
    */
-  public List flushChanges(String aBasePath) {
-    return flushChanges(aBasePath, new String[0]);
+  public Collection flushChanges(String aBasePath) {
+    return flushChanges(aBasePath, Collections.EMPTY_LIST);
   }
 
   /**
@@ -105,9 +119,9 @@ public class ChangeTracker {
    *     path, exluding a list of excluded paths, and removes them from
    *     the tracker.
    */
-  public List flushChanges(String aBasePath, String[] anExcludedPaths) {
+  public Collection flushChanges(String aBasePath, Collection anExcludedPaths) {
     synchronized (changes) {
-      List result = getChanges(aBasePath, anExcludedPaths);
+      Collection result = getChanges(aBasePath, anExcludedPaths);
 
       removeChanges(result);
 
@@ -147,22 +161,34 @@ public class ChangeTracker {
   }
 
   /**
-   * gets all changes within a base path, but excluding some other paths
+   * Gets all changes within a base path, but excluding some other paths
+   *
+   * @param aBasePath
+   * @param anExcludedPaths a collection of paths to exclude. may be <code>null</code>
+   *       to not exclude anything
    */
-  List getChanges(String aBasePath, String[] anExcludingPaths) {
+  Collection getChanges(String aBasePath, Collection anExcludedPaths) {
     synchronized (changes) {
       List result = getChanges(aBasePath);
 
-      for (int i=0; i<anExcludingPaths.length && result.size()>0; i++) {
-        List remove = new ArrayList();
-        Iterator j = result.iterator();
-        while (j.hasNext()) {
-          Change change = (Change) j.next();
-          if (change.getPath().startsWith(anExcludingPaths[i])) {
-            remove.add(change);
+      if (anExcludedPaths != null) {
+        Iterator i = anExcludedPaths.iterator();
+        while (i.hasNext()) {
+          String excludedPath = (String) i.next();
+          List remove = new ArrayList();
+          Iterator j = result.iterator();
+          while (j.hasNext()) {
+            Change change = (Change) j.next();
+            if (change.getPath().startsWith(excludedPath)) {
+              remove.add(change);
+            }
           }
+          result.removeAll(remove);
         }
-        result.removeAll(remove);
+      }
+
+      if (changes.size()>MAX_CHANGES) {
+        changes.clear();
       }
 
       return result;