001: /*******************************************************************************
002: * Copyright (c) 2002, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.Platform;
016:
017: public class CheatSheetStopWatch {
018: private static CheatSheetStopWatch stopWatch = null;
019:
020: private Map table;
021:
022: private CheatSheetStopWatch() {
023:
024: }
025:
026: public static CheatSheetStopWatch getInstance() {
027: if (stopWatch == null) {
028: stopWatch = new CheatSheetStopWatch();
029: }
030:
031: return stopWatch;
032: }
033:
034: public void start(String key) {
035:
036: Entry entry = getEntry(key);
037: if (entry == null) {
038: entry = new Entry();
039: putEntry(key, entry);
040: } else {
041: resetEntry(entry);
042: }
043:
044: entry.start = System.currentTimeMillis();
045: }
046:
047: public void stop(String key) {
048: Entry entry = getEntry(key);
049: entry.stop = System.currentTimeMillis();
050: }
051:
052: public long totalElapsedTime(String key) {
053: Entry entry = getEntry(key);
054: return entry.stop - entry.start;
055: }
056:
057: public void lapTime(String key) {
058: Entry entry = getEntry(key);
059: if (entry.currentLap == -1) {
060: entry.previousLap = entry.start;
061: } else {
062: entry.previousLap = entry.currentLap;
063: }
064: entry.currentLap = System.currentTimeMillis();
065: }
066:
067: public long elapsedTime(String key) {
068: Entry entry = getEntry(key);
069: return entry.currentLap - entry.previousLap;
070: }
071:
072: /**
073: * Contains the data for an entry in the stopwatch.
074: */
075: private static class Entry {
076: protected long start = -1;
077: protected long stop = -1;
078: protected long currentLap = -1;
079: protected long previousLap = -1;
080: }
081:
082: private Entry getEntry(String key) {
083: return (Entry) getTable().get(key);
084: }
085:
086: private void putEntry(String key, Entry entry) {
087: getTable().put(key, entry);
088: }
089:
090: private void resetEntry(Entry entry) {
091: entry.start = -1;
092: entry.stop = -1;
093: entry.currentLap = -1;
094: entry.previousLap = -1;
095: }
096:
097: private Map getTable() {
098: if (table == null) {
099: table = new HashMap(10);
100: }
101: return table;
102: }
103:
104: public static boolean isTracing() {
105: if (CheatSheetPlugin.getPlugin().isDebugging()) {
106: String traceTimes = Platform
107: .getDebugOption("org.eclipse.ui.cheatsheets/trace/creation/times"); //$NON-NLS-1$
108: if (traceTimes != null
109: && traceTimes.equalsIgnoreCase("true")) { //$NON-NLS-1$
110: return true;
111: }
112: }
113: return false;
114: }
115:
116: public static void startStopWatch(String key) {
117: if (isTracing()) {
118: getInstance().start(key);
119: }
120: }
121:
122: public static void printTotalTime(String key, String 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 key, String message) {
131: if (isTracing()) {
132: getInstance().lapTime(key);
133: System.out.print(message);
134: System.out.println(getInstance().elapsedTime(key));
135: }
136: }
137: }
|