Java Integer Clamp clamp(int number, int low, int high)

Here you can find the source of clamp(int number, int low, int high)

Description

Returns a number clamped between low and high inclusive.

License

Open Source License

Parameter

Parameter Description
number The number to clamp.
low The lower bound.
high The higher bound.

Return

The clamped number.

Declaration

public static int clamp(int number, int low, int high) 

Method Source Code

//package com.java2s;
/*//from   w w w.  jav a2s  . c o  m
 * DoomManager
 * Copyright (C) 2014  Chris K
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Returns a number clamped between low and high inclusive. If low is not
     * the lower of the low/high, it will be set via this method.
     * 
     * @param number
     *       The number to clamp.
     * 
     * @param low
     *       The lower bound.
     * 
     * @param high
     *       The higher bound.
     * 
     * @return
     *       The clamped number.
     */
    public static int clamp(int number, int low, int high) {
        if (low == high)
            return low;
        int _low = Math.min(low, high);
        int _high = Math.max(low, high);
        return Math.min(Math.max(number, _low), _high);
    }
}

Related

  1. clamp(int n, int l, int u)
  2. clamp(int n, int lower, int upper)
  3. clamp(int n, int min, int max)
  4. clamp(int n, int min, int max)
  5. clamp(int num, int min, int max)
  6. clamp(int ptr, int size)
  7. clamp(int v, int min, int max)
  8. clamp(int v, int min, int max)
  9. clamp(int v, int min, int max)