001/*----------------------------------------------------------------------------*/
002/* Copyright (c) 2008-2018 FIRST. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj.util;
009
010/**
011 * This exception represents an error in which a lower limit was set as higher than an upper limit.
012 */
013public class BoundaryException extends RuntimeException {
014  /**
015   * Create a new exception with the given message.
016   *
017   * @param message the message to attach to the exception
018   */
019  public BoundaryException(String message) {
020    super(message);
021  }
022
023  /**
024   * Make sure that the given value is between the upper and lower bounds, and throw an exception if
025   * they are not.
026   *
027   * @param value The value to check.
028   * @param lower The minimum acceptable value.
029   * @param upper The maximum acceptable value.
030   */
031  public static void assertWithinBounds(double value, double lower, double upper) {
032    if (value < lower || value > upper) {
033      throw new BoundaryException("Value must be between " + lower + " and " + upper + ", " + value
034          + " given");
035    }
036  }
037
038  /**
039   * Returns the message for a boundary exception. Used to keep the message consistent across all
040   * boundary exceptions.
041   *
042   * @param value The given value
043   * @param lower The lower limit
044   * @param upper The upper limit
045   * @return the message for a boundary exception
046   */
047  public static String getMessage(double value, double lower, double upper) {
048    return "Value must be between " + lower + " and " + upper + ", " + value + " given";
049  }
050}