Skip to content

Commit

Permalink
Added supplementary actions
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Feb 16, 2019
1 parent 3caca12 commit e7e0cd7
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ time to end and change the active action. There is a factory method to create th
* Use tabs instead of spaces, 2 tabs for continued line indent. (Only for *.java files)
* Reference interfaces instead of concrete classes in method signatures as much as possible
### TODO
* Create an Action that runs two actions. If the first action is done, the whole action becomes done
* Nothing
7 changes: 7 additions & 0 deletions src/main/java/me/retrodaredevil/action/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ public static Action createEndOverrideRecyclable(Action action){
return new OverrideEndAction(true, action);
}
// endregion

public static LinkedAction createSupplementaryLinkedAction(LinkedAction linkedAction, Action supplementaryAction){
return new SupplementaryLinkedAction(linkedAction, supplementaryAction);
}
public static Action createSupplementaryAction(Action mainAction, Action supplementaryAction){
return new SupplementaryAction(mainAction, supplementaryAction);
}

static abstract class Builder<T extends Builder> {
protected boolean canBeDone = true, canRecycle = false;
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/me/retrodaredevil/action/SupplementaryAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.retrodaredevil.action;

class SupplementaryAction implements Action {
private final Action mainAction, supplementaryAction;
public SupplementaryAction(Action mainAction, Action supplementaryAction) {
this.mainAction = mainAction;
this.supplementaryAction = supplementaryAction;
}

@Override
public void update() {
mainAction.update();
supplementaryAction.update();
}

@Override
public void end() {
mainAction.end();
supplementaryAction.end();
}

@Override
public boolean isDone() {
return mainAction.isDone();
}

@Override
public boolean isActive() {
return mainAction.isActive();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.retrodaredevil.action;

public class SupplementaryLinkedAction implements LinkedAction {

private final LinkedAction linkedAction;
private final Action supplementaryAction;

public SupplementaryLinkedAction(LinkedAction linkedAction, Action supplementaryAction) {
this.linkedAction = linkedAction;
this.supplementaryAction = supplementaryAction;
}

@Override
public Action getNextAction() {
return linkedAction.getNextAction();
}

@Override
public void update() {
linkedAction.update();
supplementaryAction.update();
}

@Override
public void end() {
linkedAction.end();
supplementaryAction.end();
}

@Override
public boolean isDone() {
return linkedAction.isDone();
}

@Override
public boolean isActive() {
return linkedAction.isActive();
}
}
40 changes: 40 additions & 0 deletions src/test/java/me/retrodaredevil/action/test/SupplementaryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.retrodaredevil.action.test;

import me.retrodaredevil.action.Action;
import me.retrodaredevil.action.Actions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

final class SupplementaryTest {
@Test
void testSupplementaryAction(){
final int[] timesRan = {0};
final int[] timesRanSupplementary = {0};
final Action mainAction = Actions.createRunForever(() -> timesRan[0]++);
final Action supplementaryAction = Actions.createRunOnce(() -> timesRanSupplementary[0]++);
final Action action = Actions.createSupplementaryAction(mainAction, supplementaryAction);

assertFalse(action.isActive());
assertFalse(mainAction.isActive());
assertFalse(supplementaryAction.isActive());

action.update();
assertTrue(action.isActive());
assertTrue(mainAction.isActive());
assertTrue(supplementaryAction.isActive());
assertFalse(action.isDone());
assertFalse(mainAction.isDone());
assertTrue(supplementaryAction.isDone());

action.update();
assertEquals(2, timesRan[0]);
assertEquals(1, timesRanSupplementary[0]);

action.end();
assertFalse(action.isActive());
assertFalse(mainAction.isActive());
assertFalse(supplementaryAction.isActive());

}
}

0 comments on commit e7e0cd7

Please sign in to comment.