KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.team.core.synchronize.SyncInfo;
18
19 /**
20  * Counts SyncInfo states and allows for easy querying for different sync states.
21  */

22 public class SyncInfoStatistics {
23     // {int sync kind -> int number of infos with that sync kind in this sync set}
24
protected Map JavaDoc stats = new HashMap JavaDoc();
25
26     /**
27      * Count this sync kind. Only the type of the sync info is stored.
28      * @param info the new info
29      */

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

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

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

91     public void clear() {
92         stats.clear();
93     }
94     
95     /**
96      * For debugging
97      */

98     public String JavaDoc toString() {
99         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
100         Iterator JavaDoc it = stats.keySet().iterator();
101         while (it.hasNext()) {
102             Integer JavaDoc kind = (Integer JavaDoc) it.next();
103             out.append(SyncInfo.kindToString(kind.intValue()) + ": " + stats.get(kind) + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
104
}
105         return out.toString();
106     }
107 }
108
Popular Tags