mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
[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:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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!")),
|
||||
|
||||
Reference in New Issue
Block a user