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