Class StopWatch

java.lang.Object
net.storm.sdk.commons.StopWatch

public class StopWatch extends Object
A utility class for measuring elapsed time and calculating rates.

StopWatch provides functionality for:

  • Tracking elapsed time since start
  • Setting optional end times for countdown functionality
  • Calculating rates (e.g., items per hour)
  • Formatting elapsed/remaining time as strings

Example usage:


 StopWatch timer = StopWatch.start();

 // Later...
 Duration elapsed = timer.getElapsed();
 String formatted = timer.toElapsedString(); // "01:30:45"

 // Calculate hourly rate
 double xpPerHour = timer.getHourlyRate(totalXpGained);
 
See Also:
  • Method Details

    • start

      public static StopWatch start(Supplier<Instant> supplier)
      Creates and starts a new StopWatch using a custom instant supplier.
      Parameters:
      supplier - the supplier providing the start instant
      Returns:
      a new StopWatch instance
    • start

      public static StopWatch start()
      Creates and starts a new StopWatch using the current time.
      Returns:
      a new StopWatch instance started at the current time
    • exceeds

      public boolean exceeds(Duration duration)
      Checks if the elapsed time exceeds the specified duration.
      Parameters:
      duration - the duration to compare against
      Returns:
      true if elapsed time exceeds the duration, false otherwise
    • setEndIn

      public void setEndIn(Duration duration)
      Sets the end time to a specified duration from now.

      This is useful for countdown timers.

      Parameters:
      duration - the duration from now when the stopwatch should end
    • isRunning

      public boolean isRunning()
      Checks if the stopwatch is still running (has not reached its end time).
      Returns:
      true if no end time is set or current time is before end, false if the end time has been reached
    • getElapsed

      public Duration getElapsed()
      Gets the elapsed duration since the stopwatch was started.
      Returns:
      the elapsed duration
    • getRemaining

      public Duration getRemaining()
      Gets the remaining duration until the end time.
      Returns:
      the remaining duration, or Duration.ZERO if no end time is set
    • toElapsedString

      public String toElapsedString()
      Formats the elapsed time as a HH:MM:SS string.
      Returns:
      the formatted elapsed time string
    • toRemainingString

      public String toRemainingString()
      Formats the remaining time as a HH:MM:SS string.
      Returns:
      the formatted remaining time string
    • reset

      public void reset()
      Resets the stopwatch to the current time.

      If an end time was set, the same duration will be applied from the new start time.

    • getRate

      public double getRate(long value, Duration rate)
      Calculates the rate of a value over a specified time period.
      Parameters:
      value - the accumulated value (e.g., experience gained)
      rate - the time period to calculate the rate for
      Returns:
      the rate (value per time period)
    • getHourlyRate

      public double getHourlyRate(long value)
      Calculates the hourly rate of a value.

      This is a convenience method equivalent to getRate(value, Duration.ofHours(1)).

      Parameters:
      value - the accumulated value (e.g., experience gained)
      Returns:
      the hourly rate