Skip to content

Commit 25ed5b6

Browse files
Implement seperate action expirable interface for actions that can expire
1 parent f068019 commit 25ed5b6

8 files changed

Lines changed: 46 additions & 51 deletions

File tree

‎src/main/java/com/abilityapi/sequenceapi/Sequence.java‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.abilityapi.sequenceapi;
22

33
import com.abilityapi.sequenceapi.action.Action;
4+
import com.abilityapi.sequenceapi.action.Expirable;
45
import com.abilityapi.sequenceapi.action.type.after.AfterAction;
56
import com.abilityapi.sequenceapi.action.type.observe.ObserverAction;
67
import com.abilityapi.sequenceapi.action.type.schedule.ScheduleAction;
@@ -139,21 +140,14 @@ public void applyAfter(final SequenceContext sequenceContext) {
139140
return;
140141
}
141142

142-
// 2. Fail the action if it being executed after the expire.
143-
144-
if (this.lastTime + ((action.getExpire() / 20) * 1000) < current) {
145-
this.fail(action, sequenceContext);
146-
return;
147-
}
148-
149-
// 3. Run the action conditions and fail if they do not pass.
143+
// 2. Run the action conditions and fail if they do not pass.
150144

151145
if (!action.apply(sequenceContext)) {
152146
this.fail(action, sequenceContext);
153147
return;
154148
}
155149

156-
// 4. Succeed the action, remove it, increment the index and set finish if there are no more actions left.
150+
// 3. Succeed the action, remove it, increment the index and set finish if there are no more actions left.
157151

158152
this.index++;
159153

@@ -318,11 +312,13 @@ public enum State {
318312
public void update(final Sequence sequence) {
319313
// Set no more actions as expired.
320314
if (sequence.actions.isEmpty()) {
321-
sequence.state = State.EXPIRED;
315+
sequence.state = State.FINISHED;
322316
return;
323317
}
324318

325-
Action action = (Action) sequence.actions.get(0);
319+
if (!(sequence.actions.get(0) instanceof Expirable)) return;
320+
321+
Expirable action = (Expirable) sequence.actions.get(0);
326322
if (sequence.getLastActionTime() + ((action.getExpire() / 20) * 1000) < System.currentTimeMillis()) sequence.state = State.EXPIRED;
327323
}
328324

‎src/main/java/com/abilityapi/sequenceapi/SequenceManager.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ public void clean(final SequenceContext sequenceContext, boolean force) {
229229
this.sequences.get(sequenceContext.getId()).removeIf(sequence -> {
230230
if (sequence.getState().equals(Sequence.State.FINISHED)) return force;
231231

232-
sequence.getState().update(sequence);
233-
234232
return force || sequence.getState().equals(Sequence.State.EXPIRED);
235233
});
236234
}
@@ -246,6 +244,9 @@ public boolean _invokeObserver(final T event, final Sequence<T> sequence, final
246244

247245
final Sequence.State sequenceState = sequence.getState();
248246

247+
// Check for an updated state first.
248+
sequenceState.update(sequence);
249+
249250
if (!sequenceState.isSafe()) {
250251
remove = true;
251252
}
@@ -272,6 +273,9 @@ public boolean _invokeAfter(final Sequence<T> sequence, final SequenceContext se
272273

273274
final Sequence.State sequenceState = sequence.getState();
274275

276+
// Check for an updated state first.
277+
sequenceState.update(sequence);
278+
275279
if (!sequenceState.isSafe()) {
276280
remove = true;
277281
}
@@ -298,6 +302,9 @@ public boolean _invokeScheduler(final Sequence<T> sequence, final SequenceContex
298302

299303
final Sequence.State sequenceState = sequence.getState();
300304

305+
// Check for an updated state first.
306+
sequenceState.update(sequence);
307+
301308
if (!sequenceState.isSafe()) {
302309
remove = true;
303310
}
@@ -327,6 +334,10 @@ public void _createBlueprints(final T event, final SequenceContext sequenceConte
327334
final Sequence<T> sequence = sequenceBlueprint.create(sequenceContext);
328335

329336
if (sequence.applyObserve(event, sequenceContext)) {
337+
338+
// Check for an updated state first.
339+
sequence.getState().update(sequence);
340+
330341
if (!sequence.getState().isSafe()) {
331342
continue;
332343
}

‎src/main/java/com/abilityapi/sequenceapi/action/Action.java‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,13 @@ public interface Action {
2323
*/
2424
void setDelay(final int period);
2525

26-
/**
27-
* Sets an expire to this {@link Action}.
28-
*
29-
* @param period the period to expire
30-
*/
31-
void setExpire(final int period);
32-
3326
/**
3427
* Returns the delay period.
3528
*
3629
* @return the delay period
3730
*/
3831
int getDelay();
3932

40-
/**
41-
* Returns the expire period.
42-
*
43-
* @return the expire period
44-
*/
45-
int getExpire();
46-
4733
/**
4834
* Applies all {@link ConditionType#NORMAL} {@link Condition}s
4935
* in this {@link Action} and returns the result of it.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.abilityapi.sequenceapi.action;
2+
3+
/**
4+
* Represents an {@link Action} that can expire.
5+
*/
6+
public interface Expirable {
7+
8+
/**
9+
* Sets an expire to this {@link Action}.
10+
*
11+
* @param period the period to expire
12+
*/
13+
void setExpire(final int period);
14+
15+
/**
16+
* Returns the expire period.
17+
*
18+
* @return the expire period
19+
*/
20+
int getExpire();
21+
22+
}

‎src/main/java/com/abilityapi/sequenceapi/action/type/after/AfterAction.java‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public class AfterAction implements Action {
1212

1313
private List<Condition> conditions = new ArrayList<>();
1414
private int delay = 0;
15-
private int expire = 0;
1615

1716
public AfterAction() {}
1817

@@ -26,21 +25,11 @@ public void setDelay(int period) {
2625
this.delay = period;
2726
}
2827

29-
@Override
30-
public void setExpire(int period) {
31-
this.expire = period;
32-
}
33-
3428
@Override
3529
public int getDelay() {
3630
return this.delay;
3731
}
3832

39-
@Override
40-
public int getExpire() {
41-
return this.expire;
42-
}
43-
4433
@Override
4534
public final boolean apply(final SequenceContext sequenceContext) {
4635
return this.conditions.stream()

‎src/main/java/com/abilityapi/sequenceapi/action/type/after/AfterActionBuilder.java‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@ public final AfterActionBuilder<T> delay(final int value) {
4545
return this;
4646
}
4747

48-
/**
49-
* Sets an expire to this {@link AfterAction}.
50-
*
51-
* @param value the period to expire
52-
* @return this builder
53-
*/
54-
public final AfterActionBuilder<T> expire(final int value) {
55-
this.action.setExpire(value);
56-
return this;
57-
}
58-
5948
@Override
6049
public final ObserverActionBuilder<T> observe(final Class<? extends T> event) {
6150
return this.observe(new ObserverAction<>(event));

‎src/main/java/com/abilityapi/sequenceapi/action/type/observe/ObserverAction.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.abilityapi.sequenceapi.Sequence;
44
import com.abilityapi.sequenceapi.SequenceContext;
55
import com.abilityapi.sequenceapi.action.Action;
6+
import com.abilityapi.sequenceapi.action.Expirable;
67
import com.abilityapi.sequenceapi.action.condition.Condition;
78
import com.abilityapi.sequenceapi.action.condition.ConditionType;
89

@@ -27,7 +28,7 @@
2728
*
2829
* @param <T> the event type
2930
*/
30-
public class ObserverAction<T> implements Action {
31+
public class ObserverAction<T> implements Action, Expirable {
3132

3233
private final Class<? extends T> eventClass;
3334

‎src/main/java/com/abilityapi/sequenceapi/action/type/schedule/ScheduleAction.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.abilityapi.sequenceapi.Sequence;
44
import com.abilityapi.sequenceapi.SequenceContext;
55
import com.abilityapi.sequenceapi.action.Action;
6+
import com.abilityapi.sequenceapi.action.Expirable;
67
import com.abilityapi.sequenceapi.action.condition.Condition;
78
import com.abilityapi.sequenceapi.action.condition.ConditionType;
89

@@ -33,7 +34,7 @@
3334
* which it will then execute the {@link ConditionType#SUCCESS}
3435
* {@link Condition}s one last time.</p>
3536
*/
36-
public class ScheduleAction implements Action {
37+
public class ScheduleAction implements Action, Expirable {
3738

3839
private List<Condition> conditions = new ArrayList<>();
3940
private int delay = 0;

0 commit comments

Comments
 (0)