Skip to content

Commit

Permalink
[commands] Make Java SelectCommand generic (#5849)
Browse files Browse the repository at this point in the history
This allows users to use any type of map and selector without needing to explicitly match
Map<Object, Command>
  • Loading branch information
rzblue committed Oct 30, 2023
1 parent 9eecf2a commit a4a8ad9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ public static Command either(Command onTrue, Command onFalse, BooleanSupplier se
/**
* Runs one of several commands, based on the selector function.
*
* @param <K> The type of key used to select the command
* @param selector the selector function
* @param commands map of commands to select from
* @return the command
* @see SelectCommand
*/
public static Command select(Map<Object, Command> commands, Supplier<Object> selector) {
return new SelectCommand(commands, selector);
public static <K> Command select(Map<K, Command> commands, Supplier<? extends K> selector) {
return new SelectCommand<>(commands, selector);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
* subsystems its components require.
*
* <p>This class is provided by the NewCommands VendorDep
*
* @param <K> The type of key used to select the command
*/
public class SelectCommand extends Command {
private final Map<Object, Command> m_commands;
private final Supplier<Object> m_selector;
public class SelectCommand<K> extends Command {
private final Map<K, Command> m_commands;
private final Supplier<? extends K> m_selector;
private Command m_selectedCommand;
private boolean m_runsWhenDisabled = true;
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
Expand All @@ -36,7 +38,7 @@ public class SelectCommand extends Command {
* @param commands the map of commands to choose from
* @param selector the selector to determine which command to run
*/
public SelectCommand(Map<Object, Command> commands, Supplier<Object> selector) {
public SelectCommand(Map<K, Command> commands, Supplier<? extends K> selector) {
m_commands = requireNonNullParam(commands, "commands", "SelectCommand");
m_selector = requireNonNullParam(selector, "selector", "SelectCommand");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ void selectRunWhenDisabledTest() {
MockCommandHolder command4Holder = new MockCommandHolder(false);
Command command4 = command4Holder.getMock();

Command runWhenDisabled = new SelectCommand(Map.of(1, command1, 2, command2), () -> 1);
Command dontRunWhenDisabled = new SelectCommand(Map.of(1, command3, 2, command4), () -> 1);
Command runWhenDisabled = new SelectCommand<>(Map.of(1, command1, 2, command2), () -> 1);
Command dontRunWhenDisabled = new SelectCommand<>(Map.of(1, command3, 2, command4), () -> 1);

try (CommandScheduler scheduler = new CommandScheduler()) {
scheduler.schedule(runWhenDisabled, dontRunWhenDisabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import java.util.Map;
import org.junit.jupiter.api.Test;

class SelectCommandTest extends CommandTestBase implements MultiCompositionTestBase<SelectCommand> {
class SelectCommandTest extends CommandTestBase
implements MultiCompositionTestBase<SelectCommand<Integer>> {
@Test
void selectCommandTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
Expand All @@ -25,8 +26,8 @@ void selectCommandTest() {
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();

SelectCommand selectCommand =
new SelectCommand(
SelectCommand<String> selectCommand =
new SelectCommand<>(
Map.ofEntries(
Map.entry("one", command1),
Map.entry("two", command2),
Expand Down Expand Up @@ -61,8 +62,8 @@ void selectCommandInvalidKeyTest() {
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();

SelectCommand selectCommand =
new SelectCommand(
SelectCommand<String> selectCommand =
new SelectCommand<>(
Map.ofEntries(
Map.entry("one", command1),
Map.entry("two", command2),
Expand All @@ -88,8 +89,8 @@ void selectCommandRequirementTest() {
MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4);
Command command3 = command3Holder.getMock();

SelectCommand selectCommand =
new SelectCommand(
SelectCommand<String> selectCommand =
new SelectCommand<>(
Map.ofEntries(
Map.entry("one", command1),
Map.entry("two", command2),
Expand All @@ -108,11 +109,11 @@ void selectCommandRequirementTest() {
}

@Override
public SelectCommand compose(Command... members) {
var map = new HashMap<Object, Command>();
public SelectCommand<Integer> compose(Command... members) {
var map = new HashMap<Integer, Command>();
for (int i = 0; i < members.length; i++) {
map.put(i, members[i]);
}
return new SelectCommand(map, () -> 0);
return new SelectCommand<>(map, () -> 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private CommandSelector select() {
// selector does not have to be an enum; it could be any desired type (string, integer,
// boolean, double...)
private final Command m_exampleSelectCommand =
new SelectCommand(
new SelectCommand<>(
// Maps selector values to commands
Map.ofEntries(
Map.entry(CommandSelector.ONE, new PrintCommand("Command one was selected!")),
Expand Down

0 comments on commit a4a8ad9

Please sign in to comment.