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.
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
*/
public void addChange(String aPath, ChangeType aChangeType) {
synchronized (changes) {
- changes.add(new Change(aPath, aChangeType));
+ changes.add(new Change(aPath, aChangeType, calendar.getTime()));
}
}
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() {
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);
}
/**
* 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);
}
/**
- * 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;