Java Number Range Create range(T val, T min, T max)

Here you can find the source of range(T val, T min, T max)

Description

Clips a number so it never goes above its max number or below its minimum number.

License

Open Source License

Parameter

Parameter Description
val the number being checked.
min the minimum value the number can be.
max the maximum value the number can be.

Return

itself if it does not exceed its range, else it will return its minimum or maximum number.

Declaration

public static <T extends Comparable<T>> T range(T val, T min, T max) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w w w.  ja  v a2s . c  o m*/
     * Clips a number so it never goes above its max number or below its minimum
     * number.
     * 
     * @param val
     *            the number being checked.
     * @param min
     *            the minimum value the number can be.
     * @param max
     *            the maximum value the number can be.
     * @return itself if it does not exceed its range, else it will return its
     *         minimum or maximum number.
     */
    public static <T extends Comparable<T>> T range(T val, T min, T max) {
        if (val.compareTo(min) < 0)
            return min;
        else if (val.compareTo(max) > 0)
            return max;
        else
            return val;
    }
}

Related

  1. range(long array[])
  2. range(long start, long end)
  3. range(String field, int start, int end)
  4. range(String input[], int beginIndex, int endIndex)
  5. range(String second, String first)
  6. RangeConvert(float start, float end, float newStart, float newEnd, float value)