Can anyone help me with an example of how to create Range in JOOL
& What is the meaning of a Range
in JOOL? Its java doc says
A range is a special {@link Tuple2} with two times the same type.
Also, Range
has methods like
public boolean overlaps(Tuple2<T, T> other) {
return Tuple2.overlaps(this, other);
}
public boolean overlaps(T t1, T t2) {
return overlaps(new Range<>(t1, t2));
}
public Optional<Range<T>> intersect(Tuple2<T, T> other) {
return Tuple2.intersect(this, other).map(Range::new);
}
public Optional<Range<T>> intersect(T t1, T t2) {
return intersect(new Range<>(t1, t2));
}
Please explain use-case of these as well. Thanks!
Range
is basically like a range in mathematics e.g. 1..6 is a range starting from 1 and ending at 6. A range can be created in two ways:-
Tuple2
from Jool LibraryRange range = new Range<>(Tuple.tuple(20, 30));
or
Range range = new Range<>(20,30);
Now, you could perform 2 actions on range overlap
& intersection
.
Overlap means that there is at least one element in a Range that exists in other range as well. overlaps
method returns true or false. See example below:-
package jool.features.analysis.tuple;
import org.jooq.lambda.tuple.Range;
import org.jooq.lambda.tuple.Tuple;
public class RangeTest {
public static void main(String[] args) {
Range<Integer> range = new Range<>(20,30);
Range<Integer> range2 = new Range<>(Tuple.tuple(20,30));
System.out.println(range.overlaps(22,40)); // returns true as 22 falls between 20 & 30.
}
}
Similarly, a intersection means a Range that comes out of intersection between two Ranges. See example below:-
package jool.features.analysis.tuple;
import org.jooq.lambda.tuple.Range;
import org.jooq.lambda.tuple.Tuple;
public class RangeTest {
public static void main(String[] args) {
Range<Integer> range = new Range<>(20,30);
Range<Integer> range2 = new Range<>(Tuple.tuple(20,30));
System.out.println(range.intersection(22,40)); // returns Range(22,30)
}
}
In Range you could either pass same type of values or a Tuple2 of generic type as below:-
Tuple2<T,T>
where T is any Generic Type
i.e. Both the values in tuple should be of same type and Tuple should be of length 2 only.
It took a while for me to understand how it all works. But I still don't understand a use-case scenario where using Range fits in.