KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > subscribers > DiffTreeStatistics


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.core.subscribers;
12
13 import java.util.*;
14
15 import org.eclipse.team.core.diff.IDiff;
16 import org.eclipse.team.core.diff.IThreeWayDiff;
17 import org.eclipse.team.core.synchronize.SyncInfo;
18
19 public class DiffTreeStatistics {
20     // {int sync kind -> int number of infos with that sync kind in this sync set}
21
protected Map stats = new HashMap();
22
23     /**
24      * Count this sync state.
25      * @param state the state
26      */

27     public void add(int state) {
28         // update statistics
29
Long JavaDoc count = (Long JavaDoc)stats.get(new Integer JavaDoc(state));
30         if(count == null) {
31             count = new Long JavaDoc(0);
32         }
33         stats.put(new Integer JavaDoc(state), new Long JavaDoc(count.longValue() + 1));
34     }
35     
36     /**
37      * Remove this sync kind.
38      * @param state the info type to remove
39      */

40     public void remove(int state) {
41         // update stats
42
Integer JavaDoc kind = new Integer JavaDoc(state);
43         Long JavaDoc count = (Long JavaDoc)stats.get(kind);
44         if(count == null) {
45             // error condition, shouldn't be removing if we haven't added yet
46
// programmer error calling remove before add.
47
} else {
48             long newCount = count.intValue() - 1;
49             if(newCount > 0) {
50                 stats.put(kind, new Long JavaDoc(newCount));
51             } else {
52                 stats.remove(kind);
53             }
54         }
55     }
56     
57     /**
58      * Return the count of sync infos for the specified sync kind. A mask can be used to acucmulate
59      * counts for specific directions or change types.
60      * To return the number of outgoing changes:
61      * long outgoingChanges = stats.countFor(SyncInfo.OUTGOING, SyncInfo.DIRECTION_MASK);
62      *
63      * @param state the sync kind for which to return the count
64      * @param mask the mask applied to the stored sync kind
65      * @return the number of sync info types added for the specific kind
66      */

67     public long countFor(int state, int mask) {
68         if(mask == 0) {
69             Long JavaDoc count = (Long JavaDoc)stats.get(new Integer JavaDoc(state));
70             return count == null ? 0 : count.longValue();
71         } else {
72             Iterator it = stats.keySet().iterator();
73             long count = 0;
74             while (it.hasNext()) {
75                 Integer JavaDoc key = (Integer JavaDoc) it.next();
76                 if((key.intValue() & mask) == state) {
77                     count += ((Long JavaDoc)stats.get(key)).intValue();
78                 }
79             }
80             return count;
81         }
82     }
83
84     /**
85      * Clear the statistics counts. All calls to countFor() will return 0 until new
86      * sync infos are added.
87      */

88     public void clear() {
89         stats.clear();
90     }
91     
92     /**
93      * For debugging
94      */

95     public String JavaDoc toString() {
96         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
97         Iterator it = stats.keySet().iterator();
98         while (it.hasNext()) {
99             Integer JavaDoc kind = (Integer JavaDoc) it.next();
100             out.append(SyncInfo.kindToString(kind.intValue()) + ": " + stats.get(kind) + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
101
}
102         return out.toString();
103     }
104
105     public void add(IDiff delta) {
106         int state = getState(delta);
107         add(state);
108     }
109
110     public void remove(IDiff delta) {
111         int state = getState(delta);
112         remove(state);
113     }
114
115     private int getState(IDiff delta) {
116         int state = delta.getKind();
117         if (delta instanceof IThreeWayDiff) {
118             IThreeWayDiff twd = (IThreeWayDiff) delta;
119             state |= twd.getDirection();
120         }
121         return state;
122     }
123 }
124
Popular Tags