You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@slider.apache.org by st...@apache.org on 2014/06/03 17:30:47 UTC

[3/6] git commit: SLIDER-94 (testing related) duration adds more details on its arguments

SLIDER-94 (testing related) duration adds more details on its arguments


Project: http://git-wip-us.apache.org/repos/asf/incubator-slider/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-slider/commit/7e507e44
Tree: http://git-wip-us.apache.org/repos/asf/incubator-slider/tree/7e507e44
Diff: http://git-wip-us.apache.org/repos/asf/incubator-slider/diff/7e507e44

Branch: refs/heads/develop
Commit: 7e507e44508d6dc2047f4215da7072030753da8e
Parents: 5192fc7
Author: Steve Loughran <st...@apache.org>
Authored: Tue Jun 3 16:18:00 2014 +0100
Committer: Steve Loughran <st...@apache.org>
Committed: Tue Jun 3 16:18:00 2014 +0100

----------------------------------------------------------------------
 .../apache/slider/common/tools/Duration.java    | 67 +++++++++++++++++---
 1 file changed, 58 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/7e507e44/slider-core/src/main/java/org/apache/slider/common/tools/Duration.java
----------------------------------------------------------------------
diff --git a/slider-core/src/main/java/org/apache/slider/common/tools/Duration.java b/slider-core/src/main/java/org/apache/slider/common/tools/Duration.java
index 119991f..25b68ae 100644
--- a/slider-core/src/main/java/org/apache/slider/common/tools/Duration.java
+++ b/slider-core/src/main/java/org/apache/slider/common/tools/Duration.java
@@ -18,44 +18,93 @@
 
 package org.apache.slider.common.tools;
 
+import java.io.Closeable;
+
 /**
- * A duration in milliseconds
+ * A duration in milliseconds. This class can be used
+ * to count time, and to be polled to see if a time limit has
+ * passed.
  */
-public class Duration {
+public class Duration implements Closeable {
   public long start, finish;
   public final long limit;
 
+  /**
+   * Create a duration instance with a limit of 0
+   */
   public Duration() {
     this(0);
   }
 
+  /**
+   * Create a duration with a limit specified in millis
+   * @param limit duration in milliseconds
+   */
   public Duration(long limit) {
     this.limit = limit;
   }
 
+  /**
+   * Start
+   * @return self
+   */
   public Duration start() {
-    start = System.currentTimeMillis();
+    start = now();
     return this;
   }
 
+  /**
+   * The close operation relays to {@link #finish()}.
+   * Implementing it allows Duration instances to be automatically
+   * finish()'d in Java7 try blocks for when used in measuring durations.
+   */
+  @Override
+  public final void close() {
+    finish();
+  }
+
   public void finish() {
-    finish = System.currentTimeMillis();
+    finish = now();
+  }
+
+  protected long now() {
+    return System.currentTimeMillis();
   }
 
   public long getInterval() {
     return finish - start;
   }
 
+  /**
+   * return true if the limit has been exceeded
+   * @return true if a limit was set and the current time
+   * exceeds it.
+   */
   public boolean getLimitExceeded() {
-    return limit >= 0 && ((System.currentTimeMillis() - start) > limit);
+    return limit >= 0 && ((now() - start) > limit);
   }
 
   @Override
   public String toString() {
-    return "Duration " +
-           ((finish >= start)
-            ? (" of " + getInterval() + " millis")
-            : "undefined");
+    StringBuilder builder = new StringBuilder();
+    builder.append("Duration");
+     if (finish >= start) {
+       builder.append(" finished at ").append(getInterval()).append(" millis;");
+     } else {
+       if (start > 0) {
+         builder.append(" started but not yet finished;");
+       } else {
+         builder.append(" unstarted;");
+       }
+     }
+    if (limit > 0) {
+      builder.append(" limit: ").append(limit).append(" millis");
+      if (getLimitExceeded()) {
+        builder.append(" -  exceeded");
+      }
+    }
+    
+    return  builder.toString();
   }