Java Number Range Check rangeCheck(final int value, final int min, final int max)

Here you can find the source of rangeCheck(final int value, final int min, final int max)

Description

range Check

License

Apache License

Declaration

public static int rangeCheck(final int value, final int min, final int max) 

Method Source Code

//package com.java2s;
/**//from   w w w  .  j  ava2s. c o m
 * Copyright 2005-2012 Akiban Technologies, Inc.
 * 
 * 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.
 */

public class Main {
    final static String SPACES = "                                                                    ";

    public static int rangeCheck(final int value, final int min, final int max) {
        if (value >= min && value <= max) {
            return value;
        }
        if (min == Integer.MIN_VALUE) {
            throw new IllegalArgumentException(
                    String.format("Value must be less than or equals to %,d: %,d", max, value));
        }
        if (max == Integer.MAX_VALUE) {
            throw new IllegalArgumentException(
                    String.format("Value must be greater than or equal to %,d: %,d", min, value));
        } else {
            throw new IllegalArgumentException(
                    String.format("Value must be between %d and %d, inclusive: ", min, max, value));
        }
    }

    public static long rangeCheck(final long value, final long min, final long max) {
        if (value >= min && value <= max) {
            return value;
        }
        if (min == Long.MIN_VALUE) {
            throw new IllegalArgumentException(
                    String.format("Value must be less than or equals to %,d: %,d", max, value));
        }
        if (max == Long.MAX_VALUE) {
            throw new IllegalArgumentException(
                    String.format("Value must be greater than or equal to %,d: %,d", min, value));
        } else {
            throw new IllegalArgumentException(
                    String.format("Value must be between %d and %d, inclusive: ", min, max, value));
        }
    }

    public static float rangeCheck(final float value, final float min, final float max) {
        if (value >= min && value <= max) {
            return value;
        }
        if (min == Float.MIN_VALUE) {
            throw new IllegalArgumentException(
                    String.format("Value must be less than or equals to %,f: %,f", max, value));
        }
        if (max == Float.MAX_VALUE) {
            throw new IllegalArgumentException(
                    String.format("Value must be greater than or equal to %,f: %,f", min, value));
        } else {
            throw new IllegalArgumentException(
                    String.format("Value must be between %f and %f, inclusive: ", min, max, value));
        }
    }

    public static String format(final String s, final int width, final boolean right) {
        final int pad = width - s.length();
        if (pad < 0)
            return s.substring(0, width - 1) + "&";
        if (pad == 0)
            return s;
        if (right)
            return SPACES.substring(0, pad) + s;
        else
            return s + SPACES.substring(0, pad);
    }

    public static String format(final int i) {
        return format(i, 10);
    }

    public static String format(final long i) {
        return format(i, 22);
    }

    public static String format(final long i, final int width) {
        return format(Long.toString(i), width, true);
    }

    public static String toString(final Object object) {
        return object == null ? null : object.toString();
    }
}

Related

  1. inRange(Integer x1, Integer y1, Integer x2, Integer y2)
  2. inRange(long value, long lowerBound, long upperBound)
  3. inrange2(double value, double min, double max)
  4. rangeCheck(double value)
  5. rangeCheck(final C start, final C end, final C step)
  6. rangeCheck(int arrayLen, int fromIndex, int toIndex)
  7. rangeCheck(int arrayLength, int offset, int length)
  8. rangeCheck(int index, int size)
  9. rangeCheck(int length, int fromIndex, int toIndex)