[commands] Add FinallyDo and HandleInterrupt decorators (#4412)

This commit is contained in:
Starlight220
2022-10-11 19:53:27 +03:00
committed by GitHub
parent 1497665f96
commit 89a3d00297
7 changed files with 251 additions and 0 deletions

View File

@@ -4,6 +4,9 @@
package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.wpilibj.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.util.function.BooleanConsumer;
import java.util.Set;
import java.util.function.BooleanSupplier;
@@ -348,6 +351,42 @@ public interface Command {
};
}
/**
* Decorates this command with a lambda to call on interrupt or end, following the command's
* inherent {@link #end(boolean)} method.
*
* @param end a lambda accepting a boolean parameter specifying whether the command was
* interrupted.
* @return the decorated command
*/
default WrapperCommand finallyDo(BooleanConsumer end) {
requireNonNullParam(end, "end", "Command.finallyDo()");
return new WrapperCommand(this) {
@Override
public void end(boolean interrupted) {
super.end(interrupted);
end.accept(interrupted);
}
};
}
/**
* Decorates this command with a lambda to call on interrupt, following the command's inherent
* {@link #end(boolean)} method.
*
* @param handler a lambda to run when the command is interrupted
* @return the decorated command
*/
default WrapperCommand handleInterrupt(Runnable handler) {
requireNonNullParam(handler, "handler", "Command.handleInterrupt()");
return finallyDo(
interrupted -> {
if (interrupted) {
handler.run();
}
});
}
/** Schedules this command. */
default void schedule() {
CommandScheduler.getInstance().schedule(this);