Search code examples
javaguavarangeset

How to get proper string from Guava range class


For example i do the following:

RangeSet<Long> rangeSet = TreeRangeSet.create();;
rangeSet.add(Range.closed(20L,29L).canonical(DiscreteDomain.longs())); 
rangeSet.add(Range.closed(10L,19L).canonical(DiscreteDomain.longs()));
rangeSet.add(Range.closed(50L,59L).canonical(DiscreteDomain.longs()));
System.out.println(rangeSet);

I get the following output:

[[0‥30), [50‥60)]

As I see 30 is not in range because it is closedOpen and 60 is not in range because it is also closedOpen.

How to get from the Range class the following strings?

[0‥30) contains numbers from 0 to 29
[50‥60) contains numbers from 50 to 59

Solution

  • ContiguousSet<Long> set =
        ContiguousSet.create(range, DiscreteDomain.longs());
    

    Then you can use

    set.first()
    

    and

    set.last()
    

    to get the values you want.