introduction of the change engine
[mir.git] / source / mir / changetracker / ChangeTracker.java
1 /*
2  * Copyright (C) 2001, 2002 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  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package mir.changetracker;
32
33 import java.util.ArrayList;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Arrays;
37
38 /**
39  * Change tracker, tracks changes to a path based repository.
40  * All methods are thread-safe
41  */
42
43 public class ChangeTracker {
44   private List changes;
45
46   public ChangeTracker() {
47     changes = new ArrayList();
48   }
49
50   /**
51    * Adds a single change
52    */
53   public void addChange(String aPath) {
54     synchronized (changes) {
55       changes.add(aPath);
56     }
57   }
58
59   /**
60    * Adds an array of changes
61    */
62   public void addChanges(String[] aPaths) {
63     addChanges(Arrays.asList(aPaths));
64   }
65
66   /**
67    * Adds a <code>List</code> of changes
68    */
69   public void addChanges(List aChanges) {
70     synchronized (changes) {
71       changes.addAll(aChanges);
72     }
73   }
74
75   /**
76    * Returns changes within a base path, and removes them from the
77    *     tracker.
78    */
79   public List flushChanges(String aBasePath) {
80     return flushChanges(aBasePath, new String[0]);
81   }
82
83   /**
84    * Returns changes within a base path, and removes them from the
85    *     tracker.
86    */
87   public List flushChanges(String aBasePath, String[] anExcludedPaths) {
88     synchronized (changes) {
89       List result = getChanges(aBasePath, anExcludedPaths);
90
91       clearChanges(result);
92
93       return result;
94     }
95   }
96
97   /**
98    * Removes changes from the tracker
99    */
100   public void clearChanges(List aChanges) {
101     synchronized (changes) {
102       changes.removeAll(aChanges);
103     }
104   }
105
106   /**
107    * Returns all changes within a base path
108    */
109   public List getChanges(String aBasePath) {
110     synchronized (changes) {
111       List result = new ArrayList();
112
113       Iterator i = changes.iterator();
114       while (i.hasNext()) {
115         String change = (String) i.next();
116         if (change.startsWith(aBasePath))
117           result.add(change);
118       }
119
120       return result;
121     }
122   }
123
124   /**
125    * gets all changes within a base path, but excluding some other paths
126    */
127   public List getChanges(String aBasePath, String[] anExcludingPaths) {
128     synchronized (changes) {
129       List result = getChanges(aBasePath);
130
131       for (int i=0; i<anExcludingPaths.length && result.size()>0; i++) {
132         List remove = new ArrayList();
133         Iterator j = result.iterator();
134         while (j.hasNext()) {
135           String item = (String) j.next();
136           if (item.startsWith(anExcludingPaths[i])) {
137             remove.add(item);
138           }
139         }
140         result.removeAll(remove);
141       }
142
143       return result;
144     }
145   }
146 }