Created
June 21, 2023 09:22
-
-
Save daviddenton/d5dfcc51cb5c47b01c0e5cac8d096f72 to your computer and use it in GitHub Desktop.
AdjustableTickingClock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdjustableTickingClock( | |
private val startTime: Instant = EPOCH, | |
private val tick: Duration = Duration.ofSeconds(5) | |
) : Clock() { | |
private var currentTime = startTime | |
override fun getZone() = ZoneId.of("UTC") | |
override fun withZone(zone: ZoneId?) = this | |
override fun instant() = currentTime.also { currentTime += tick } | |
fun reset(): AdjustableTickingClock { | |
currentTime = startTime | |
return this | |
} | |
fun goBackBy(duration: Duration): AdjustableTickingClock { | |
currentTime -= duration | |
return this | |
} | |
fun goForwardBy(duration: Duration): AdjustableTickingClock { | |
currentTime += duration | |
return this | |
} | |
fun nowIs(now: Instant) { | |
currentTime = now | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment