KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > CheatSheetStopWatch


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.ui.internal.cheatsheets;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.Platform;
17
18 public class CheatSheetStopWatch {
19     private static CheatSheetStopWatch stopWatch = null;
20
21     private Map JavaDoc table;
22
23     private CheatSheetStopWatch() {
24         
25     }
26     
27     public static CheatSheetStopWatch getInstance() {
28         if(stopWatch == null) {
29             stopWatch = new CheatSheetStopWatch();
30         }
31         
32         return stopWatch;
33     }
34     
35     public void start(String JavaDoc key) {
36
37         Entry entry = getEntry(key);
38         if (entry == null) {
39             entry = new Entry();
40             putEntry(key, entry);
41         } else {
42             resetEntry(entry);
43         }
44         
45         entry.start = System.currentTimeMillis();
46     }
47     
48     public void stop(String JavaDoc key) {
49         Entry entry = getEntry(key);
50         entry.stop = System.currentTimeMillis();
51     }
52     
53     public long totalElapsedTime(String JavaDoc key) {
54         Entry entry = getEntry(key);
55         return entry.stop - entry.start;
56     }
57     
58     public void lapTime(String JavaDoc key) {
59         Entry entry = getEntry(key);
60         if(entry.currentLap == -1) {
61             entry.previousLap = entry.start;
62         } else {
63             entry.previousLap = entry.currentLap;
64         }
65         entry.currentLap = System.currentTimeMillis();
66     }
67
68     public long elapsedTime(String JavaDoc key) {
69         Entry entry = getEntry(key);
70         return entry.currentLap - entry.previousLap;
71     }
72
73     /**
74      * Contains the data for an entry in the stopwatch.
75      */

76     private static class Entry {
77         protected long start = -1;
78         protected long stop = -1;
79         protected long currentLap = -1;
80         protected long previousLap = -1;
81     }
82     
83     private Entry getEntry(String JavaDoc key) {
84         return (Entry) getTable().get(key);
85     }
86
87     private void putEntry(String JavaDoc key, Entry entry) {
88         getTable().put(key, entry);
89     }
90
91     private void resetEntry(Entry entry) {
92         entry.start = -1;
93         entry.stop = -1;
94         entry.currentLap = -1;
95         entry.previousLap = -1;
96     }
97
98     private Map JavaDoc getTable() {
99         if (table == null) {
100             table = new HashMap JavaDoc(10);
101         }
102         return table;
103     }
104
105
106     public static boolean isTracing() {
107         if (CheatSheetPlugin.getPlugin().isDebugging()) {
108             String JavaDoc traceTimes = Platform.getDebugOption("org.eclipse.ui.cheatsheets/trace/creation/times"); //$NON-NLS-1$
109
if (traceTimes != null && traceTimes.equalsIgnoreCase("true")) { //$NON-NLS-1$
110
return true;
111             }
112         }
113         return false;
114     }
115     
116     public static void startStopWatch(String JavaDoc key) {
117         if(isTracing()) {
118             getInstance().start(key);
119         }
120     }
121
122     public static void printTotalTime(String JavaDoc key, String JavaDoc message) {
123         if(isTracing()) {
124             getInstance().stop(key);
125             System.out.print(message);
126             System.out.println(getInstance().totalElapsedTime(key));
127         }
128     }
129
130     public static void printLapTime(String JavaDoc key, String JavaDoc message) {
131         if(isTracing()) {
132             getInstance().lapTime(key);
133             System.out.print(message);
134             System.out.println(getInstance().elapsedTime(key));
135         }
136     }
137 }
138
Popular Tags