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
009/**
010 * A Command that runs instantly; it will initialize, execute once, and end on the same iteration of
011 * the scheduler. Users can either pass in a Runnable and a set of requirements, or else subclass
012 * this command if desired.
013 */
014public class InstantCommand extends CommandBase {
015  private final Runnable m_toRun;
016
017  /**
018   * Creates a new InstantCommand that runs the given Runnable with the given requirements.
019   *
020   * @param toRun the Runnable to run
021   * @param requirements the subsystems required by this command
022   */
023  public InstantCommand(Runnable toRun, Subsystem... requirements) {
024    m_toRun = requireNonNullParam(toRun, "toRun", "InstantCommand");
025
026    addRequirements(requirements);
027  }
028
029  /**
030   * Creates a new InstantCommand with a Runnable that does nothing. Useful only as a no-arg
031   * constructor to call implicitly from subclass constructors.
032   */
033  public InstantCommand() {
034    m_toRun = () -> {};
035  }
036
037  @Override
038  public void initialize() {
039    m_toRun.run();
040  }
041
042  @Override
043  public final boolean isFinished() {
044    return true;
045  }
046}