Scoop API

scoop:scoop.std:0.1.0

src/time/time_mark.scoop

Selected production source · src/time/time_mark.scoop

1package scoop.time
3/**
4 * A source of relative time marks whose readings can be compared only within that same source.
5 */
6public interface TimeSource {
7 /**
8 * Captures a current reading together with this source identity.
9 */
10 public fun markNow(): TimeMark
13/**
14 * A relative reading tied to a specific TimeSource instance. Ordering and subtraction between
15 * marks from different sources throw.
16 */
17public struct TimeMark(val source: TimeSource, val reading: Duration)
18 : Equals<TimeMark>, Compare<TimeMark>, Add<Duration, TimeMark>, Sub<Duration, TimeMark> {
19 /**
20 * Compares both source identity and relative reading.
21 */
22 public override operator fun equals(other: TimeMark): Boolean = source === other.source && reading == other.reading
23 /**
24 * Orders readings from the same source; different source instances throw.
25 */
26 public override operator fun compareTo(other: TimeMark): Long {
27 checkSource(other)
28 return reading.compareTo(other.reading)
29 }
30 /**
31 * Returns a mark shifted by the duration while retaining its source.
32 */
33 public override operator fun plus(other: Duration): TimeMark = TimeMark(source, reading + other)
34 /**
35 * Shifts by a duration, or subtracts a mark from the same source to obtain an elapsed
36 * duration.
37 */
38 public override operator fun minus(other: Duration): TimeMark = TimeMark(source, reading - other)
39 /**
40 * Shifts by a duration, or subtracts a mark from the same source to obtain an elapsed
41 * duration.
42 */
43 public operator fun minus(other: TimeMark): Duration {
44 checkSource(other)
45 return reading - other.reading
46 }
47 private fun checkSource(other: TimeMark) {
48 if (source !== other.source) throw IllegalArgumentException(Some("different monotonic time sources"))
49 }