001: /*
002: * ElapsedTimer.java
003: *
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
005: *
006: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common
010: * Development and Distribution License("CDDL") (collectively, the
011: * "License"). You may not use this file except in compliance with the
012: * License. You can obtain a copy of the License at
013: * http://www.netbeans.org/cddl-gplv2.html
014: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
015: * specific language governing permissions and limitations under the
016: * License. When distributing the software, include this License Header
017: * Notice in each file and include the License file at
018: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
019: * particular file as subject to the "Classpath" exception as provided
020: * by Sun in the GPL Version 2 section of the License file that
021: * accompanied this code. If applicable, add the following below the
022: * License Header, with the fields enclosed by brackets [] replaced by
023: * your own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Contributor(s):
027: *
028: * The Original Software is NetBeans. The Initial Developer of the Original
029: * Software is Sun Microsystems, Inc. Portions Copyright 2000-2001 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: *
032: * If you wish your version of this file to be governed by only the CDDL
033: * or only the GPL Version 2, indicate your decision by adding
034: * "[Contributor] elects to include this software in this distribution
035: * under the [CDDL or GPL Version 2] license." If you do not indicate a
036: * single choice of license, a recipient has the option to distribute
037: * your version of this file under either the CDDL, the GPL Version 2 or
038: * to extend the choice of license to its licensees as provided above.
039: * However, if you add GPL Version 2 code and therefore, elected the GPL
040: * Version 2 license, then the option applies only if the new code is
041: * made subject to such option by the copyright holder.
042: *
043: * Contributor(s): Thomas Ball
044: *
045: * Version: $Revision$
046: */
047:
048: /**
049: * A simple class for calculating and reporting elapsed system time.
050: * Each timer has a start time and may have an end time. If an
051: * elapsed time value is requested of a timer which doesn't have its
052: * stop time set, the current system time is used.
053: *
054: * @author Tom Ball
055: */
056: public class ElapsedTimer {
057: // System times in milliseconds; see System.currentTimeMillis();
058: private long startTime;
059: private long stopTime;
060:
061: /**
062: * Create a new timer.
063: */
064: public ElapsedTimer() {
065: reset();
066: }
067:
068: /**
069: * Stop the current timer; that is, set its stopTime.
070: */
071: public void stop() {
072: stopTime = System.currentTimeMillis();
073: }
074:
075: /**
076: * Reset the starting time to the current system time.
077: */
078: public final void reset() {
079: startTime = System.currentTimeMillis();
080: }
081:
082: public long getElapsedMilliseconds() {
083: long st = (stopTime == 0) ? System.currentTimeMillis()
084: : stopTime;
085: return st - startTime;
086: }
087:
088: public int getElapsedSeconds() {
089: return (int) (getElapsedMilliseconds() / 1000L);
090: }
091:
092: public String toString() {
093: long ms = getElapsedMilliseconds();
094: int sec = (int) (ms / 1000L);
095: int frac = (int) (ms % 1000L);
096: StringBuffer sb = new StringBuffer();
097: sb.append(ms / 1000L);
098: sb.append('.');
099: sb.append(ms % 1000L);
100: sb.append(" seconds");
101: return sb.toString();
102: }
103: }
|