com.ossuminc.riddl.utils.AdjustableClock.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of riddl-utils_3 Show documentation
Show all versions of riddl-utils_3 Show documentation
Various utilities used throughout riddl libraries
/*
* Copyright 2019 Ossum, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
package com.ossuminc.riddl.utils
import java.time.*
/** An implementation of java.time.Clock which only moves through time via calls
* to `updateInstant`. This allows fine-grained, side-effect-free deterministic
* control over the progression of system time, which is useful for testing
* purposes.
*/
final class AdjustableClock(
private var inst: Instant,
zone: ZoneId = ZoneId.of("UTC"))
extends Clock {
override def getZone: ZoneId = zone
override def withZone(zone: ZoneId): Clock =
new AdjustableClock(instant(), zone)
override def instant(): Instant = inst
/** Updates the current time of this clock to the result of applying `f` to
* the current time
*/
def updateInstant(f: Instant => Instant): this.type = {
this.inst = f(this.inst)
this
}
}