001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj2.command;
006
007import java.util.Set;
008
009/**
010 * A class used internally to wrap commands while overriding a specific method; all other methods
011 * will call through to the wrapped command.
012 *
013 * <p>The rules for command compositions apply: command instances that are passed to it cannot be
014 * added to any other composition or scheduled individually, and the composition requires all
015 * subsystems its components require.
016 */
017public abstract class WrapperCommand extends Command {
018  protected final Command m_command;
019
020  /**
021   * Wrap a command.
022   *
023   * @param command the command being wrapped. Trying to directly schedule this command or add it to
024   *     a composition will throw an exception.
025   */
026  @SuppressWarnings("this-escape")
027  protected WrapperCommand(Command command) {
028    CommandScheduler.getInstance().registerComposedCommands(command);
029    m_command = command;
030    // copy the wrapped command's name
031    setName(command.getName());
032  }
033
034  /** The initial subroutine of a command. Called once when the command is initially scheduled. */
035  @Override
036  public void initialize() {
037    m_command.initialize();
038  }
039
040  /** The main body of a command. Called repeatedly while the command is scheduled. */
041  @Override
042  public void execute() {
043    m_command.execute();
044  }
045
046  /**
047   * The action to take when the command ends. Called when either the command finishes normally, or
048   * when it interrupted/canceled.
049   *
050   * <p>Do not schedule commands here that share requirements with this command. Use {@link
051   * #andThen(Command...)} instead.
052   *
053   * @param interrupted whether the command was interrupted/canceled
054   */
055  @Override
056  public void end(boolean interrupted) {
057    m_command.end(interrupted);
058  }
059
060  /**
061   * Whether the command has finished. Once a command finishes, the scheduler will call its end()
062   * method and un-schedule it.
063   *
064   * @return whether the command has finished.
065   */
066  @Override
067  public boolean isFinished() {
068    return m_command.isFinished();
069  }
070
071  /**
072   * Specifies the set of subsystems used by this command. Two commands cannot use the same
073   * subsystem at the same time. If the command is scheduled as interruptible and another command is
074   * scheduled that shares a requirement, the command will be interrupted. Else, the command will
075   * not be scheduled. If no subsystems are required, return an empty set.
076   *
077   * <p>Note: it is recommended that user implementations contain the requirements as a field, and
078   * return that field here, rather than allocating a new set every time this is called.
079   *
080   * @return the set of subsystems that are required
081   */
082  @Override
083  public Set<Subsystem> getRequirements() {
084    return m_command.getRequirements();
085  }
086
087  /**
088   * Whether the given command should run when the robot is disabled. Override to return true if the
089   * command should run when disabled.
090   *
091   * @return whether the command should run when the robot is disabled
092   */
093  @Override
094  public boolean runsWhenDisabled() {
095    return m_command.runsWhenDisabled();
096  }
097
098  @Override
099  public InterruptionBehavior getInterruptionBehavior() {
100    return m_command.getInterruptionBehavior();
101  }
102}