Sometimes we write unnecessary code. My question is pretty simple: is there a method like the following?
/** @return true if a given value is inside the range. */
public static boolean range(min, max, value)
I didn't find it on Google. Is that because it doesn't exist?
You could create a typed Range
class that has a within
method:
public class Range<T extends Comparable<T>> {
private final T min;
private final T max;
public Range( T min, T max ) {
this.min = min;
this.max = max;
}
public boolean within( T value ) {
return min.compareTo(value) <= 0 && max.compareTo(value) >= 0;
}
}
If min and max were the same for a group of tests, you could reuse your range
object for all tests.
FWIW, this seems kinda handy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With