I have a numeric value myX that I want to bound to some range [X_MIN, X_MAX]. I don't care whether myX is within that range or not, I just want to make it so.
Obviously this works:
if(myX < X_MIN) {
myX = X_MIN;
} else if (myX > X_MAX) {
mX = X_MAX;
}
as does this:
myX = Math.max(X_MIN, Math.min(myX, X_MAX));
The first is too verbose and the meaning of the second isn't immediately obvious. Is there another simple, explicit way to do this? Solutions using Guava, Commons, etc are welcome.