KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > analyzer > listeners > TCRAnalyzer


1 /*
2  * TCRInspector.java
3  *
4  * Created on April 18, 2005, 12:01 PM
5  */

6
7 package analyzer.listeners;
8
9 import java.util.Hashtable JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.TreeSet JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.lang.StringBuffer JavaDoc;
14
15 /**
16  *
17  * @author loicsegapelli
18  */

19 public class TCRAnalyzer extends TCRParser{
20     
21     boolean editorInUse;
22     long editorTime;
23     int tabSwitch, fields, constructors, methods, beanPatterns,tabCounter, maxEditorTabs;
24     Hashtable JavaDoc extensions;
25     public Hashtable JavaDoc windows,editorWindows;
26     public TreeSet JavaDoc sortedWindows;
27     WindowData previousWindow,lastB4OutWindow;
28     public long trashThis;
29     private final String JavaDoc EDITOR_ID="*EDITOR*",OUT="*OUT OF NB*";
30     WindowData editorSum;
31     
32     /** Creates a new instance of TCRInspector */
33     public TCRAnalyzer(String JavaDoc pathAndTS) {
34         super(pathAndTS);
35     }
36     
37     protected void init(){
38         System.out.println("\nAnalyze of the TopComponent.Registry listener output...");
39         editorInUse = false;
40         windows = new Hashtable JavaDoc();
41         editorWindows = new Hashtable JavaDoc();
42         extensions = new Hashtable JavaDoc();
43         maxEditorTabs=fields=constructors=methods=beanPatterns=tabCounter=tabSwitch=0;
44         
45         editorSum=new WindowData(false,EDITOR_ID);
46         windows.put(EDITOR_ID, editorSum);
47         editorSum.chartColor=java.awt.Color.RED;
48         
49         WindowData outNB=new WindowData(false,OUT);
50         windows.put(OUT, outNB);
51         outNB.chartColor=java.awt.Color.RED;
52     }
53     
54     protected void analyzeNode(String JavaDoc parent, String JavaDoc node){
55         matchFieldName(parent);
56         matchFieldName(node);
57     }
58     
59     protected void analyzeOpen(String JavaDoc windowId, boolean editor){
60         trashThis=startTime;
61         WindowData w;
62         if(windows.containsKey((Object JavaDoc)windowId)){
63             w=(WindowData)windows.get((Object JavaDoc)windowId);
64         } else{
65             w = new WindowData(editor,windowId);
66             windows.put((Object JavaDoc)windowId,w);
67             if(editor){
68                 editorWindows.put((Object JavaDoc)windowId,w);
69                 w.chartColor=java.awt.Color.BLUE;
70                 tabCounter++;
71                 if(tabCounter>maxEditorTabs)maxEditorTabs=tabCounter;
72                 extension(windowId);
73             }
74         }
75         w.open(timestamp);
76     }
77     
78     protected void analyzeGAINED(){
79         if(lastB4OutWindow!=null) analyzeActivated(lastB4OutWindow.id,lastB4OutWindow.editor);
80         else if(previousWindow!=null)analyzeActivated(previousWindow.id,previousWindow.editor);
81     }
82     
83     protected void analyzeLOST(){
84         lastB4OutWindow=previousWindow;
85         analyzeActivated(OUT,false);
86     }
87     
88     protected void analyzeClose(String JavaDoc windowId, boolean editor){
89         WindowData w;
90         if(editor)tabCounter--;
91         if(windows.containsKey((Object JavaDoc)windowId)){
92             w=(WindowData)windows.get((Object JavaDoc)windowId);
93             w.close(timestamp);
94         } else {
95             myError("close without open:"+windowId);
96         }
97     }
98     
99     protected void analyzeActivated(String JavaDoc windowId, boolean editor){
100         WindowData currentWindow;
101         if(previousWindow!=null){
102             previousWindow.deactivated(timestamp);
103             editorSum.deactivated(timestamp);
104         }
105         if(!windows.containsKey((Object JavaDoc)windowId))analyzeOpen(windowId,editor);
106         currentWindow=(WindowData)windows.get((Object JavaDoc)windowId);
107         currentWindow.activated(timestamp);
108         previousWindow=currentWindow;
109         if(editorInUse&&editor)tabSwitch++;
110         if(editor)editorSum.activated(timestamp);
111         editorInUse=editor;
112     }
113     
114     
115     protected void eof(){
116         Enumeration JavaDoc e=windows.elements();
117         WindowData w;
118         while(e.hasMoreElements()){
119             w=(WindowData)e.nextElement();
120             w.end(timestamp);
121             //w.close(timestamp);
122
}
123         print();
124     }
125     
126     private void printWindows(){
127         pln("Windows");
128         sortedWindows=new TreeSet JavaDoc();
129         Enumeration JavaDoc e=windows.elements();
130         while(e.hasMoreElements())sortedWindows.add((WindowData)e.nextElement());
131         Iterator JavaDoc it=sortedWindows.iterator();
132         WindowData w;
133         boolean test=true;
134         while(it.hasNext()){
135             w=(WindowData)it.next();
136             pln(" + "+w.id+"\topen: "+prettyTime(w.open)+"\tactivated: "+prettyTime(w.activated));
137         }
138     }
139     
140     private void printEditor(){
141         WindowData w;
142         long editorTime=0;
143         Enumeration JavaDoc e=editorWindows.elements();
144         while(e.hasMoreElements()){
145             w=(WindowData)e.nextElement();
146             editorTime+=w.activated;
147         }
148         pln("editor time:"+prettyTime(editorTime));
149         pln("max editor tabs simultaneously: "+maxEditorTabs);
150         pln("tab switches: "+tabSwitch);
151         if(tabSwitch>0){
152             double time = new Long JavaDoc(editorTime).doubleValue();
153             pln("tab frequency: "+prettyTime(new Double JavaDoc(time/tabSwitch).longValue()));
154         }
155     }
156     
157     private void printExtension(){
158         Enumeration JavaDoc e=extensions.keys();
159         String JavaDoc ext, count;
160         while(e.hasMoreElements()){
161             ext=(String JavaDoc)e.nextElement();
162             count=((Integer JavaDoc)extensions.get((Object JavaDoc)ext)).toString();
163             pln("extension type: "+ext+"\tcount:"+count);
164         }
165     }
166     
167     
168     private void printGal(){
169         long duration=timestamp-startTime;
170         pln("Duration of session:"+prettyTime(duration));
171     }
172     
173     private void printFields(){
174         pln("fields:"+fields+" constructors:"+constructors+" methods:"+methods+" beans:"+beanPatterns);
175     }
176     
177     private void print(){
178         pln("***TCR");
179         printGal();
180         printWindows();
181         printEditor();
182         printExtension();
183         printFields();
184         pln("***");
185     }
186     
187     private void pln(String JavaDoc s){
188         System.out.println(s);
189     }
190     
191     protected void myError(String JavaDoc msg){
192         error("ANALYZER:"+msg);
193     }
194     
195     private void matchFieldName(String JavaDoc s){
196         if(s==null) return;
197         if(s.equalsIgnoreCase("Fields")) {
198             fields++;
199             return;
200         }
201         if(s.equalsIgnoreCase("Constructors")) {
202             constructors++;
203             return;
204         }
205         if(s.equalsIgnoreCase("Methods")) {
206             methods++;
207             return;
208         }
209         if(s.equalsIgnoreCase("BeanPatterns")) {
210             beanPatterns++;
211             return;
212         }
213     }
214     
215     private void extension(String JavaDoc s){
216         int index=s.lastIndexOf('.');
217         if(index>0){
218             Object JavaDoc ext;
219             ext=(Object JavaDoc)s.substring(index);
220             int i=1;
221             if(extensions.containsKey(ext)){
222                 i=((Integer JavaDoc)extensions.get(ext)).intValue();
223                 i++;
224             }
225             extensions.put(ext,(Object JavaDoc)new Integer JavaDoc(i));
226         }
227     }
228     
229 }
230
Popular Tags