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.wpilibj.util.ErrorMessages.requireNonNullParam;
008
009import java.util.function.BooleanSupplier;
010
011/**
012 * A command that runs a Runnable continuously. Has no end condition as-is; either subclass it or
013 * use {@link Command#withTimeout(double)} or {@link Command#until(BooleanSupplier)} to give it one.
014 * If you only wish to execute a Runnable once, use {@link InstantCommand}.
015 *
016 * <p>This class is provided by the NewCommands VendorDep
017 */
018public class RunCommand extends CommandBase {
019  protected final Runnable m_toRun;
020
021  /**
022   * Creates a new RunCommand. The Runnable will be run continuously until the command ends. Does
023   * not run when disabled.
024   *
025   * @param toRun the Runnable to run
026   * @param requirements the subsystems to require
027   */
028  public RunCommand(Runnable toRun, Subsystem... requirements) {
029    m_toRun = requireNonNullParam(toRun, "toRun", "RunCommand");
030    addRequirements(requirements);
031  }
032
033  @Override
034  public void execute() {
035    m_toRun.run();
036  }
037}