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 CommandBase {
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  protected WrapperCommand(Command command) {
027    CommandScheduler.getInstance().registerComposedCommands(command);
028    m_command = command;
029    // copy the wrapped command's name
030    setName(command.getName());
031  }
032
033  /** The initial subroutine of a command. Called once when the command is initially scheduled. */
034  @Override
035  public void initialize() {
036    m_command.initialize();
037  }
038
039  /** The main body of a command. Called repeatedly while the command is scheduled. */
040  @Override
041  public void execute() {
042    m_command.execute();
043  }
044
045  /**
046   * The action to take when the command ends. Called when either the command finishes normally, or
047   * when it interrupted/canceled.
048   *
049   * <p>Do not schedule commands here that share requirements with this command. Use {@link
050   * #andThen(Command...)} instead.
051   *
052   * @param interrupted whether the command was interrupted/canceled
053   */
054  @Override
055  public void end(boolean interrupted) {
056    m_command.end(interrupted);
057  }
058
059  /**
060   * Whether the command has finished. Once a command finishes, the scheduler will call its end()
061   * method and un-schedule it.
062   *
063   * @return whether the command has finished.
064   */
065  @Override
066  public boolean isFinished() {
067    return m_command.isFinished();
068  }
069
070  /**
071   * Specifies the set of subsystems used by this command. Two commands cannot use the same
072   * subsystem at the same time. If the command is scheduled as interruptible and another command is
073   * scheduled that shares a requirement, the command will be interrupted. Else, the command will
074   * not be scheduled. If no subsystems are required, return an empty set.
075   *
076   * <p>Note: it is recommended that user implementations contain the requirements as a field, and
077   * return that field here, rather than allocating a new set every time this is called.
078   *
079   * @return the set of subsystems that are required
080   */
081  @Override
082  public Set<Subsystem> getRequirements() {
083    return m_command.getRequirements();
084  }
085
086  /**
087   * Whether the given command should run when the robot is disabled. Override to return true if the
088   * command should run when disabled.
089   *
090   * @return whether the command should run when the robot is disabled
091   */
092  @Override
093  public boolean runsWhenDisabled() {
094    return m_command.runsWhenDisabled();
095  }
096
097  @Override
098  public InterruptionBehavior getInterruptionBehavior() {
099    return m_command.getInterruptionBehavior();
100  }
101}