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