Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 61194 invoked from network); 1 Mar 2009 21:18:40 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 1 Mar 2009 21:18:40 -0000 Received: (qmail 49179 invoked by uid 500); 1 Mar 2009 21:18:40 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 49113 invoked by uid 500); 1 Mar 2009 21:18:39 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 49104 invoked by uid 99); 1 Mar 2009 21:18:39 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 01 Mar 2009 13:18:39 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 01 Mar 2009 21:18:38 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 66D9C23889D0; Sun, 1 Mar 2009 21:18:18 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r749114 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java Date: Sun, 01 Mar 2009 21:18:18 -0000 To: commits@commons.apache.org From: bayard@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090301211818.66D9C23889D0@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bayard Date: Sun Mar 1 21:18:18 2009 New Revision: 749114 URL: http://svn.apache.org/viewvc?rev=749114&view=rev Log: Applying my patch from LANG-478 - moving StopWatch to using nanoTime under the hood. Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java?rev=749114&r1=749113&r2=749114&view=diff ============================================================================== --- commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java (original) +++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/StopWatch.java Sun Mar 1 21:18:18 2009 @@ -56,6 +56,8 @@ */ public class StopWatch { + private static final long NANO_2_MILLIS = 1000000L; + // running states private static final int STATE_UNSTARTED = 0; @@ -86,6 +88,13 @@ private long startTime; /** + * The start time in Millis - nanoTime is only for elapsed time so we + * need to also store the currentTimeMillis to maintain the old + * getStartTime API. + */ + private long startTimeMillis; + + /** * The stop time. */ private long stopTime; @@ -118,7 +127,8 @@ if (this.runningState != STATE_UNSTARTED) { throw new IllegalStateException("Stopwatch already started. "); } - this.startTime = System.currentTimeMillis(); + this.startTime = System.nanoTime(); + this.startTimeMillis = System.currentTimeMillis(); this.runningState = STATE_RUNNING; } @@ -139,7 +149,7 @@ throw new IllegalStateException("Stopwatch is not running. "); } if (this.runningState == STATE_RUNNING) { - this.stopTime = System.currentTimeMillis(); + this.stopTime = System.nanoTime(); } this.runningState = STATE_STOPPED; } @@ -175,7 +185,7 @@ if (this.runningState != STATE_RUNNING) { throw new IllegalStateException("Stopwatch is not running. "); } - this.stopTime = System.currentTimeMillis(); + this.stopTime = System.nanoTime(); this.splitState = STATE_SPLIT; } @@ -216,7 +226,7 @@ if (this.runningState != STATE_RUNNING) { throw new IllegalStateException("Stopwatch must be running to suspend. "); } - this.stopTime = System.currentTimeMillis(); + this.stopTime = System.nanoTime(); this.runningState = STATE_SUSPENDED; } @@ -237,7 +247,7 @@ if (this.runningState != STATE_SUSPENDED) { throw new IllegalStateException("Stopwatch must be suspended to resume. "); } - this.startTime += (System.currentTimeMillis() - this.stopTime); + this.startTime += (System.nanoTime() - this.stopTime); this.runningState = STATE_RUNNING; } @@ -254,12 +264,28 @@ * @return the time in milliseconds */ public long getTime() { + return getNanoTime() / NANO_2_MILLIS; + } + /** + *

+ * Get the time on the stopwatch in nanoseconds. + *

+ * + *

+ * This is either the time between the start and the moment this method is called, or the amount of time between + * start and stop. + *

+ * + * @return the time in nanoseconds + * @since 3.0 + */ + public long getNanoTime() { if (this.runningState == STATE_STOPPED || this.runningState == STATE_SUSPENDED) { return this.stopTime - this.startTime; } else if (this.runningState == STATE_UNSTARTED) { return 0; } else if (this.runningState == STATE_RUNNING) { - return System.currentTimeMillis() - this.startTime; + return System.nanoTime() - this.startTime; } throw new RuntimeException("Illegal running state has occured. "); } @@ -280,6 +306,24 @@ * @since 2.1 */ public long getSplitTime() { + return getSplitNanoTime() / NANO_2_MILLIS; + } + /** + *

+ * Get the split time on the stopwatch in nanoseconds. + *

+ * + *

+ * This is the time between start and latest split. + *

+ * + * @return the split time in nanoseconds + * + * @throws IllegalStateException + * if the StopWatch has not yet been split. + * @since 3.0 + */ + public long getSplitNanoTime() { if (this.splitState != STATE_SPLIT) { throw new IllegalStateException("Stopwatch must be split to get the split time. "); } @@ -298,7 +342,8 @@ if (this.runningState == STATE_UNSTARTED) { throw new IllegalStateException("Stopwatch has not been started"); } - return this.startTime; + // System.nanoTime is for elapsed time + return this.startTimeMillis; } /**