[commands] Refactor ProxyScheduleCommand, SelectCommand into ProxyCommand (#4534)

This commit is contained in:
Starlight220
2022-11-29 00:43:10 +02:00
committed by GitHub
parent e82cd5147b
commit 70080457d5
16 changed files with 214 additions and 43 deletions

View File

@@ -286,14 +286,14 @@ public interface Command {
}
/**
* Decorates this command to run "by proxy" by wrapping it in a {@link ProxyScheduleCommand}. This
* is useful for "forking off" from command groups when the user does not wish to extend the
* Decorates this command to run "by proxy" by wrapping it in a {@link ProxyCommand}. This is
* useful for "forking off" from command groups when the user does not wish to extend the
* command's requirements to the entire command group.
*
* @return the decorated command
*/
default ProxyScheduleCommand asProxy() {
return new ProxyScheduleCommand(this);
default ProxyCommand asProxy() {
return new ProxyCommand(this);
}
/**

View File

@@ -0,0 +1,76 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.wpilibj.util.ErrorMessages.requireNonNullParam;
import java.util.function.Supplier;
/**
* Schedules the given command when this command is initialized, and ends when it ends. Useful for
* forking off from CommandGroups. If this command is interrupted, it will cancel the command.
*
* <p>This class is provided by the NewCommands VendorDep
*/
public class ProxyCommand extends CommandBase {
private final Supplier<Command> m_supplier;
private Command m_command;
/**
* Creates a new ProxyCommand that schedules the supplied command when initialized, and ends when
* it is no longer scheduled. Useful for lazily creating commands at runtime.
*
* @param supplier the command supplier
*/
public ProxyCommand(Supplier<Command> supplier) {
m_supplier = requireNonNullParam(supplier, "supplier", "ProxyCommand");
}
/**
* Creates a new ProxyCommand that schedules the given command when initialized, and ends when it
* is no longer scheduled.
*
* @param command the command to run by proxy
*/
public ProxyCommand(Command command) {
this(() -> command);
}
@Override
public void initialize() {
m_command = m_supplier.get();
m_command.schedule();
}
@Override
public void end(boolean interrupted) {
if (interrupted) {
m_command.cancel();
}
m_command = null;
}
@Override
public void execute() {}
@Override
public boolean isFinished() {
// because we're between `initialize` and `end`, `m_command` is necessarily not null
// but if called otherwise and m_command is null,
// it's UB, so we can do whatever we want -- like return true.
return m_command == null || !m_command.isScheduled();
}
/**
* Whether the given command should run when the robot is disabled. Override to return true if the
* command should run when disabled.
*
* @return true. Otherwise, this proxy would cancel commands that do run when disabled.
*/
@Override
public boolean runsWhenDisabled() {
return true;
}
}

View File

@@ -22,7 +22,10 @@ public class ProxyScheduleCommand extends CommandBase {
* when they are all no longer scheduled.
*
* @param toSchedule the commands to schedule
* @deprecated Replace with {@link ProxyCommand}, composing multiple of them in a {@link
* ParallelRaceGroup} if needed.
*/
@Deprecated
public ProxyScheduleCommand(Command... toSchedule) {
m_toSchedule = Set.of(toSchedule);
}

View File

@@ -63,7 +63,9 @@ public class SelectCommand extends CommandBase {
* Creates a new selectcommand.
*
* @param toRun a supplier providing the command to run
* @deprecated Replace with {@link ProxyCommand}
*/
@Deprecated
public SelectCommand(Supplier<Command> toRun) {
m_commands = null;
m_selector = null;