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