-
Notifications
You must be signed in to change notification settings - Fork 8
/
FixedRateTrigger.java
56 lines (47 loc) · 1.46 KB
/
FixedRateTrigger.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
package com.fluxtion.runtime.time;
import com.fluxtion.runtime.annotations.Initialise;
import com.fluxtion.runtime.annotations.NoTriggerReference;
import com.fluxtion.runtime.annotations.OnEventHandler;
import lombok.Getter;
//@EqualsAndHashCode
public class FixedRateTrigger {
@NoTriggerReference
private final Clock clock;
private final int rate;
private long previousTime;
/**
* number of triggers that should have fired since the last time update to now
*/
@Getter
private int triggerCount;
public static FixedRateTrigger atMillis(int millis) {
return new FixedRateTrigger(millis);
}
public FixedRateTrigger(int rate) {
this(Clock.DEFAULT_CLOCK, rate);
}
public FixedRateTrigger(Clock clock, int rate) {
this.clock = clock;
this.rate = rate;
}
@OnEventHandler
public boolean hasExpired(Object input) {
long newTime = clock.getWallClockTime();
boolean expired = rate <= (newTime - previousTime);
if (expired) {
triggerCount = (int) ((newTime - previousTime) / rate);
previousTime += (long) triggerCount * rate;
}
return expired;
}
@OnEventHandler(propagate = false)
public boolean setClockStrategy(ClockStrategy.ClockStrategyEvent event) {
init();
return false;
}
@Initialise
public void init() {
previousTime = clock.getWallClockTime();
triggerCount = 0;
}
}