16ff7b421e73cfe3640de6484e443b73bce5b0eb
[mir.git] / source / mir / changetracker / ChangeTracker.java
1 /*
2  * Copyright (C) 2001-2006 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
27  */
28
29 package mir.changetracker;
30
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;
36
37 /**
38  * Change tracker, tracks changes to a path based repository.
39  * All methods are thread-safe
40  */
41
42 public class ChangeTracker {
43   private final List changes = new ArrayList();
44
45   /**
46    * Add a single change. A change is represented by the full path
47    * of the file involved.
48    */
49   public void addChange(String aPath, ChangeType aChangeType) {
50     synchronized (changes) {
51       changes.add(new Change(aPath, aChangeType));
52     }
53   }
54
55   /**
56    * Add an array of changes. Each change is represented by the full path
57    * of the file involved.
58    */
59   public void addChanges(String[] aPaths, ChangeType aChangeType) {
60     addChanges(Arrays.asList(aPaths), aChangeType);
61   }
62
63   /**
64    * Adds a <code>Collection</code> of changes. Each change is represented by the
65    * full path of the file involved.
66    */
67   public void addChanges(Collection aChanges, ChangeType aChangeType) {
68     synchronized (changes) {
69       Iterator i = aChanges.iterator();
70       while (i.hasNext()) {
71         addChange((String) i.next(), aChangeType);
72       }
73     }
74   }
75
76   public class Change {
77     private ChangeType type;
78     private String path;
79
80     Change(String anAbsolutePath, ChangeType aType) {
81       type = aType;
82       path = anAbsolutePath;
83     }
84
85     public String getPath() {
86       return path;
87     }
88
89     public ChangeType getType() {
90       return type;
91     }
92
93   }
94
95   /**
96    * Returns a <code>Collection</code> of {@link Change}s within a base
97    *     path, and removes them from the tracker.
98    */
99   public List flushChanges(String aBasePath) {
100     return flushChanges(aBasePath, new String[0]);
101   }
102
103   /**
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
106    *     the tracker.
107    */
108   public List flushChanges(String aBasePath, String[] anExcludedPaths) {
109     synchronized (changes) {
110       List result = getChanges(aBasePath, anExcludedPaths);
111
112       removeChanges(result);
113
114       return result;
115     }
116   }
117
118   /**
119    * Remove specific changes from the change tracker.
120    *
121    * @param someChanges a <code>Collection</code> of changes represented by
122    * their full path in the form of a <code>String</code>
123    */
124   void removeChanges(Collection someChanges) {
125     synchronized (changes) {
126       changes.removeAll(someChanges);
127     }
128   }
129
130   /**
131    * Returns all changes within a base path
132    */
133   List getChanges(String aBasePath) {
134     synchronized (changes) {
135       List result = new ArrayList();
136
137       Iterator i = changes.iterator();
138       while (i.hasNext()) {
139         Change change = (Change) i.next();
140         if (change.getPath().startsWith(aBasePath))
141           result.add(change);
142       }
143
144       return result;
145     }
146   }
147
148   /**
149    * gets all changes within a base path, but excluding some other paths
150    */
151   List getChanges(String aBasePath, String[] anExcludingPaths) {
152     synchronized (changes) {
153       List result = getChanges(aBasePath);
154
155       for (int i=0; i<anExcludingPaths.length && result.size()>0; i++) {
156         List remove = new ArrayList();
157         Iterator j = result.iterator();
158         while (j.hasNext()) {
159           Change change = (Change) j.next();
160           if (change.getPath().startsWith(anExcludingPaths[i])) {
161             remove.add(change);
162           }
163         }
164         result.removeAll(remove);
165       }
166
167       return result;
168     }
169   }
170 }