Class SimpleStopWatch

  • All Implemented Interfaces:

    
    public final class SimpleStopWatch
    
                        

    Minimalistic stopwatch to measure the elapsed time between operations.

    This stopwatch does not support multi-threading. When implementing this stopwatch, make sure to take care of this properly, otherwise inconsistency and unexpected errors will be a problem for you. This stopwatch uses Clock.System.now to retrieve both the start-time and stop-time. A regular run expects you to call start, followed by stop, and then either of getTime or toString to retrieve the elapsed time as a formatted string.

    Author:

    Griefed

    • Constructor Detail

      • SimpleStopWatch

        SimpleStopWatch()
    • Method Detail

      • start

         final SimpleStopWatch start()

        Starts the stopwatch.

        After having started the stopwatch, call .stop, followed by any of

        • .getTime

        • .getTime

        • .toString

        to retrieve the elapsed time.

        Returns:

        This instance of SimpleStopWatch

      • stop

         final SimpleStopWatch stop()

        Stops the stopwatch.

        The stopwatch must have been started before calling this method, otherwise an IllegalStateException will be thrown. Call .start first!

        Returns:

        This instance of SimpleStopWatch

      • toString

         String toString()

        Get the elapsed time of this stopwatch, formatted using %2dh:%02dm:%02ds:%04dms.

        The stopwatch must be started and stopped before calling this method, .getTime, or .getTime. If any of the aforementioned methods are called, but the stopwatch was not started and stopped yet first, an IllegalStateException will be thrown.

        Returns:

        The elapsed time of a stopwatch run, formatted using %2d:%02d:%02d.

      • getTime

         final String getTime(DurationUnit unit, Integer decimals)

        Duration value expressed in the given unit and formatted with the specified decimals-amount of digits after the decimal point.

        The stopwatch must be started and stopped before calling this method or toString. If any of the aforementioned methods are called, but the stopwatch was not started and stopped yet first, an IllegalStateException will be thrown.

        Parameters:
        unit - Unit to use in duration representation.
        decimals - The number of digits after decimal point to show.
        Returns:

        The elapsed time, formatted with the provided formatting.

      • elapsed

         final Duration elapsed()

        Get the elapsed time in nanoseconds. Depending on the state of the stopwatch, two values or an IllegalStateException may be thrown.

        • Stopwatch is still running: The elapsed time up to the point of calling this method is returned.

        • Stopwatch started and stopped: The elapsed time between the start and stop is returned.

        • Any other state: An IllegalStateException is thrown.

        Returns:

        The elapsed time in nanoseconds.