This is an automated email from the ASF dual-hosted git repository.

domgarguilo pushed a commit to branch 2.1
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/2.1 by this push:
     new b09e595ca9 Add static creation method that applies offset to Timer 
object (#4789)
b09e595ca9 is described below

commit b09e595ca996fdc78a8fe799df389702c029d062
Author: Dom G. <domgargu...@apache.org>
AuthorDate: Wed Aug 7 17:52:50 2024 -0400

    Add static creation method that applies offset to Timer object (#4789)
---
 .../java/org/apache/accumulo/core/util/Timer.java  | 25 ++++++++++++++++
 .../org/apache/accumulo/core/util/TimerTest.java   | 34 ++++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/core/src/main/java/org/apache/accumulo/core/util/Timer.java 
b/core/src/main/java/org/apache/accumulo/core/util/Timer.java
index cf06789993..b7fa4567cf 100644
--- a/core/src/main/java/org/apache/accumulo/core/util/Timer.java
+++ b/core/src/main/java/org/apache/accumulo/core/util/Timer.java
@@ -32,6 +32,10 @@ public final class Timer {
     this.startNanos = System.nanoTime();
   }
 
+  private Timer(long offsetNanos) {
+    this.startNanos = System.nanoTime() + offsetNanos;
+  }
+
   /**
    * Creates and starts a new Timer instance.
    *
@@ -41,6 +45,27 @@ public final class Timer {
     return new Timer();
   }
 
+  /**
+   * Creates a new Timer with an offset applied.
+   *
+   * @param offset the duration of the offset to apply.
+   * @return a new Timer instance with the specified offset.
+   */
+  public static Timer startNewWithOffset(Duration offset) {
+    return new Timer(offset.toNanos());
+  }
+
+  /**
+   * Creates a new Timer with an offset applied.
+   *
+   * @param offset the duration of the offset to apply.
+   * @param unit the TimeUnit of the offset.
+   * @return a new Timer instance with the specified offset.
+   */
+  public static Timer startNewWithOffset(long offset, TimeUnit unit) {
+    return new Timer(unit.toNanos(offset));
+  }
+
   /**
    * Resets the start point for this timer.
    */
diff --git a/core/src/test/java/org/apache/accumulo/core/util/TimerTest.java 
b/core/src/test/java/org/apache/accumulo/core/util/TimerTest.java
index 67b40d07e3..c9fcb9e464 100644
--- a/core/src/test/java/org/apache/accumulo/core/util/TimerTest.java
+++ b/core/src/test/java/org/apache/accumulo/core/util/TimerTest.java
@@ -96,4 +96,38 @@ public class TimerTest {
 
   }
 
+  @Test
+  public void testStartNewWithOffsetDuration() throws InterruptedException {
+    Timer timer = Timer.startNewWithOffset(Duration.ofMillis(100));
+
+    assertFalse(timer.hasElapsed(Duration.ZERO));
+
+    Thread.sleep(50);
+
+    assertFalse(timer.hasElapsed(Duration.ZERO),
+        "The timer should not indicate time has elapsed before the offset has 
passed.");
+
+    Thread.sleep(60);
+
+    assertTrue(timer.hasElapsed(Duration.ZERO),
+        "The timer should indicate time has elapsed after the offset has 
passed.");
+  }
+
+  @Test
+  public void testStartNewWithOffsetTimeUnit() throws InterruptedException {
+    Timer timer = Timer.startNewWithOffset(100, MILLISECONDS);
+
+    assertFalse(timer.hasElapsed(0, MILLISECONDS));
+
+    Thread.sleep(50);
+
+    assertFalse(timer.hasElapsed(0, MILLISECONDS),
+        "The timer should not indicate time has elapsed before the offset has 
passed.");
+
+    Thread.sleep(60);
+
+    assertTrue(timer.hasElapsed(0, MILLISECONDS),
+        "The timer should indicate time has elapsed after the offset has 
passed.");
+  }
+
 }

Reply via email to