Package net.storm.sdk.commons
Class StopWatch
java.lang.Object
net.storm.sdk.commons.StopWatch
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 Summary
Modifier and TypeMethodDescriptionbooleanChecks if the elapsed time exceeds the specified duration.Gets the elapsed duration since the stopwatch was started.doublegetHourlyRate(long value) Calculates the hourly rate of a value.doubleCalculates the rate of a value over a specified time period.Gets the remaining duration until the end time.booleanChecks if the stopwatch is still running (has not reached its end time).voidreset()Resets the stopwatch to the current time.voidSets the end time to a specified duration from now.static StopWatchstart()Creates and starts a new StopWatch using the current time.static StopWatchCreates and starts a new StopWatch using a custom instant supplier.Formats the elapsed time as a HH:MM:SS string.Formats the remaining time as a HH:MM:SS string.
-
Method Details
-
start
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
Creates and starts a new StopWatch using the current time.- Returns:
- a new StopWatch instance started at the current time
-
exceeds
Checks if the elapsed time exceeds the specified duration.- Parameters:
duration- the duration to compare against- Returns:
trueif elapsed time exceeds the duration,falseotherwise
-
setEndIn
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:
trueif no end time is set or current time is before end,falseif the end time has been reached
-
getElapsed
Gets the elapsed duration since the stopwatch was started.- Returns:
- the elapsed duration
-
getRemaining
Gets the remaining duration until the end time.- Returns:
- the remaining duration, or
Duration.ZEROif no end time is set
-
toElapsedString
Formats the elapsed time as a HH:MM:SS string.- Returns:
- the formatted elapsed time 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
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
-