I want to return the number as long as it falls within a limit, else return the maximum or minimum value of the limit. I can do this with a combination of Math.min
and Math.max
.
public int limit(int value) {
return Math.max(0, Math.min(value, 10));
}
I'm wondering if there's an existing limit
or range
function I'm overlooking.
3rd party libraries welcome if they are pretty common (eg: Commons or Guava)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…