Java Number Range Check inRange(final int value, final int min, final int max, final String fieldName)

Here you can find the source of inRange(final int value, final int min, final int max, final String fieldName)

Description

Verifies the value and fails if the given value is outside the given range.

License

Apache License

Parameter

Parameter Description
value the value to be verified
min the minimum value (inclusive)
max the maximum value (inclusive)
fieldName the fields name (which is used a part of the exception message)

Exception

Parameter Description
IllegalArgumentException if the given value is lower than min or greater than max

Return

the given value

Declaration

public static int inRange(final int value, final int min, final int max, final String fieldName)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*/*from w  ww  . j  av  a  2 s.  c  om*/
 * #%L
 * JavaCreed Secure Properties Encoder
 * %%
 * Copyright (C) 2012 - 2015 Java Creed
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

public class Main {
    /**
     * Verifies the value and fails if the given value is outside the given range.
     *
     * @param value
     *          the value to be verified
     * @param min
     *          the minimum value (inclusive)
     * @param max
     *          the maximum value (inclusive)
     * @param fieldName
     *          the fields name (which is used a part of the exception message)
     * @return the given value
     * @throws IllegalArgumentException
     *           if the given value is lower than min or greater than max
     */
    public static int inRange(final int value, final int min, final int max, final String fieldName)
            throws IllegalArgumentException {
        if (value < min || value > max) {
            throw new IllegalArgumentException(
                    "The " + fieldName + " value of " + value + " is out of range: [" + min + "," + max + "].");
        }

        return value;
    }
}

Related

  1. inRange(double x, double y, double z, double sx, double sy, double sz, double r)
  2. inRange(E element, E min, E max)
  3. inRange(final float testNum, final float min, final float max)
  4. inRange(final int id, final int min, final int max)
  5. inRange(final int value, final int min, final int max)
  6. inRange(float n, float min, float max)
  7. inrange(int actual, int leftRange, int rightRange)
  8. inRange(int check, int range)
  9. inRange(int checkValue, int min, int max)