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