mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user