[commands] Add withDeadline modifier (#7299)

Co-authored-by: Ryan Blue <ryanzblue@gmail.com>
This commit is contained in:
Daniel Chen
2024-12-13 20:30:02 -05:00
committed by GitHub
parent 5e3dba672a
commit 68285dae77
7 changed files with 153 additions and 0 deletions

View File

@@ -287,6 +287,24 @@ public abstract class Command implements Sendable {
return group;
}
/**
* Creates a new command that runs this command and the deadline in parallel, finishing (and
* interrupting this command) when the deadline finishes.
*
* <p>Note: This decorator works by adding this command to a composition. The command the
* decorator was called on cannot be scheduled independently or be added to a different
* composition (namely, decorators), unless it is manually cleared from the list of composed
* commands with {@link CommandScheduler#removeComposedCommand(Command)}. The command composition
* returned from this method can be further decorated without issue.
*
* @param deadline the deadline of the command group
* @return the decorated command
* @see Command#deadlineFor
*/
public ParallelDeadlineGroup withDeadline(Command deadline) {
return new ParallelDeadlineGroup(deadline, this);
}
/**
* Decorates this command with a set of commands to run parallel to it, ending when the calling
* command ends and interrupting all the others. Often more convenient/less-verbose than
@@ -321,6 +339,7 @@ public abstract class Command implements Sendable {
* @param parallel the commands to run in parallel. Note the parallel commands will be interrupted
* when the deadline command ends
* @return the decorated command
* @see Command#withDeadline
*/
public ParallelDeadlineGroup deadlineFor(Command... parallel) {
return new ParallelDeadlineGroup(this, parallel);