[commands] Make Java SelectCommand generic (#5849)

This allows users to use any type of map and selector without needing to explicitly match
Map<Object, Command>
This commit is contained in:
Ryan Blue
2023-10-30 14:09:14 -04:00
committed by GitHub
parent 9eecf2a456
commit a4a8ad9c75
5 changed files with 23 additions and 19 deletions

View File

@@ -144,13 +144,14 @@ public final class Commands {
/**
* 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);
}
/**

View File

@@ -19,10 +19,12 @@ import java.util.function.Supplier;
* 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;
@@ -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");

View File

@@ -141,8 +141,8 @@ class RobotDisabledCommandTest extends CommandTestBase {
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);

View File

@@ -13,7 +13,8 @@ import java.util.HashMap;
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()) {
@@ -25,8 +26,8 @@ class SelectCommandTest extends CommandTestBase implements MultiCompositionTestB
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),
@@ -61,8 +62,8 @@ class SelectCommandTest extends CommandTestBase implements MultiCompositionTestB
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),
@@ -88,8 +89,8 @@ class SelectCommandTest extends CommandTestBase implements MultiCompositionTestB
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),
@@ -108,11 +109,11 @@ class SelectCommandTest extends CommandTestBase implements MultiCompositionTestB
}
@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);
}
}

View File

@@ -36,7 +36,7 @@ public class RobotContainer {
// 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!")),