-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrowingReactiveDuration.java
142 lines (140 loc) · 4.07 KB
/
GrowingReactiveDuration.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Part of Reactive time for Hookless: https://hookless.machinezoo.com/time
package com.machinezoo.hookless.time;
import java.math.*;
import java.time.*;
import java.time.temporal.*;
import com.machinezoo.stagean.*;
/**
* Reactive version of {@link Duration}, positive (growing) variant.
*/
@DraftApi("requires review")
@DraftCode("requires review")
@NoTests
@StubDocs
public class GrowingReactiveDuration extends ReactiveDuration implements Comparable<GrowingReactiveDuration> {
GrowingReactiveDuration(ReactiveClock clock, Instant zero) {
super(clock, zero);
}
@Override
public int compareTo(GrowingReactiveDuration other) {
clock.checkSame(other.clock);
return other.zero.compareTo(zero);
}
@Override
public int compareTo(Duration duration) {
return clock.compareTo(zero.plus(duration));
}
@Override
public boolean isPositive() {
return clock.isAfter(zero);
}
@Override
public boolean isNegative() {
return clock.isBefore(zero);
}
@Override
public boolean isZero() {
return clock.isAt(zero);
}
@Override
public GrowingReactiveDuration plus(Duration duration) {
return new GrowingReactiveDuration(clock, zero.minus(duration));
}
public Duration plus(ShrinkingReactiveDuration other) {
return Duration.between(zero, other.zero);
}
@Override
public GrowingReactiveDuration plus(long amount, TemporalUnit unit) {
return plus(Duration.of(amount, unit));
}
@Override
public GrowingReactiveDuration plusDays(long days) {
return plus(Duration.ofDays(days));
}
@Override
public GrowingReactiveDuration plusHours(long hours) {
return plus(Duration.ofHours(hours));
}
@Override
public GrowingReactiveDuration plusMinutes(long minutes) {
return plus(Duration.ofMinutes(minutes));
}
@Override
public GrowingReactiveDuration plusSeconds(long seconds) {
return plus(Duration.ofSeconds(seconds));
}
@Override
public GrowingReactiveDuration plusMillis(long millis) {
return plus(Duration.ofMillis(millis));
}
@Override
public GrowingReactiveDuration plusNanos(long nanos) {
return plus(Duration.ofNanos(nanos));
}
@Override
public GrowingReactiveDuration minus(Duration duration) {
return plus(duration.negated());
}
public Duration minus(GrowingReactiveDuration other) {
return Duration.between(zero, other.zero);
}
@Override
public GrowingReactiveDuration minus(long amount, TemporalUnit unit) {
return minus(Duration.of(amount, unit));
}
@Override
public GrowingReactiveDuration minusDays(long days) {
return minus(Duration.ofDays(days));
}
@Override
public GrowingReactiveDuration minusHours(long hours) {
return minus(Duration.ofHours(hours));
}
@Override
public GrowingReactiveDuration minusMinutes(long minutes) {
return minus(Duration.ofMinutes(minutes));
}
@Override
public GrowingReactiveDuration minusSeconds(long seconds) {
return minus(Duration.ofSeconds(seconds));
}
@Override
public GrowingReactiveDuration minusMillis(long millis) {
return minus(Duration.ofMillis(millis));
}
@Override
public GrowingReactiveDuration minusNanos(long nanos) {
return minus(Duration.ofNanos(nanos));
}
@Override
public ShrinkingReactiveDuration negated() {
return new ShrinkingReactiveDuration(clock, zero);
}
@Override
public Duration truncatedTo(Duration unit) {
if (unit.isNegative() || unit.isZero())
throw new IllegalArgumentException("Can only truncate with positive unit");
Duration duration = Duration.between(zero, clock.instant());
BigInteger bigUnit = big(unit);
Duration truncated = unbig(big(duration).divide(bigUnit).multiply(bigUnit));
Instant constraint = zero.plus(truncated);
if (!duration.isNegative()) {
clock.constrainLeftClosed(constraint);
clock.constrainRightOpen(constraint.plus(unit));
} else {
clock.constrainLeftOpen(constraint);
clock.constrainRightClosed(constraint.minus(unit));
}
return truncated;
}
public Instant subtractFrom(ReactiveInstant instant) {
return instant.minus(this);
}
public ReactiveInstant addTo(Instant instant) {
return new ReactiveInstant(clock, Duration.between(zero, instant));
}
@Override
public String toString() {
return "now - " + zero.toString();
}
}