Class ControllableClock

    • Method Detail

      • freezedAt

        public static ControllableClock freezedAt​(LocalDateTime dateTime)
        Create a new controllable clock freezed at the instant resolved from a given LocalDateTime and the default time zone. The clock will not progress on its own, and will yield the instant it is set to.
        Parameters:
        dateTime - The date and time to set for the new freezed clock.
        Returns:
        the new ControllableClock
      • freezedAt

        public static ControllableClock freezedAt​(ZonedDateTime dateTime)
        Create a new controllable clock freezed at the instant of a given ZonedDateTime. The clock will not progress on its own, and will yield the instant it is set to.
        Parameters:
        dateTime - The date and time to set for the new freezed clock.
        Returns:
        the new ControllableClock
      • freezedAt

        public static ControllableClock freezedAt​(Instant instant)
        Create a new controllable clock freezed at a given instant, and with the default time zone. The clock will not progress on its own, and will yield the instant it is set to.
        Parameters:
        instant - The instant to set for the new freezed clock.
        Returns:
        the new ControllableClock
      • freezedAt

        public static ControllableClock freezedAt​(Instant instant,
                                                  ZoneId zone)
        Create a new controllable clock freezed at a given Instant, and with a given ZoneId. The clock will not progress on its own, and will yield a set instant.
        Parameters:
        instant - The instant to set for the new freezed clock.
        zone - The time zone of the new freezed clock.
        Returns:
        the new ControllableClock
      • control

        public static ControllableClock control​(Clock clock)
        Create a controllable clock based on an existing clock. The new ControllableClock will have the same behavior (freezed or progressing) and time as the given clock, but may be mutated to yield another instant, or be freezed. The given clock is of course not altered.
        Parameters:
        clock - The clock to base the new ControllableClock instance on.
        Returns:
        the new ControllableClock
      • doWithTimeAdjusted

        public <X extends Exception> void doWithTimeAdjusted​(Consumer<ClockAdjuster> adjustClock,
                                                             ThrowingConsumer<Instant,​X> action)
                                                      throws X extends Exception
        Perform an action with the clock adjusted, and have the clock reset to it's original state after the action has finished.
        Type Parameters:
        X - Exception the may be thrown from the given action
        Parameters:
        adjustClock - how to adjust the clock before running the action
        action - the action to perform, which is given an instant resolved from the adjusted clock
        Throws:
        X - if the given action throws an exception
        X extends Exception
      • getWithTimeAdjusted

        public <T,​X extends Exception> T getWithTimeAdjusted​(Consumer<ClockAdjuster> adjustClock,
                                                                   ThrowingFunction<Instant,​T,​X> resolveValue)
                                                            throws X extends Exception
        Resolve a value with the clock adjusted, and have the clock reset to it's original state after the operation has finished.
        Type Parameters:
        T - The returned type
        X - Exception the may be thrown from the given function
        Parameters:
        adjustClock - how to adjust the clock before running the action
        resolveValue - the operation which resolves the value, which is given an instant resolved from the adjusted clock
        Returns:
        the value returned from the given resolveValue function
        Throws:
        X - if the function throws an exception while resolving the value.
        X extends Exception
      • set

        public void set​(UnaryOperator<Clock> createNewClock)
        Description copied from interface: ClockAdjuster
        Set a new clock to resolve the time from.
        Specified by:
        set in interface ClockAdjuster
        Parameters:
        createNewClock - function which is given the current clock, which may be used to create a new clock.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Clock
      • timePasses

        public default void timePasses​(Duration amountOfTime)
        Description copied from interface: ClockAdjuster
        Signal that time is passing a given amount of time.
        Specified by:
        timePasses in interface ClockAdjuster
        Parameters:
        amountOfTime - the amount of time which are passing.
      • timePasses

        public default void timePasses​(TemporalAmount amountOfTime)
        Description copied from interface: ClockAdjuster
        Signal that time is passing a given amount of time.
        Specified by:
        timePasses in interface ClockAdjuster
        Parameters:
        amountOfTime - the amount of time which are passing.
      • freeze

        public default void freeze()
        Description copied from interface: ClockAdjuster
        Freeze the clock at its current instant in time.
        Specified by:
        freeze in interface ClockAdjuster
      • set

        public default void set​(Instant instant,
                                ZoneId zone)
        Description copied from interface: ClockAdjuster
        Set the time of the clock to the given instant and zone.
        Specified by:
        set in interface ClockAdjuster
        Parameters:
        instant - the instant to set.
        zone - the zone to set.
      • set

        public default void set​(Instant instant)
        Description copied from interface: ClockAdjuster
        Set the time to the given instant.
        Specified by:
        set in interface ClockAdjuster
        Parameters:
        instant - the instant to set.
      • set

        public default void set​(ZonedDateTime zonedDateTime)
        Description copied from interface: ClockAdjuster
        Set the time to the given zoned date and time.
        Specified by:
        set in interface ClockAdjuster
        Parameters:
        zonedDateTime - the date and time to set.
      • set

        public default void set​(OffsetDateTime offsetDateTime)
        Description copied from interface: ClockAdjuster
        Set the time to the given offset date and time.
        Specified by:
        set in interface ClockAdjuster
        Parameters:
        offsetDateTime - the date and time to set.
      • set

        public default void set​(LocalDateTime localDateTime)
        Description copied from interface: ClockAdjuster
        Set the time to the given local date and time.
        Specified by:
        set in interface ClockAdjuster
        Parameters:
        localDateTime - the date and time to set.
      • zonedDateTime

        public default ZonedDateTime zonedDateTime()
        Gets the current ZonedDateTime resolved with the zone of the clock.
        Returns:
        the current time as a zoned date and time.
      • localDateTime

        public default LocalDateTime localDateTime()
        Gets the current LocalDateTime resolved for the zone of the clock.
        Returns:
        the current time as a local date and time.
      • offsetDateTime

        public default OffsetDateTime offsetDateTime()
        Gets the current OffsetDateTime resolved for the zone of the clock.
        Returns:
        the current time as a date and time with zone offset.