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 edu.wpi.first.wpilibj.Timer;
010import java.util.function.BooleanSupplier;
011
012/**
013 * A command that does nothing but ends after a specified match time or condition. Useful for
014 * CommandGroups.
015 */
016public class WaitUntilCommand extends CommandBase {
017  private final BooleanSupplier m_condition;
018
019  /**
020   * Creates a new WaitUntilCommand that ends after a given condition becomes true.
021   *
022   * @param condition the condition to determine when to end
023   */
024  public WaitUntilCommand(BooleanSupplier condition) {
025    m_condition = requireNonNullParam(condition, "condition", "WaitUntilCommand");
026  }
027
028  /**
029   * Creates a new WaitUntilCommand that ends after a given match time.
030   *
031   * <p>NOTE: The match timer used for this command is UNOFFICIAL. Using this command does NOT
032   * guarantee that the time at which the action is performed will be judged to be legal by the
033   * referees. When in doubt, add a safety factor or time the action manually.
034   *
035   * @param time the match time after which to end, in seconds
036   */
037  public WaitUntilCommand(double time) {
038    this(() -> Timer.getMatchTime() - time > 0);
039  }
040
041  @Override
042  public boolean isFinished() {
043    return m_condition.getAsBoolean();
044  }
045
046  @Override
047  public boolean runsWhenDisabled() {
048    return true;
049  }
050}