2

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!

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
  • You can go through documentation https://jar-download.com/java-documentation-javadoc.php?a=jool&g=org.jooq&v=0.9.12 . Though it's not pretty clear but could give some clues to you. – Adesh Kumar Dec 17 '17 at 12:04
  • @AdeshKumar Yes! I am try by myself to understand it. But it good to get a better answer and more details on same. – Vinay Prajapati Dec 17 '17 at 12:22

1 Answers1

1

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:-

  1. By Calling constructor with parameter of type Tuple2 from Jool Library

Range range = new Range<>(Tuple.tuple(20, 30));

or

  1. By calling constructor with 2 same type of values

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.

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86