updates on the change tracker
[mir.git] / source / mir / changetracker / ChangeTracker.java
index 7b5d2c7..41dca6f 100644 (file)
@@ -32,6 +32,7 @@ import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Arrays;
+import java.util.Collection;
 
 /**
  * Change tracker, tracks changes to a path based repository.
@@ -39,79 +40,104 @@ import java.util.Arrays;
  */
 
 public class ChangeTracker {
-  private List changes;
-
-  public ChangeTracker() {
-    changes = new ArrayList();
-  }
+  private final List changes = new ArrayList();
 
   /**
-   * Adds a single change
+   * Add a single change. A change is represented by the full path
+   * of the file involved.
    */
-  public void addChange(String aPath) {
+  public void addChange(String aPath, ChangeType aChangeType) {
     synchronized (changes) {
-      changes.add(aPath);
+      changes.add(new Change(aPath, aChangeType));
     }
   }
 
   /**
-   * Adds an array of changes
+   * Add an array of changes. Each change is represented by the full path
+   * of the file involved.
    */
-  public void addChanges(String[] aPaths) {
-    addChanges(Arrays.asList(aPaths));
+  public void addChanges(String[] aPaths, ChangeType aChangeType) {
+    addChanges(Arrays.asList(aPaths), aChangeType);
   }
 
   /**
-   * Adds a <code>List</code> of changes
+   * Adds a <code>Collection</code> of changes. Each change is represented by the
+   * full path of the file involved.
    */
-  public void addChanges(List aChanges) {
+  public void addChanges(Collection aChanges, ChangeType aChangeType) {
     synchronized (changes) {
-      changes.addAll(aChanges);
+      Iterator i = aChanges.iterator();
+      while (i.hasNext()) {
+        addChange((String) i.next(), aChangeType);
+      }
     }
   }
 
+  public class Change {
+    private ChangeType type;
+    private String path;
+
+    Change(String anAbsolutePath, ChangeType aType) {
+      type = aType;
+      path = anAbsolutePath;
+    }
+
+    public String getPath() {
+      return path;
+    }
+
+    public ChangeType getType() {
+      return type;
+    }
+
+  }
+
   /**
-   * Returns changes within a base path, and removes them from the
-   *     tracker.
+   * Returns a <code>Collection</code> of {@link Change}s within a base
+   *     path, and removes them from the tracker.
    */
-  public List flushChanges(String aBasePath) {
+  List flushChanges(String aBasePath) {
     return flushChanges(aBasePath, new String[0]);
   }
 
   /**
-   * Returns changes within a base path, and removes them from the
-   *     tracker.
+   * Returns a <code>Collection</code> of {@link Change}s within a base
+   *     path, exluding a list of excluded paths, and removes them from
+   *     the tracker.
    */
-  public List flushChanges(String aBasePath, String[] anExcludedPaths) {
+  List flushChanges(String aBasePath, String[] anExcludedPaths) {
     synchronized (changes) {
       List result = getChanges(aBasePath, anExcludedPaths);
 
-      clearChanges(result);
+      removeChanges(result);
 
       return result;
     }
   }
 
   /**
-   * Removes changes from the tracker
+   * Remove specific changes from the change tracker.
+   *
+   * @param someChanges a <code>Collection</code> of changes represented by
+   * their full path in the form of a <code>String</code>
    */
-  public void clearChanges(List aChanges) {
+  void removeChanges(Collection someChanges) {
     synchronized (changes) {
-      changes.removeAll(aChanges);
+      changes.removeAll(someChanges);
     }
   }
 
   /**
    * Returns all changes within a base path
    */
-  public List getChanges(String aBasePath) {
+  List getChanges(String aBasePath) {
     synchronized (changes) {
       List result = new ArrayList();
 
       Iterator i = changes.iterator();
       while (i.hasNext()) {
-        String change = (String) i.next();
-        if (change.startsWith(aBasePath))
+        Change change = (Change) i.next();
+        if (change.getPath().startsWith(aBasePath))
           result.add(change);
       }
 
@@ -122,7 +148,7 @@ public class ChangeTracker {
   /**
    * gets all changes within a base path, but excluding some other paths
    */
-  public List getChanges(String aBasePath, String[] anExcludingPaths) {
+  List getChanges(String aBasePath, String[] anExcludingPaths) {
     synchronized (changes) {
       List result = getChanges(aBasePath);
 
@@ -130,9 +156,9 @@ public class ChangeTracker {
         List remove = new ArrayList();
         Iterator j = result.iterator();
         while (j.hasNext()) {
-          String item = (String) j.next();
-          if (item.startsWith(anExcludingPaths[i])) {
-            remove.add(item);
+          Change change = (Change) j.next();
+          if (change.getPath().startsWith(anExcludingPaths[i])) {
+            remove.add(change);
           }
         }
         result.removeAll(remove);