Class DelegatingClock

java.lang.Object
java.time.Clock
io.fluxzero.common.DelegatingClock
All Implemented Interfaces:
InstantSource

public class DelegatingClock extends Clock
A concrete implementation of a Clock that delegates its method calls to another Clock instance, allowing runtime manipulation of the delegated clock.

This class is useful when you need to dynamically switch the clock's implementation during runtime, for example, testing scenarios requiring adjustable or controlled time flows.

The delegated clock instance is stored atomically to ensure thread-safe updates.

  • Constructor Details

    • DelegatingClock

      public DelegatingClock()
  • Method Details

    • setDelegate

      public void setDelegate(@NonNull @NonNull Clock delegate)
      Replaces the backing clock and notifies registered change listeners after the new clock is visible.
    • onChange

      public Registration onChange(@NonNull @NonNull Runnable listener)
      Registers a listener that is invoked whenever the backing clock changes.
      Parameters:
      listener - callback to invoke after setDelegate(Clock)
      Returns:
      registration that removes the listener
    • getZone

      public ZoneId getZone()
      Specified by:
      getZone in class Clock
    • withZone

      public Clock withZone(ZoneId zone)
      Specified by:
      withZone in interface InstantSource
      Specified by:
      withZone in class Clock
    • instant

      public Instant instant()
      Specified by:
      instant in interface InstantSource
      Specified by:
      instant in class Clock