KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > preferences > Statistics


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  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup.preferences;
21
22 /**
23  *
24  * @author Radek Matous
25  */

26 public final class Statistics {
27     /** Creates a new instance of Statistics */
28     
29     public static final Statistics.TimeConsumer FLUSH =
30             new Statistics.TimeConsumer("Flush");//NOI18N
31
public static final Statistics.TimeConsumer LOAD =
32             new Statistics.TimeConsumer("Load");//NOI18N
33
public static final Statistics.TimeConsumer REMOVE_NODE =
34             new Statistics.TimeConsumer("Remove node");//NOI18N
35
public static final Statistics.TimeConsumer CHILDREN_NAMES =
36             new Statistics.TimeConsumer("Children names");//NOI18N
37

38     
39     private Statistics() {}
40     
41     
42     public static Statistics.StopWatch getStopWatch(Statistics.TimeConsumer consumer) {
43         return new Statistics.StopWatch(consumer);
44     }
45
46     public static Statistics.StopWatch getStopWatch(Statistics.TimeConsumer consumer, boolean start) {
47         Statistics.StopWatch retval = new Statistics.StopWatch(consumer);
48         if (start) {
49             retval.start();
50         }
51         return retval;
52     }
53     
54     public static final class TimeConsumer {
55         private int elapsedTime;
56         private int numberOfCalls;
57         private final String JavaDoc description;
58         
59         private TimeConsumer(final String JavaDoc description) {
60             this.description = description;
61         }
62         
63         public int getConsumedTime() {
64             return elapsedTime;
65         }
66         
67         public int getNumberOfCalls() {
68             return numberOfCalls;
69         }
70         
71         public void reset() {
72             elapsedTime = 0;
73             numberOfCalls = 0;
74         }
75
76         public String JavaDoc toString() {
77             return description + ": " + numberOfCalls + " calls in " + elapsedTime + "ms";//NOI18N
78
}
79         
80         private void incrementNumerOfCalls() {
81             numberOfCalls++;
82             
83         }
84     }
85     
86     public static final class StopWatch {
87         private long startTime = 0;
88         private final Statistics.TimeConsumer activity;
89         
90         
91         /** Creates a new instance of ElapsedTime */
92         private StopWatch(Statistics.TimeConsumer activity) {
93             this.activity = activity;
94         }
95         
96         
97         public void start() {
98             startTime = System.currentTimeMillis();
99         }
100         
101         public void stop() {
102             assert startTime != 0;
103             activity.elapsedTime += (System.currentTimeMillis() - startTime);
104             activity.incrementNumerOfCalls();
105             startTime = 0;
106         }
107     }
108 }
109
110
Popular Tags