Java Clamp clamp(final short value, final short lower, final short upper)

Here you can find the source of clamp(final short value, final short lower, final short upper)

Description

Return the value if it is greater than lower and less than upper, otherwise return lower or upper, respectively.

License

Creative Commons License

Parameter

Parameter Description
value Value to clamp.
lower Lower bounds of clamp.
upper Upper bounds of clamp.

Return

Value clamped between lower and upper.

Declaration

public static short clamp(final short value, final short lower, final short upper) 

Method Source Code

//package com.java2s;
/**/*www  .j  av a 2s  .  c  o  m*/
 * Static helper class to centralize various useful methods
 * that do not fit in any other util.
 * Copyright (c) 2015 Vinland Solutions
 * Creative Commons Attribution-NonCommercial <http://creativecommons.org/licenses/by-nc/3.0/deed.en_US>
 * @author JayJeckel <http://minecraft.jeckelland.site88.net/>
 */

public class Main {
    /**
     * Return the value if it is greater than lower and less than upper, otherwise
     * return lower or upper, respectively.
     * @param value Value to clamp.
     * @param lower Lower bounds of clamp.
     * @param upper Upper bounds of clamp.
     * @return Value clamped between lower and upper.
     */
    public static short clamp(final short value, final short lower, final short upper) {
        if (value < lower) {
            return lower;
        } else if (value > upper) {
            return upper;
        } else {
            return value;
        }
    }

    /**
     * Return the value if it is greater than lower and less than upper, otherwise
     * return lower or upper, respectively.
     * @param value Value to clamp.
     * @param lower Lower bounds of clamp.
     * @param upper Upper bounds of clamp.
     * @return Value clamped between lower and upper.
     */
    public static int clamp(final int value, final int lower, final int upper) {
        if (value < lower) {
            return lower;
        } else if (value > upper) {
            return upper;
        } else {
            return value;
        }
    }

    /**
     * Return the value if it is greater than lower and less than upper, otherwise
     * return lower or upper, respectively.
     * @param value Value to clamp.
     * @param lower Lower bounds of clamp.
     * @param upper Upper bounds of clamp.
     * @return Value clamped between lower and upper.
     */
    public static long clamp(final long value, final long lower, final long upper) {
        if (value < lower) {
            return lower;
        } else if (value > upper) {
            return upper;
        } else {
            return value;
        }
    }

    /**
     * Return the value if it is greater than lower and less than upper, otherwise
     * return lower or upper, respectively.
     * @param value Value to clamp.
     * @param lower Lower bounds of clamp.
     * @param upper Upper bounds of clamp.
     * @return Value clamped between lower and upper.
     */
    public static float clamp(final float value, final float lower, final float upper) {
        if (value < lower) {
            return lower;
        } else if (value > upper) {
            return upper;
        } else {
            return value;
        }
    }

    /**
     * Return the value if it is greater than lower and less than upper, otherwise
     * return lower or upper, respectively.
     * @param value Value to clamp.
     * @param lower Lower bounds of clamp.
     * @param upper Upper bounds of clamp.
     * @return Value clamped between lower and upper.
     */
    public static double clamp(final double value, final double lower, final double upper) {
        if (value < lower) {
            return lower;
        } else if (value > upper) {
            return upper;
        } else {
            return value;
        }
    }
}

Related

  1. clamp(final T MIN, final T MAX, final T VALUE)
  2. clamp(final T MIN, final T MAX, final T VALUE)
  3. clamp(T min, T val, T max)
  4. clamp(T n, T l, T h)