[Java] Range check that does not require compareTo

How are you implementing range checking?

Checking the range of numbers and characters is often written with a combination of ʻobj.compareTo (value) . However, I just can't remember the return pattern of that compareTo ... All I remember is that the match is "0". I run it every time to check which is smaller / larger, > 0 or <0` ... However, there was a way to avoid writing a range check of "greater than or equal to the minimum value and less than or equal to the maximum value" using the compareTo method or the equal sign / inequality sign operator.

After all Apache Commons is excellent

The ʻorg.apache.commons.lang3.Range` class (which has been around for quite some time but was ignorant) was the savior of Ponkotsu SE ... I can guess this. If you google with "java range check", the implementation by equal sign / inequality sign operator and compareTo is at the top of the search result, so you can not easily reach it unless you devise a little "commons" or keywords, right? ..

Sample code

int num0 = 0;
int num1 = 1;
int num2 = 2;

//Create a range object
Range rng = Range.between(num0, num2);

System.out.println("Check within range(" + num1 + ") = " + rng.contains(num1));
System.out.println("Check within range(" + -1 + ") = " + rng.contains(-1));

//With Range, you can do more than just check the range.
System.out.println("Minimum range= " + rng.getMinimum() + ", Maximum range= " + rng.getMaximum());
System.out.println("Check if the range is after the argument(" + -1 + ") = " + rng.isAfter(-1));
System.out.println("Check if the range is after the argument(" + num0 + ") = " + rng.isAfter(num0));

//Method chain is possible without storing in variable
System.out.println("Check within range(" + num1 + ") = " + Range.between(num0, num2).contains(num1));

Execution result

Check within range(1) = true
Check within range(-1) = false
Minimum range=0, maximum range= 2
Check if the range is after the argument(-1) = true
Check if the range is after the argument(0) = false
Check within range(1) = true

In addition to the above sample, various methods such as ʻisBefore and ʻelementCompareTo that return a return value like compareTo are implemented. By the way, if the minimum and maximum values of between do not match, a compile error will occur, and if one of them is null, an IllegalArgumentException will occur. Also, regardless of the order of the minimum and maximum values passed to between, the range is set correctly even with between (maximum value, minimum value). (The values of getMaximum and getMinimum do not collapse) If the type of the range does not match the type of the contains argument, a ClassCastException will occur.

Benefits of Generics

The Range class is great just because it's a break from compareTo, but that's not the only great thing. The above example was an int type of primitive (no conversion to Integer is required), but for example, you can pass a string type as well as a date or time type! Range I really love you.

//Date type(LocalDate)Sample
LocalDate ld0 = LocalDate.now().minusDays(1);
LocalDate ld1 = LocalDate.now();
LocalDate ld2 = LocalDate.now().plusDays(1);

Range rng = Range.between(ld0, ld2);
System.out.println(rng.contains(ld0)); // true
System.out.println(rng.contains(ld1)); // true
System.out.println(rng.contains(ld2)); // true
System.out.println(rng.contains(ld2.plusDays(1))); // false
System.out.println(rng.isAfter(ld0.minusDays(1))); // true
System.out.println(rng.contains(null)); // false

I wanted to know sooner ... it's the highest ...

You can also pass a Comparator class object to between, so you can compare your own objects. Really the best of the best. I feel that the condition of the ponkotsu is improved a little.

I don't think there are so many Ponkotsu SEs who can't remember the return value of compareTo, but I think it's easier to understand by using the Range class. With contains, no logical operators are needed. Therefore, I would like you to use the Range class for the implementation of range checking in the future.

Recommended Posts

[Java] Range check that does not require compareTo
[Java] compareTo
Java Integer comparison (==) does not work correctly
Eclipse does not start after Java 11 is installed
Java version check
Java check process