Java Integer Clamp clamp(int value, int a, int b)

Here you can find the source of clamp(int value, int a, int b)

Description

Returns the passed in value if it resides within the specified range (inclusive).

License

Open Source License

Parameter

Parameter Description
value The value to be compared against the range.
a First boundary range value.
b Second boundary range value.

Return

The passed in value if it is within the range, otherwise the closest boundary value.

Declaration

public static int clamp(int value, int a, int b) 

Method Source Code

//package com.java2s;
// Use of this source code is governed by a BSD-style license that can be

public class Main {
    /**/*  ww  w  . j  a  va2  s .c o  m*/
     * Returns the passed in value if it resides within the specified range (inclusive).  If not,
     * it will return the closest boundary from the range.  The ordering of the boundary values does
     * not matter.
     *
     * @param value The value to be compared against the range.
     * @param a First boundary range value.
     * @param b Second boundary range value.
     * @return The passed in value if it is within the range, otherwise the closest boundary value.
     */
    public static int clamp(int value, int a, int b) {
        int min = (a > b) ? b : a;
        int max = (a > b) ? a : b;
        if (value < min)
            value = min;
        else if (value > max)
            value = max;
        return value;
    }

    /**
     * Returns the passed in value if it resides within the specified range (inclusive).  If not,
     * it will return the closest boundary from the range.  The ordering of the boundary values does
     * not matter.
     *
     * @param value The value to be compared against the range.
     * @param a First boundary range value.
     * @param b Second boundary range value.
     * @return The passed in value if it is within the range, otherwise the closest boundary value.
     */
    public static long clamp(long value, long a, long b) {
        long min = (a > b) ? b : a;
        long max = (a > b) ? a : b;
        if (value < min)
            value = min;
        else if (value > max)
            value = max;
        return value;
    }

    /**
     * Returns the passed in value if it resides within the specified range (inclusive).  If not,
     * it will return the closest boundary from the range.  The ordering of the boundary values does
     * not matter.
     *
     * @param value The value to be compared against the range.
     * @param a First boundary range value.
     * @param b Second boundary range value.
     * @return The passed in value if it is within the range, otherwise the closest boundary value.
     */
    public static float clamp(float value, float a, float b) {
        float min = (a > b) ? b : a;
        float max = (a > b) ? a : b;
        if (value < min)
            value = min;
        else if (value > max)
            value = max;
        return value;
    }
}

Related

  1. clamp(int v, int min, int max)
  2. clamp(int v, int min, int max)
  3. clamp(int v, int min, int max)
  4. clamp(int val, int min, int max)
  5. clamp(int value, final int minimum, final int maximum)
  6. clamp(int value, int low, int high)
  7. clamp(int value, int low, int high)
  8. clamp(int value, int min, int max)
  9. clamp(int value, int min, int max)