Class Clockwork

java.lang.Object
dev.twilite.game.common.Clockwork

public class Clockwork extends Object
Small mutable clock for measuring runtime, countdown windows, pauses, and rates.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Builder-style entry point for creating a clock.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns whether this clock is still inside its countdown window.
    age()
    Returns how long this clock has been running, excluding frozen time.
    Returns this clock's age as HH:mm:ss.
    boolean
    Returns whether this clock has a countdown window.
    static Clockwork
    Creates a countdown clock for the supplied duration.
    boolean
    Returns whether this clock has reached or passed its countdown limit.
    double
    hourly(long value)
    Returns the value scaled to one hour based on this clock's age.
    Returns this clock's remaining countdown window as HH:mm:ss.
    void
    Re-marks this clock from now and preserves the current countdown window length.
    boolean
    olderThan(Duration duration)
    Returns whether this clock's age is greater than the supplied duration.
    static Clockwork
    Creates an open-ended clock using the system clock.
    Returns when this clock was last marked.
    double
    pace(long value, Duration duration)
    Returns the value scaled to the supplied duration based on this clock's age.
    void
    Freezes this clock until resume() is called.
    boolean
    Returns whether this clock is currently frozen.
    Returns the time left in this clock's countdown window, or zero when unbounded or expired.
    void
    Resumes this clock and shifts its origin and countdown limit by the frozen duration.
    static String
    text(Duration duration)
    Formats a duration as HH:mm:ss.
     
    use()
    Begins building a clock using the system clock.
    use(Clock clock)
    Begins building a clock using a custom clock.
    Begins building a clock using a custom instant source.
    void
    window(Duration duration)
    Sets the countdown window from now.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • use

      public static Clockwork.Seed use()
      Begins building a clock using the system clock.
    • use

      public static Clockwork.Seed use(Clock clock)
      Begins building a clock using a custom clock.
    • use

      public static Clockwork.Seed use(Supplier<Instant> clock)
      Begins building a clock using a custom instant source.
    • open

      public static Clockwork open()
      Creates an open-ended clock using the system clock.
    • countdown

      public static Clockwork countdown(Duration window)
      Creates a countdown clock for the supplied duration.
    • origin

      public Instant origin()
      Returns when this clock was last marked.
    • age

      public Duration age()
      Returns how long this clock has been running, excluding frozen time.
    • paused

      public boolean paused()
      Returns whether this clock is currently frozen.
    • bounded

      public boolean bounded()
      Returns whether this clock has a countdown window.
    • active

      public boolean active()
      Returns whether this clock is still inside its countdown window.
    • expired

      public boolean expired()
      Returns whether this clock has reached or passed its countdown limit.
    • olderThan

      public boolean olderThan(Duration duration)
      Returns whether this clock's age is greater than the supplied duration.
    • mark

      public void mark()
      Re-marks this clock from now and preserves the current countdown window length.
    • window

      public void window(Duration duration)
      Sets the countdown window from now.
    • pause

      public void pause()
      Freezes this clock until resume() is called.
    • resume

      public void resume()
      Resumes this clock and shifts its origin and countdown limit by the frozen duration.
    • remaining

      public Duration remaining()
      Returns the time left in this clock's countdown window, or zero when unbounded or expired.
    • pace

      public double pace(long value, Duration duration)
      Returns the value scaled to the supplied duration based on this clock's age.
    • hourly

      public double hourly(long value)
      Returns the value scaled to one hour based on this clock's age.
    • ageText

      public String ageText()
      Returns this clock's age as HH:mm:ss.
    • leftText

      public String leftText()
      Returns this clock's remaining countdown window as HH:mm:ss.
    • text

      public static String text(Duration duration)
      Formats a duration as HH:mm:ss.
    • toString

      public String toString()
      Overrides:
      toString in class Object