Repository navigation
Expand file tree
/
Copy pathSequence.java
More file actions
180 lines (125 loc) · 5.57 KB
/
Copy pathSequence.java
File metadata and controls
180 lines (125 loc) · 5.57 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.abilityapi.sequenceapi;
import com.abilityapi.sequenceapi.action.Action;
import com.abilityapi.sequenceapi.action.type.observe.ObserverAction;
import com.abilityapi.sequenceapi.action.type.schedule.ScheduleAction;
import com.abilityapi.sequenceapi.origin.Origin;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
public abstract class Sequence<T> {
private final Origin origin;
private final SequenceBlueprint<T> sequenceBlueprint;
private final Map<ScheduleAction, Integer> scheduleActions = new HashMap<>();
private final Map<ObserverAction<T>, Integer> observerActions = new HashMap<>();
private long lastExecutionTime = System.currentTimeMillis();
private AtomicInteger index = new AtomicInteger(0);
private AtomicLong ticks = new AtomicLong(0);
private State state = State.INACTIVE;
public Sequence(final Origin origin, final SequenceBlueprint<T> sequenceBlueprint,
final Map<ScheduleAction, Integer> scheduleActions,
final Map<ObserverAction<T>, Integer> observerActions) {
this.origin = origin;
this.sequenceBlueprint = sequenceBlueprint;
this.scheduleActions.putAll(scheduleActions);
this.observerActions.putAll(observerActions);
}
public boolean applyObserve(final T event, final Origin origin) {
Iterator<ObserverAction<T>> iterator = this.observerActions.keySet().iterator();
if (this.state.equals(State.INACTIVE)) this.state = State.ACTIVE;
if (iterator.hasNext()) {
ObserverAction<T> action = iterator.next();
if (this.observerActions.get(action) != this.index.get()) return false;
this.index.incrementAndGet();
long current = System.currentTimeMillis();
// 1. Check that the event is the correct one for this action.
if (!action.getEventClass().equals(event.getClass())) return this.fail(action, origin);
// 2. Fail the action if it is being executed before the delay.
if (this.lastExecutionTime + ((action.getDelay() / 20) * 1000) > current) {
return this.fail(action, origin);
}
// 3. Fail the action if it being executed after the expire.
if (this.lastExecutionTime + ((action.getExpire() / 20) * 1000) < current) {
return this.fail(action, origin);
}
// 4. Run the action conditions and fail if they do not pass.
if (!action.apply(origin)) {
return this.fail(action, origin);
}
// 5. Succeed the action, remove it and set finish if there are no more actions left.
iterator.remove();
action.success(origin);
this.lastExecutionTime = System.currentTimeMillis();
if (this.index.get() >= this.observerActions.size() + this.scheduleActions.size()) {
this.state = State.FINISHED;
}
}
return true;
}
public boolean applySchedule(final Origin origin) {
Iterator<ScheduleAction> iterator = this.scheduleActions.keySet().iterator();
if (this.state.equals(State.INACTIVE)) this.state = State.ACTIVE;
this.ticks.incrementAndGet();
if (iterator.hasNext()) {
ScheduleAction action = iterator.next();
if (this.scheduleActions.get(action) != this.index.get()) return false;
this.index.incrementAndGet();
long current = System.currentTimeMillis();
// 1. Fail the action if it is being executed before the delay.
if (this.lastExecutionTime + ((action.getDelay() / 20) * 1000) > current) {
return this.fail(action, origin);
}
// 2. Fail the action if it being executed after the expire.
if (this.lastExecutionTime + ((action.getExpire() / 20) * 1000) < current) {
return this.fail(action, origin);
}
// 3. Check that the tick is being executed in the period wanted.
if (this.ticks.get() % action.getPeriod() != 0) {
return false;
}
// 4. Run the action conditions and fail if they do not pass.
if (!action.apply(origin)) {
return this.fail(action, origin);
}
// 5. Succeed the action, remove it and set finish if there are no more actions left.
iterator.remove();
action.success(origin);
this.lastExecutionTime = System.currentTimeMillis();
if (this.index.get() >= this.observerActions.size() + this.scheduleActions.size()) {
this.state = State.FINISHED;
}
}
return true;
}
private boolean fail(final Action action, final Origin origin) {
this.state = action.failure(origin) ? State.CANCELLED : this.state;
return false;
}
public SequenceBlueprint<T> getBlueprint() {
return null;
}
public Origin getOrigin() {
return null;
}
public Class<? extends T> getTrigger() {
return null;
}
public State getState() {
return null;
}
public enum State {
ACTIVE(true),
INACTIVE(true),
CANCELLED(false),
EXPIRED(false),
FINISHED(true);
private final boolean safe;
State(boolean safe) {
this.safe = safe;
}
public boolean isSafe() {
return this.safe;
}
}
}