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 static edu.wpi.first.wpilibj2.command.CommandGroupBase.registerGroupedCommands;
008import static edu.wpi.first.wpilibj2.command.CommandGroupBase.requireUngrouped;
009
010/**
011 * A command that runs another command in perpetuity, ignoring that command's end conditions. While
012 * this class does not extend {@link CommandGroupBase}, it is still considered a CommandGroup, as it
013 * allows one to compose another command within it; the command instances that are passed to it
014 * cannot be added to any other groups, or scheduled individually.
015 *
016 * <p>As a rule, CommandGroups require the union of the requirements of their component commands.
017 */
018public class PerpetualCommand extends CommandBase {
019  protected final Command m_command;
020
021  /**
022   * Creates a new PerpetualCommand. Will run another command in perpetuity, ignoring that command's
023   * end conditions, unless this command itself is interrupted.
024   *
025   * @param command the command to run perpetually
026   */
027  public PerpetualCommand(Command command) {
028    requireUngrouped(command);
029    registerGroupedCommands(command);
030    m_command = command;
031    m_requirements.addAll(command.getRequirements());
032  }
033
034  @Override
035  public void initialize() {
036    m_command.initialize();
037  }
038
039  @Override
040  public void execute() {
041    m_command.execute();
042  }
043
044  @Override
045  public void end(boolean interrupted) {
046    m_command.end(interrupted);
047  }
048
049  @Override
050  public boolean runsWhenDisabled() {
051    return m_command.runsWhenDisabled();
052  }
053
054  @Override
055  public PerpetualCommand perpetually() {
056    return this;
057  }
058}