KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > domain > Counts


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.domain;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.EnumMap JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * Object that holds statistics for items within a single space
26  * a map of these would serve as the model for the dashboard view
27  * contains logic for totalling etc.
28  */

29 public class Counts implements Serializable JavaDoc {
30
31     public static final int ASSIGNED_TO_ME = 1;
32     public static final int LOGGED_BY_ME = 2;
33     public static final int TOTAL = 3;
34     
35     private Map JavaDoc<Integer JavaDoc, Map JavaDoc<Integer JavaDoc, Integer JavaDoc>> typeCounts = new HashMap JavaDoc<Integer JavaDoc, Map JavaDoc<Integer JavaDoc, Integer JavaDoc>>();
36     
37     public void add(int type, int state, int count) {
38         Map JavaDoc<Integer JavaDoc, Integer JavaDoc> stateCounts = typeCounts.get(type);
39         if (stateCounts == null) {
40             stateCounts = new HashMap JavaDoc<Integer JavaDoc, Integer JavaDoc>();
41             typeCounts.put(type, stateCounts);
42         }
43         Integer JavaDoc i = stateCounts.get(state);
44         if (i == null) {
45             stateCounts.put(state, count);
46         } else {
47             stateCounts.put(state, i + count);
48         }
49     }
50     
51     protected int getTotalForType(int type) {
52         Map JavaDoc<Integer JavaDoc, Integer JavaDoc> stateCounts = typeCounts.get(type);
53         if (stateCounts == null) {
54             return 0;
55         }
56         int total = 0;
57         for(Map.Entry JavaDoc<Integer JavaDoc, Integer JavaDoc> entry : stateCounts.entrySet()) {
58             total += entry.getValue();
59         }
60         return total;
61     }
62     
63     public int getLoggedByMe() {
64         return getTotalForType(LOGGED_BY_ME);
65     }
66     
67     public int getAssignedToMe() {
68         return getTotalForType(ASSIGNED_TO_ME);
69     }
70     
71     public int getTotal() {
72         return getTotalForType(TOTAL);
73     }
74  
75     public Map JavaDoc<Integer JavaDoc, Integer JavaDoc> getLoggedByMeMap() {
76         return typeCounts.get(LOGGED_BY_ME);
77     }
78     
79     public Map JavaDoc<Integer JavaDoc, Integer JavaDoc> getAssignedToMeMap() {
80         return typeCounts.get(ASSIGNED_TO_ME);
81     }
82     
83     public Map JavaDoc<Integer JavaDoc, Integer JavaDoc> getTotalMap() {
84         return typeCounts.get(TOTAL);
85     }
86     
87 }
88
Popular Tags