Here you can find the source of clamp(final short value, final short lower, final short upper)
Parameter | Description |
---|---|
value | Value to clamp. |
lower | Lower bounds of clamp. |
upper | Upper bounds of clamp. |
public static short clamp(final short value, final short lower, final short upper)
//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; } } }