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.wpilibj.command;
006
007/**
008 * This command will execute once, then finish immediately afterward.
009 *
010 * <p>Subclassing {@link InstantCommand} is shorthand for returning true from {@link Command
011 * isFinished}.
012 */
013public class InstantCommand extends Command {
014  private Runnable m_func;
015
016  public InstantCommand() {}
017
018  /**
019   * Creates a new {@link InstantCommand InstantCommand} with the given name.
020   *
021   * @param name the name for this command
022   */
023  public InstantCommand(String name) {
024    super(name);
025  }
026
027  /**
028   * Creates a new {@link InstantCommand InstantCommand} with the given requirement.
029   *
030   * @param subsystem the subsystem this command requires
031   */
032  public InstantCommand(Subsystem subsystem) {
033    super(subsystem);
034  }
035
036  /**
037   * Creates a new {@link InstantCommand InstantCommand} with the given name and requirement.
038   *
039   * @param name the name for this command
040   * @param subsystem the subsystem this command requires
041   */
042  public InstantCommand(String name, Subsystem subsystem) {
043    super(name, subsystem);
044  }
045
046  /**
047   * Creates a new {@link InstantCommand InstantCommand}.
048   *
049   * @param func the function to run when {@link Command#initialize() initialize()} is run
050   */
051  public InstantCommand(Runnable func) {
052    m_func = func;
053  }
054
055  /**
056   * Creates a new {@link InstantCommand InstantCommand}.
057   *
058   * @param name the name for this command
059   * @param func the function to run when {@link Command#initialize() initialize()} is run
060   */
061  public InstantCommand(String name, Runnable func) {
062    super(name);
063    m_func = func;
064  }
065
066  /**
067   * Creates a new {@link InstantCommand InstantCommand}.
068   *
069   * @param requirement the subsystem this command requires
070   * @param func the function to run when {@link Command#initialize() initialize()} is run
071   */
072  public InstantCommand(Subsystem requirement, Runnable func) {
073    super(requirement);
074    m_func = func;
075  }
076
077  /**
078   * Creates a new {@link InstantCommand InstantCommand}.
079   *
080   * @param name the name for this command
081   * @param requirement the subsystem this command requires
082   * @param func the function to run when {@link Command#initialize() initialize()} is run
083   */
084  public InstantCommand(String name, Subsystem requirement, Runnable func) {
085    super(name, requirement);
086    m_func = func;
087  }
088
089  @Override
090  protected boolean isFinished() {
091    return true;
092  }
093
094  /**
095   * Trigger the stored function.
096   *
097   * <p>Called just before this Command runs the first time.
098   */
099  @Override
100  protected void _initialize() {
101    super._initialize();
102    if (m_func != null) {
103      m_func.run();
104    }
105  }
106}