|
| 1 | +package org.usfirst.frc.team69.util; |
| 2 | + |
| 3 | +import edu.wpi.first.wpilibj.command.Command; |
| 4 | +import edu.wpi.first.wpilibj.command.CommandGroup; |
| 5 | + |
| 6 | +/** |
| 7 | + * A builder class to create commands in autonomous. Currently, it wraps a |
| 8 | + * {@link CommandGroup}, but the implementation may change in future versions. |
| 9 | + * |
| 10 | + * To build a command, add child commands using {@link #sequential(Command)} |
| 11 | + * and {@link #parallel(Command)}, then get a command using {@link #build()}. |
| 12 | + * |
| 13 | + * @author James |
| 14 | + * |
| 15 | + */ |
| 16 | +public class CommandBuilder { |
| 17 | + private final CommandGroup m_cmdGroup; |
| 18 | + |
| 19 | + /** |
| 20 | + * Construct a new CommandBuilder |
| 21 | + */ |
| 22 | + public CommandBuilder() { |
| 23 | + m_cmdGroup = new CommandGroup(); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Construct a new CommandBuilder with a given name |
| 28 | + * @param name The name of the command to build |
| 29 | + */ |
| 30 | + public CommandBuilder(String name) { |
| 31 | + m_cmdGroup = new CommandGroup(name); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Add a command in sequence. Execution of the next command will not |
| 36 | + * happen until this one finishes. |
| 37 | + * @param command The command to add in sequence |
| 38 | + * @return This CommandBuilder object |
| 39 | + */ |
| 40 | + public CommandBuilder sequential(Command command) { |
| 41 | + m_cmdGroup.addSequential(command); |
| 42 | + return this; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Add a command in sequence. Execution of the next command will not |
| 47 | + * happen until this one finishes, or until the timeout expires |
| 48 | + * @param command The command to add in sequence |
| 49 | + * @param timeout The timeout, in seconds |
| 50 | + * @return This CommandBuilder object |
| 51 | + */ |
| 52 | + public CommandBuilder sequential(Command command, double timeout) { |
| 53 | + m_cmdGroup.addSequential(command, timeout); |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Add a command in parallel. Execution of the next command will begin |
| 59 | + * immediately, while this one continues to execute in parallel. |
| 60 | + * |
| 61 | + * Note that the command created will not end until all parallel commands |
| 62 | + * finish. |
| 63 | + * |
| 64 | + * @param command The command to add in parallel |
| 65 | + * @return This CommandBuilder object |
| 66 | + */ |
| 67 | + public CommandBuilder parallel(Command command) { |
| 68 | + m_cmdGroup.addParallel(command); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Add a command in parallel. Execution of the next command will begin |
| 74 | + * immediately, while this one continues to execute in parallel. The |
| 75 | + * command will be killed if the timeout expires. |
| 76 | + * |
| 77 | + * Note that the command created will not end until all parallel commands |
| 78 | + * finish. |
| 79 | + * |
| 80 | + * @param command The command to add in parallel |
| 81 | + * @param timeout The timeout, in seconds |
| 82 | + * @return This CommandBuilder object |
| 83 | + */ |
| 84 | + public CommandBuilder parallel(Command command, double timeout) { |
| 85 | + m_cmdGroup.addParallel(command, timeout); |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Build a command. Note that for simplicity, calling this method |
| 91 | + * multiple times will return the same {@link Command} object. This |
| 92 | + * may change in the future, to match the behavior of other objects |
| 93 | + * following the builder pattern. |
| 94 | + * @return A command created from this builder |
| 95 | + */ |
| 96 | + public Command build() { |
| 97 | + return m_cmdGroup; |
| 98 | + } |
| 99 | +} |
0 commit comments