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 * Schedules the given commands when this command is initialized, and ends when all the commands are
011 * no longer scheduled. Useful for forking off from CommandGroups. If this command is interrupted,
012 * it will cancel all the commands.
013 *
014 * <p>This class is provided by the NewCommands VendorDep
015 */
016public class ProxyScheduleCommand extends CommandBase {
017  private final Set<Command> m_toSchedule;
018  private boolean m_finished;
019
020  /**
021   * Creates a new ProxyScheduleCommand that schedules the given commands when initialized, and ends
022   * when they are all no longer scheduled.
023   *
024   * @param toSchedule the commands to schedule
025   * @deprecated Replace with {@link ProxyCommand}, composing multiple of them in a {@link
026   *     ParallelRaceGroup} if needed.
027   */
028  @Deprecated
029  public ProxyScheduleCommand(Command... toSchedule) {
030    m_toSchedule = Set.of(toSchedule);
031  }
032
033  @Override
034  public void initialize() {
035    for (Command command : m_toSchedule) {
036      command.schedule();
037    }
038  }
039
040  @Override
041  public void end(boolean interrupted) {
042    if (interrupted) {
043      for (Command command : m_toSchedule) {
044        command.cancel();
045      }
046    }
047  }
048
049  @Override
050  public void execute() {
051    m_finished = true;
052    for (Command command : m_toSchedule) {
053      m_finished &= !command.isScheduled();
054    }
055  }
056
057  @Override
058  public boolean isFinished() {
059    return m_finished;
060  }
061}