From 204cdcd618f061c0e575deff73c5dafb9de9d974 Mon Sep 17 00:00:00 2001 From: Consolegamer2 <160079707+Consolegamer2@users.noreply.github.com> Date: Wed, 30 Jul 2025 16:46:46 -0400 Subject: [PATCH 1/3] add Action Interface, SequentialAction, SimultaneousAction, and SleepAction --- .../main/java/codebase/actions/Action.java | 9 +++ .../codebase/actions/SequentialAction.java | 57 ++++++++++++++++++ .../codebase/actions/SimultaneousAction.java | 58 +++++++++++++++++++ .../java/codebase/actions/SleepAction.java | 27 +++++++++ 4 files changed, 151 insertions(+) create mode 100644 TeamCode/src/main/java/codebase/actions/Action.java create mode 100644 TeamCode/src/main/java/codebase/actions/SequentialAction.java create mode 100644 TeamCode/src/main/java/codebase/actions/SimultaneousAction.java create mode 100644 TeamCode/src/main/java/codebase/actions/SleepAction.java diff --git a/TeamCode/src/main/java/codebase/actions/Action.java b/TeamCode/src/main/java/codebase/actions/Action.java new file mode 100644 index 0000000..bab8736 --- /dev/null +++ b/TeamCode/src/main/java/codebase/actions/Action.java @@ -0,0 +1,9 @@ +package codebase.actions; + +import codebase.Loop; + +public interface Action extends Loop { + void init(); + + boolean isComplete(); +} diff --git a/TeamCode/src/main/java/codebase/actions/SequentialAction.java b/TeamCode/src/main/java/codebase/actions/SequentialAction.java new file mode 100644 index 0000000..ad5b15e --- /dev/null +++ b/TeamCode/src/main/java/codebase/actions/SequentialAction.java @@ -0,0 +1,57 @@ +package codebase.actions; + +import java.util.Arrays; + +public class SequentialAction implements Action { + + private ActionNode currentActionNode; + + public SequentialAction(Action... actions) { + if (actions.length == 0) { + throw new IllegalArgumentException("You must pass at least one action"); + } + + this.currentActionNode = new ActionNode(actions); + } + + @Override + public void init() { + currentActionNode.action.init(); + } + + @Override + public boolean isComplete() { + return this.currentActionNode == null; + } + + @Override + public void loop() { + if (this.currentActionNode.action.isComplete()) { + this.currentActionNode = this.currentActionNode.next; + } + + if (this.currentActionNode == null) { + return; + } + + this.currentActionNode.action.loop(); + } + + + +} + +class ActionNode { + public Action action; + public ActionNode next; + public ActionNode(Action[] actions) { + this.action = actions[0]; + if (actions.length == 1) { + this.next = null; + return; + } + this.next = new ActionNode(Arrays.copyOfRange(actions, 1, actions.length)); + } + + +} \ No newline at end of file diff --git a/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java b/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java new file mode 100644 index 0000000..dc4dc82 --- /dev/null +++ b/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java @@ -0,0 +1,58 @@ +package codebase.actions; + +import androidx.annotation.NonNull; + +import java.util.ArrayList; + +public class SimultaneousAction implements Action { + + private final ArrayList actions; + + public SimultaneousAction(Action... actions) { + if (actions.length == 0) { + throw new IllegalArgumentException("You must pass at least one action"); + } + + this.actions = new ArrayList(); + for (Action action : actions) { + this.add(action); + } + } + + @Override + public void init() { + for (Action action : actions) { + action.init(); + } + } + + @Override + public boolean isComplete() { + for (Action action : actions) { + if (!action.isComplete()) { + return false; + } + } + return true; + } + + @Override + public void loop() { + for (Action action : actions) { + if (!action.isComplete()) { + action.loop(); + } + } + } + + public void add(@NonNull Action action) { + String actionName = action.getClass().getName(); + + for (Action a : actions) { + if (a.getClass().getName().equals(actionName)) { + throw new IllegalArgumentException("You can't add multiple of the same Action to SimultaneousAction."); + } + } + actions.add(action); + } +} \ No newline at end of file diff --git a/TeamCode/src/main/java/codebase/actions/SleepAction.java b/TeamCode/src/main/java/codebase/actions/SleepAction.java new file mode 100644 index 0000000..ecaad07 --- /dev/null +++ b/TeamCode/src/main/java/codebase/actions/SleepAction.java @@ -0,0 +1,27 @@ +package codebase.actions; + +public class SleepAction implements Action{ + + private final long ms; + + private long endMs; + + public SleepAction(long ms) { + this.ms = ms; + } + + + @Override + public void init() { + this.endMs = System.currentTimeMillis() + this.ms; + } + + @Override + public boolean isComplete() { + return System.currentTimeMillis() >= this.endMs; + } + + @Override + public void loop() { + } +} From eef381c5d59809b0258b563853fb6f4652472657 Mon Sep 17 00:00:00 2001 From: liambridgers Date: Wed, 30 Jul 2025 17:04:49 -0400 Subject: [PATCH 2/3] add init option to add method of SimultaneousAction for adding actions during teleop after already running, remove explicit type argument of actions list in SimultaneousAction --- .../main/java/codebase/actions/SimultaneousAction.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java b/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java index dc4dc82..13eadfd 100644 --- a/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java +++ b/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java @@ -13,9 +13,9 @@ public SimultaneousAction(Action... actions) { throw new IllegalArgumentException("You must pass at least one action"); } - this.actions = new ArrayList(); + this.actions = new ArrayList<>(); for (Action action : actions) { - this.add(action); + this.add(action, false); } } @@ -45,7 +45,7 @@ public void loop() { } } - public void add(@NonNull Action action) { + public void add(@NonNull Action action, boolean init) { String actionName = action.getClass().getName(); for (Action a : actions) { @@ -54,5 +54,7 @@ public void add(@NonNull Action action) { } } actions.add(action); + + if (init) action.init(); } } \ No newline at end of file From 8f1cc30a608a7204becf22c1ce2af89682028b4d Mon Sep 17 00:00:00 2001 From: liambridgers Date: Thu, 31 Jul 2025 17:26:19 -0400 Subject: [PATCH 3/3] Fix spacing in SleepAction, change SequentialAction and SimultaneousAction to require at least on Action parameter at compile time, change formatting of brackets and change wording of exception in SimultaneousAction, fix white space in ActionNode of SequentialAction --- .../java/codebase/actions/SequentialAction.java | 17 ++++++----------- .../codebase/actions/SimultaneousAction.java | 17 +++++++++-------- .../main/java/codebase/actions/SleepAction.java | 2 +- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/TeamCode/src/main/java/codebase/actions/SequentialAction.java b/TeamCode/src/main/java/codebase/actions/SequentialAction.java index ad5b15e..74bff30 100644 --- a/TeamCode/src/main/java/codebase/actions/SequentialAction.java +++ b/TeamCode/src/main/java/codebase/actions/SequentialAction.java @@ -1,17 +1,14 @@ package codebase.actions; import java.util.Arrays; +import java.util.stream.Stream; public class SequentialAction implements Action { private ActionNode currentActionNode; - public SequentialAction(Action... actions) { - if (actions.length == 0) { - throw new IllegalArgumentException("You must pass at least one action"); - } - - this.currentActionNode = new ActionNode(actions); + public SequentialAction(Action first, Action... rest) { + this.currentActionNode = new ActionNode(Stream.concat(Stream.of(first), Arrays.stream(rest)).toArray(Action[]::new)); } @Override @@ -36,22 +33,20 @@ public void loop() { this.currentActionNode.action.loop(); } - - - } class ActionNode { public Action action; public ActionNode next; + public ActionNode(Action[] actions) { this.action = actions[0]; + if (actions.length == 1) { this.next = null; return; } + this.next = new ActionNode(Arrays.copyOfRange(actions, 1, actions.length)); } - - } \ No newline at end of file diff --git a/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java b/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java index 13eadfd..29b3090 100644 --- a/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java +++ b/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java @@ -8,13 +8,12 @@ public class SimultaneousAction implements Action { private final ArrayList actions; - public SimultaneousAction(Action... actions) { - if (actions.length == 0) { - throw new IllegalArgumentException("You must pass at least one action"); - } - + public SimultaneousAction(Action first, Action... rest) { this.actions = new ArrayList<>(); - for (Action action : actions) { + + this.actions.add(first); + + for (Action action : rest) { this.add(action, false); } } @@ -50,11 +49,13 @@ public void add(@NonNull Action action, boolean init) { for (Action a : actions) { if (a.getClass().getName().equals(actionName)) { - throw new IllegalArgumentException("You can't add multiple of the same Action to SimultaneousAction."); + throw new IllegalArgumentException("You can't add multiple of the same class of Action to SimultaneousAction."); } } actions.add(action); - if (init) action.init(); + if (init) { + action.init(); + } } } \ No newline at end of file diff --git a/TeamCode/src/main/java/codebase/actions/SleepAction.java b/TeamCode/src/main/java/codebase/actions/SleepAction.java index ecaad07..81185c8 100644 --- a/TeamCode/src/main/java/codebase/actions/SleepAction.java +++ b/TeamCode/src/main/java/codebase/actions/SleepAction.java @@ -1,6 +1,6 @@ package codebase.actions; -public class SleepAction implements Action{ +public class SleepAction implements Action { private final long ms;