Java Utililty Methods Clamp

List of utility methods to do Clamp

Description

The list of methods to do Clamp are organized into topic(s).

Method

shortclamp(final short value, final short lower, final short upper)
Return the value if it is greater than lower and less than upper, otherwise return lower or upper, respectively.
if (value < lower) {
    return lower;
} else if (value > upper) {
    return upper;
} else {
    return value;
Tclamp(final T MIN, final T MAX, final T VALUE)
clamp
if (VALUE.doubleValue() < MIN.doubleValue())
    return MIN;
if (VALUE.doubleValue() > MAX.doubleValue())
    return MAX;
return VALUE;
Tclamp(final T MIN, final T MAX, final T VALUE)
clamp
if (VALUE.doubleValue() < MIN.doubleValue())
    return MIN;
if (VALUE.doubleValue() > MAX.doubleValue())
    return MAX;
return VALUE;
Tclamp(T min, T val, T max)
clamp
return (val.compareTo(min) < 0) ? min : (val.compareTo(max) > 0) ? max : val;
Tclamp(T n, T l, T h)
clamp
return (n.doubleValue() > h.doubleValue() ? h : (n.doubleValue() < l.doubleValue() ? l : n));
Tclamp(T value, T minimum, T maximum)
Clamps a Number
if (minimum.doubleValue() > maximum.doubleValue()) {
    T temp = minimum;
    minimum = maximum;
    maximum = temp;
if (value.doubleValue() > maximum.doubleValue())
    value = maximum;
if (value.doubleValue() < minimum.doubleValue())
...
longclamp01(long value)

Clamp the given value between 0 and 1 .

return clamp(value, 0, 1);
longclampAdd(long lhs, long rhs)
Add the supplied arguments and handle overflow by clamping the resulting sum to Long.MinValue if the sum would have been less than Long.MinValue or Long.MaxValue if the sum would have been greater than Long.MaxValue .
long sum = lhs + rhs;
if ((~(lhs ^ rhs) & (lhs ^ sum)) < 0) {
    if (lhs >= 0) {
        return Long.MAX_VALUE;
    } else {
        return Long.MIN_VALUE;
return sum;