-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactiveAlarm.java
41 lines (39 loc) · 1.14 KB
/
ReactiveAlarm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Part of Reactive time for Hookless: https://hookless.machinezoo.com/time
package com.machinezoo.hookless.time;
import java.time.*;
/*
* Alarm is an immutable version of ReactiveClock for indexing in AlarmIndex.
* ReactiveClock must hold a reference to current ReactiveAlarm in order to protect it from GC.
*/
class ReactiveAlarm {
/*
* Alarm's range of valid times is half-closed: [lower, upper).
*/
final Instant lower;
final Instant upper;
private final ReactiveClock clock;
ReactiveAlarm(Instant lower, Instant upper, ReactiveClock clock) {
this.lower = lower;
this.upper = upper;
this.clock = clock;
}
void ring() {
clock.ring();
}
ReactiveAlarm constrainUpper(Instant time) {
if (upper == null || time.isBefore(upper))
return new ReactiveAlarm(lower, time, clock);
else
return this;
}
ReactiveAlarm constrainLower(Instant time) {
if (lower == null || time.isAfter(lower))
return new ReactiveAlarm(time, upper, clock);
else
return this;
}
@Override
public String toString() {
return "[" + (lower != null ? lower.toString() : "infinity") + ", " + (upper != null ? upper.toString() : "infinity") + ")";
}
}