forceBetween method
Clamps this integer to be within the inclusive range [from, to].
Returns from if this is less than from, to if this is greater than
to, or this unchanged if already within range.
If from > to (invalid range), returns this unchanged.
Example:
5.forceBetween(1, 10); // 5 (within range)
0.forceBetween(1, 10); // 1 (clamped to min)
15.forceBetween(1, 10); // 10 (clamped to max)
Implementation
int forceBetween(int from, final int to) {
if (from > to) {
return this;
}
if (this < from) {
return from;
}
if (this > to) {
return to;
}
return this;
}