001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2017. 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  /**
016   * Create a new exception with the given message.
017   *
018   * @param message the message to attach to the exception
019   */
020  public BoundaryException(String message) {
021    super(message);
022  }
023
024  /**
025   * Make sure that the given value is between the upper and lower bounds, and throw an exception if
026   * they are not.
027   *
028   * @param value The value to check.
029   * @param lower The minimum acceptable value.
030   * @param upper The maximum acceptable value.
031   */
032  public static void assertWithinBounds(double value, double lower, double upper) {
033    if (value < lower || value > upper) {
034      throw new BoundaryException("Value must be between " + lower + " and " + upper + ", " + value
035          + " given");
036    }
037  }
038
039  /**
040   * Returns the message for a boundary exception. Used to keep the message consistent across all
041   * boundary exceptions.
042   *
043   * @param value The given value
044   * @param lower The lower limit
045   * @param upper The upper limit
046   * @return the message for a boundary exception
047   */
048  public static String getMessage(double value, double lower, double upper) {
049    return "Value must be between " + lower + " and " + upper + ", " + value + " given";
050  }
051}