001: /*
002: * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003: * NETSCAPE COMMUNICATIONS CORPORATION
004: *
005: * Copyright (c) 1996 Netscape Communications Corporation.
006: * All Rights Reserved.
007: * Use of this Source Code is subject to the terms of the applicable
008: * license agreement from Netscape Communications Corporation.
009: */
010:
011: package util;
012:
013: import java.util.Date;
014:
015: /**
016: * A stopwatch for doing timing stuff.
017: */
018:
019: public class StopWatch {
020: private long startTime;
021: private long stopTime;
022: private long firstStartTime;
023: private long lapTime;
024: private long elapsedTime;
025: private int state;
026:
027: private final static int INDETERMINATE = -1;
028: private final static int STOPPED = 1;
029: private final static int RUNNING = 2;
030:
031: public StopWatch() {
032: reset();
033: }
034:
035: /**
036: * Reset everything. All times become indeterminate.
037: */
038: public void reset() {
039: state = INDETERMINATE;
040: startTime = INDETERMINATE;
041: firstStartTime = INDETERMINATE;
042: stopTime = INDETERMINATE;
043: elapsedTime = INDETERMINATE;
044: }
045:
046: private long now() {
047: return (new Date()).getTime();
048: }
049:
050: /**
051: * Start the clock ticking.
052: * Ignored if the clock is already running.
053: * Resets interval times.
054: */
055: public void start() {
056: if (state == RUNNING)
057: return;
058: if (state == INDETERMINATE)
059: elapsedTime = 0;
060: state = RUNNING;
061: lapTime = startTime = now();
062: if (firstStartTime == INDETERMINATE)
063: firstStartTime = startTime;
064: stopTime = INDETERMINATE;
065: }
066:
067: /**
068: * Stop (pause) the stopwatch.
069: */
070: public void stop() {
071: if (state != RUNNING)
072: return;
073: state = STOPPED;
074: stopTime = now();
075: elapsedTime += stopTime - startTime;
076: }
077:
078: /**
079: * Return the elapsed (running) time since
080: * first started, not counting pauses.
081: */
082: public long elapsedTime() {
083: if (state == INDETERMINATE)
084: return INDETERMINATE;
085: if (state == RUNNING)
086: return elapsedTime + now() - startTime;
087: else
088: return elapsedTime;
089: }
090:
091: /**
092: * Return the lap time - the time since either
093: * start() or this function was last called.
094: */
095: public long lapTime() {
096: if (state == INDETERMINATE)
097: return INDETERMINATE;
098: long lap;
099: if (state == RUNNING) {
100: long now = now();
101: lap = now - lapTime;
102: lapTime = now;
103: } else {
104: lap = stopTime - lapTime;
105: lapTime = stopTime;
106: }
107: return lap;
108: }
109:
110: /**
111: * Return the total real time since first started.
112: */
113: public long realTime() {
114: if (firstStartTime == INDETERMINATE)
115: return INDETERMINATE;
116: return now() - firstStartTime;
117: }
118:
119: /**
120: * Return the start time.
121: */
122: public long startTime() {
123: return startTime;
124: }
125:
126: /**
127: * Return the stop time.
128: */
129: public long stopTime() {
130: return (state == STOPPED) ? stopTime : INDETERMINATE;
131: }
132:
133: public String toString() {
134: return "StopWatch: (" + Header.VERSION + ")" + " strt: "
135: + startTime() + " stop: " + stopTime() + " elps: "
136: + elapsedTime() + " lap: " + lapTime() + " real: "
137: + realTime() + " state: " + state;
138: }
139: }
|