Skip to content

Commit

Permalink
Fixed compiler error, added forceUpdateInOrder for multiplexer
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Mar 6, 2019
1 parent 58e3c51 commit 2a5012f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/main/java/me/retrodaredevil/action/Actions.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package me.retrodaredevil.action;

import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.*;

public final class Actions {
private Actions(){ throw new UnsupportedOperationException(); }
Expand Down Expand Up @@ -219,6 +216,7 @@ public T canRecycle(boolean b){
public static class ActionMultiplexerBuilder extends Builder<ActionMultiplexerBuilder>{
private final Action[] initialActions;
private boolean clearAllOnEnd = false;
private boolean updateInOrder = false;

public ActionMultiplexerBuilder(Action... initialActions){
this.initialActions = initialActions;
Expand All @@ -232,8 +230,18 @@ public ActionMultiplexerBuilder clearAllOnEnd(boolean b){
clearAllOnEnd = b;
return getThis();
}
public ActionMultiplexerBuilder forceUpdateInOrder(boolean b){
updateInOrder = b;
return getThis();
}
public ActionMultiplexer build(){
return new SetActionMultiplexer(canRecycle, new HashSet<>(Arrays.asList(initialActions)), canBeDone, clearAllOnEnd);
final Set<Action> actions;
if(updateInOrder){
actions = new LinkedHashSet<>(Arrays.asList(initialActions));
} else {
actions = new HashSet<>(Arrays.asList(initialActions));
}
return new SetActionMultiplexer(canRecycle, actions, canBeDone, clearAllOnEnd);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,29 @@ void testRemoving(){
assertFalse(value[0]);

}
@Test
void testForceOrder(){
final int[] i = {0};
final int[] value = {0};
final ActionMultiplexer multiplexer = new Actions.ActionMultiplexerBuilder(
Actions.createRunOnce(() -> {
assertEquals(0, i[0]++);
value[0]++;
}),
Actions.createRunOnce(() -> {
assertEquals(1, i[0]++);
value[0]++;
}),
Actions.createRunOnce(() -> {
assertEquals(2, i[0]++);
value[0]++;
}),
Actions.createRunOnce(() -> {
assertEquals(3, i[0]++);
value[0]++;
})
).forceUpdateInOrder(true).build();
multiplexer.update();
assertEquals(4, value[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class TryCatchActionTest {
@Test
void testTryCatchAction(){
final boolean[] ran = {false};
final Action action = new TryCatchAction<RuntimeException>(Actions.createRunOnce(() -> { throw new RuntimeException("my exception"); }), RuntimeException.class) {
final Action action = new TryCatchAction<RuntimeException>(false, Actions.createRunOnce(() -> { throw new RuntimeException("my exception"); }), RuntimeException.class) {
@Override
protected void onCatchUpdate(RuntimeException throwable) {
assertEquals("my exception", throwable.getMessage());
Expand Down

0 comments on commit 2a5012f

Please sign in to comment.