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.hal.util;
006
007/**
008 * This exception represents an error in which a lower limit was set as higher than an upper limit.
009 */
010@SuppressWarnings("serial")
011public class BoundaryException extends RuntimeException {
012  /**
013   * Create a new exception with the given message.
014   *
015   * @param message the message to attach to the exception
016   */
017  public BoundaryException(String message) {
018    super(message);
019  }
020
021  /**
022   * Make sure that the given value is between the upper and lower bounds, and throw an exception if
023   * they are not.
024   *
025   * @param value The value to check.
026   * @param lower The minimum acceptable value.
027   * @param upper The maximum acceptable value.
028   */
029  public static void assertWithinBounds(double value, double lower, double upper) {
030    if (value < lower || value > upper) {
031      throw new BoundaryException(
032          "Value must be between " + lower + " and " + upper + ", " + value + " given");
033    }
034  }
035
036  /**
037   * Returns the message for a boundary exception. Used to keep the message consistent across all
038   * boundary exceptions.
039   *
040   * @param value The given value
041   * @param lower The lower limit
042   * @param upper The upper limit
043   * @return the message for a boundary exception
044   */
045  public static String getMessage(double value, double lower, double upper) {
046    return "Value must be between " + lower + " and " + upper + ", " + value + " given";
047  }
048}