[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");