Java Integer Clamp clamp(int value, int minValue, int maxValue)

Here you can find the source of clamp(int value, int minValue, int maxValue)

Description

Limit an integer value to a specific range.

License

Open Source License

Parameter

Parameter Description
value value to be limited
minValue minimum value. Should be <= maxValue.
maxValue maximum value. Should be >= minValue.

Return

if value is in range [minValue, maxValue], value is returned. Otherwise if value > maxValue, maxValue is returned, if value < minValue, minValue is returned.

Declaration

public static int clamp(int value, int minValue, int maxValue) 

Method Source Code

//package com.java2s;
/***************************************************************************
 *                   (C) Copyright 2003-2010 - Stendhal                    *
 ***************************************************************************
 ***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

public class Main {
    /**//w  ww  .j ava 2s .c  o m
     * Limit an integer value to a specific range.
     * 
     * @param value value to be limited
     * @param minValue minimum value. Should be <= maxValue.
     * @param maxValue maximum value. Should be >= minValue.
     * @return if value is in range [minValue, maxValue], value is returned.
     *    Otherwise if value > maxValue, maxValue is returned, if value < minValue,
     *    minValue is returned.
     */
    public static int clamp(int value, int minValue, int maxValue) {
        return Math.max(minValue, Math.min(value, maxValue));
    }

    /**
     * Limit a float value to a specific range.
     * 
     * @param value value to be limited
     * @param minValue minimum value. Should be <= maxValue.
     * @param maxValue maximum value. Should be >= minValue.
     * @return if value is in range [minValue, maxValue], value is returned.
     *    Otherwise if value > maxValue, maxValue is returned, if value < minValue,
     *    minValue is returned.
     */
    public static float clamp(float value, float minValue, float maxValue) {
        return Math.max(minValue, Math.min(value, maxValue));
    }
}

Related

  1. clamp(int value, int min, int max)
  2. clamp(int value, int min, int max)
  3. clamp(int value, int min, int max)
  4. clamp(int value, int min, int max)
  5. clamp(int value, int min, int max)
  6. clamp(int var, int min, int max)
  7. clamp(int x, int min, int max)
  8. clamp(int x, int min, int max)
  9. clamp(String string, int maxChars)