KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > server > uihandler > statistics > ProjectTypes


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
16  */

17
18 package org.netbeans.server.uihandler.statistics;
19
20 import javax.servlet.jsp.PageContext JavaDoc;
21 import org.netbeans.server.uihandler.*;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Map.Entry;
26 import java.util.Set JavaDoc;
27 import java.util.TreeMap JavaDoc;
28 import java.util.logging.LogRecord JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import org.netbeans.lib.uihandler.ProjectOp;
31 import org.netbeans.server.uihandler.statistics.ProjectTypes.Counts;
32
33 /** Counts the number of used projecs and project types.
34  *
35  * @author Jaroslav Tulach
36  */

37 public final class ProjectTypes extends Statistics<ProjectTypes.Counts> {
38     static final Logger JavaDoc LOG = Logger.getLogger(ProjectTypes.class.getName());
39     
40     public ProjectTypes() {
41         super("ProjectTypes");
42     }
43     
44     protected Counts newData() {
45         return Counts.EMPTY;
46     }
47
48     protected Counts process(LogRecord JavaDoc rec) {
49         ProjectOp projectOp = ProjectOp.valueOf(rec);
50         if (projectOp != null) {
51             return new Counts(projectOp);
52         } else {
53             return Counts.EMPTY;
54         }
55     }
56
57     protected Counts finishSessionUpload(String JavaDoc userId, int sessionNumber,
58                                         boolean initialParse, Counts d) {
59         return d;
60     }
61
62     protected Counts join(Counts one, Counts two) {
63         if (one.counts.isEmpty()) {
64             return two;
65         }
66         if (two.counts.isEmpty()) {
67             return one;
68         }
69         Counts join = new Counts();
70         join.add(one);
71         join.add(two);
72         return join;
73     }
74     
75     @Override JavaDoc
76     protected void registerPageContext(PageContext JavaDoc page, String JavaDoc name, Counts data) {
77         int sum = 0;
78         for (int i : data.counts.values()) {
79             sum += i;
80         }
81         
82         int others = 0;
83         TreeMap JavaDoc<String JavaDoc,Object JavaDoc> percentages = new TreeMap JavaDoc<String JavaDoc,Object JavaDoc>();
84         for (Map.Entry JavaDoc<String JavaDoc, Integer JavaDoc> entry : data.getUsages()) {
85             if (entry.getValue() < sum / 20) {
86                 others += entry.getValue();
87             } else {
88                 int percent = entry.getValue() * 100 / sum;
89                 percentages.put(entry.getKey(), percent);
90             }
91         }
92         
93         if (others > 0) {
94             int percent = others * 100 / sum;
95             percentages.put("Others", percent);
96         }
97         
98         if (!percentages.isEmpty()) {
99             page.setAttribute(name, percentages.entrySet());
100         }
101     }
102
103     
104     
105     public static final class Counts {
106         static final Counts EMPTY = new Counts();
107         
108         final Map JavaDoc<String JavaDoc,Integer JavaDoc> counts = new TreeMap JavaDoc<String JavaDoc, Integer JavaDoc>();
109         
110         Counts() {
111         }
112         
113         Counts(ProjectOp projectOp) {
114             String JavaDoc name = projectOp.getProjectDisplayName();
115             if (name.endsWith("Project")) {
116                 name = name.substring(0, name.length() - 7);
117             }
118             counts.put(name, Math.abs(projectOp.getDelta()));
119         }
120
121         public final Set JavaDoc<Map.Entry JavaDoc<String JavaDoc,Integer JavaDoc>> getUsages() {
122             Set JavaDoc<Entry<String JavaDoc, Integer JavaDoc>> t = counts.entrySet();
123             return Collections.unmodifiableSet(t);
124         }
125         
126         final void add(Counts toAdd) {
127             for (Map.Entry JavaDoc<String JavaDoc,Integer JavaDoc> entry : toAdd.counts.entrySet()) {
128                 Integer JavaDoc prev = counts.get(entry.getKey());
129                 if (prev == null) {
130                     counts.put(entry.getKey(), entry.getValue());
131                 } else {
132                     counts.put(entry.getKey(), entry.getValue() + prev);
133                 }
134             }
135         }
136     } // end of Counts
137
}
138
Popular Tags