2 * Copyright (C) 2001-2006 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * and distribute linked combinations including the two. You must obey the
23 * GNU General Public License in all respects for all of the code used other than
24 * the above mentioned libraries. If you modify this file, you may extend this
25 * exception to your version of the file, but you are not obligated to do so.
26 * If you do not wish to do so, delete this exception statement from your version.
29 package mir.changetracker;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Arrays;
35 import java.util.Collection;
38 * Change tracker, tracks changes to a path based repository.
39 * All methods are thread-safe
42 public class ChangeTracker {
43 private final List changes = new ArrayList();
46 * Add a single change. A change is represented by the full path
47 * of the file involved.
49 public void addChange(String aPath, ChangeType aChangeType) {
50 synchronized (changes) {
51 changes.add(new Change(aPath, aChangeType));
56 * Add an array of changes. Each change is represented by the full path
57 * of the file involved.
59 public void addChanges(String[] aPaths, ChangeType aChangeType) {
60 addChanges(Arrays.asList(aPaths), aChangeType);
64 * Adds a <code>Collection</code> of changes. Each change is represented by the
65 * full path of the file involved.
67 public void addChanges(Collection aChanges, ChangeType aChangeType) {
68 synchronized (changes) {
69 Iterator i = aChanges.iterator();
71 addChange((String) i.next(), aChangeType);
77 private ChangeType type;
80 Change(String anAbsolutePath, ChangeType aType) {
82 path = anAbsolutePath;
85 public String getPath() {
89 public ChangeType getType() {
96 * Returns a <code>Collection</code> of {@link Change}s within a base
97 * path, and removes them from the tracker.
99 public List flushChanges(String aBasePath) {
100 return flushChanges(aBasePath, new String[0]);
104 * Returns a <code>Collection</code> of {@link Change}s within a base
105 * path, exluding a list of excluded paths, and removes them from
108 public List flushChanges(String aBasePath, String[] anExcludedPaths) {
109 synchronized (changes) {
110 List result = getChanges(aBasePath, anExcludedPaths);
112 removeChanges(result);
119 * Remove specific changes from the change tracker.
121 * @param someChanges a <code>Collection</code> of changes represented by
122 * their full path in the form of a <code>String</code>
124 void removeChanges(Collection someChanges) {
125 synchronized (changes) {
126 changes.removeAll(someChanges);
131 * Returns all changes within a base path
133 List getChanges(String aBasePath) {
134 synchronized (changes) {
135 List result = new ArrayList();
137 Iterator i = changes.iterator();
138 while (i.hasNext()) {
139 Change change = (Change) i.next();
140 if (change.getPath().startsWith(aBasePath)) {
150 * gets all changes within a base path, but excluding some other paths
152 List getChanges(String aBasePath, String[] anExcludingPaths) {
153 synchronized (changes) {
154 List result = getChanges(aBasePath);
156 for (int i=0; i<anExcludingPaths.length && result.size()>0; i++) {
157 List remove = new ArrayList();
158 Iterator j = result.iterator();
159 while (j.hasNext()) {
160 Change change = (Change) j.next();
161 if (change.getPath().startsWith(anExcludingPaths[i])) {
165 result.removeAll(remove);