edu.wpi.first.wpilibj.command

## Class ConditionalCommand

- java.lang.Object
- - edu.wpi.first.wpilibj.command.Command
  - - edu.wpi.first.wpilibj.command.ConditionalCommand

- All Implemented Interfaces:
    NamedSendable, Sendable

      public abstract class ConditionalCommand
      extends Command

  A `ConditionalCommand` is a `Command` that starts one of two commands.

  A `ConditionalCommand` uses m_condition to determine whether it should run m_onTrue or m_onFalse.

  A `ConditionalCommand` adds the proper `Command` to the `Scheduler` during `Command.initialize()` and then `isFinished()` will return true once that `Command` has finished executing.

  If no `Command` is specified for m_onFalse, the occurrence of that condition will be a no-op.

  See Also:
    `Command`, `Scheduler`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ConditionalCommand(Command onTrue)` Creates a new ConditionalCommand with given onTrue and onFalse Commands.
    `ConditionalCommand(Command onTrue, Command onFalse)` Creates a new ConditionalCommand with given onTrue and onFalse Commands.
    `ConditionalCommand(java.lang.String name, Command onTrue)` Creates a new ConditionalCommand with given name and onTrue and onFalse Commands.
    `ConditionalCommand(java.lang.String name, Command onTrue, Command onFalse)` Creates a new ConditionalCommand with given name and onTrue and onFalse Commands.

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `_cancel()` This works like cancel(), except that it doesn't throw an exception if it is a part of a command group.
    `protected void` | `_initialize()` Calls `condition()` and runs the proper command.
    `protected abstract boolean` | `condition()` The Condition to test to determine which Command to run.
    `protected void` | `interrupted()` Called when the command ends because somebody called `cancel()` or another command shared the same requirements as this one, and booted it out.
    `protected boolean` | `isFinished()` Returns whether this command is finished.

    - ### Methods inherited from class edu.wpi.first.wpilibj.command.Command

      `cancel, clearRequirements, doesRequire, end, execute, getGroup, getName, getSmartDashboardType, initialize, initTable, isCanceled, isInterruptible, isRunning, isTimedOut, requires, setInterruptible, setRunWhenDisabled, setTimeout, start, timeSinceInitialized, toString, willRunWhenDisabled`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### ConditionalCommand

          public ConditionalCommand(Command onTrue)

      Creates a new ConditionalCommand with given onTrue and onFalse Commands.

      Users of this constructor should also override condition().

      Parameters:
        `onTrue` - The Command to execute if `condition()` returns true

    - #### ConditionalCommand

          public ConditionalCommand(Command onTrue,
                                    Command onFalse)

      Creates a new ConditionalCommand with given onTrue and onFalse Commands.

      Users of this constructor should also override condition().

      Parameters:
        `onTrue` - The Command to execute if `condition()` returns true
        `onFalse` - The Command to execute if `condition()` returns false

    - #### ConditionalCommand

          public ConditionalCommand(java.lang.String name,
                                    Command onTrue)

      Creates a new ConditionalCommand with given name and onTrue and onFalse Commands.

      Users of this constructor should also override condition().

      Parameters:
        `name` - the name for this command group
        `onTrue` - The Command to execute if `condition()` returns true

    - #### ConditionalCommand

          public ConditionalCommand(java.lang.String name,
                                    Command onTrue,
                                    Command onFalse)

      Creates a new ConditionalCommand with given name and onTrue and onFalse Commands.

      Users of this constructor should also override condition().

      Parameters:
        `name` - the name for this command group
        `onTrue` - The Command to execute if `condition()` returns true
        `onFalse` - The Command to execute if `condition()` returns false

  - ### Method Detail

    - #### condition

          protected abstract boolean condition()

      The Condition to test to determine which Command to run.
      Returns:
        true if m_onTrue should be run or false if m_onFalse should be run.

    - #### _initialize

          protected void _initialize()

      Calls `condition()` and runs the proper command.

    - #### _cancel

          protected void _cancel()

      Description copied from class: `Command`
      This works like cancel(), except that it doesn't throw an exception if it is a part of a command group. Should only be called by the parent command group.

    - #### isFinished

          protected boolean isFinished()

      Description copied from class: `Command`
      Returns whether this command is finished. If it is, then the command will be removed and `end()` will be called.

      It may be useful for a team to reference the `isTimedOut()` method for time-sensitive commands.

      Returning false will result in the command never ending automatically. It may still be cancelled manually or interrupted by another command. Returning true will result in the command executing once and finishing immediately. We recommend using `InstantCommand` for this.

      Specified by:
        `isFinished` in class `Command`
      Returns:
        whether this command is finished.
      See Also:
        `isTimedOut()`

    - #### interrupted

          protected void interrupted()

      Description copied from class: `Command`
      Called when the command ends because somebody called `cancel()` or another command shared the same requirements as this one, and booted it out.

      This is where you may want to wrap up loose ends, like shutting off a motor that was being used in the command.

      Generally, it is useful to simply call the `end()` method within this method, as done here.

      Overrides:
        `interrupted` in class `Command`