Guava Range Example

0

Guava’s Range represents an interval, for example, a < range < b. Here range includes any value between a and b, called  endpoints which form the boundary. Any value between the boundary is a contiguous span of values of type Comparable.

Each end of the range may be bounded or unbounded. If bounded, there is an associated endpoint value. If not it will be treated as infinity. A range can be further defined as either open or closed based whether the range is exclusive or inclusive of the endpoints.

If we use a square bracket [] to indicate that the range is closed on that side and a parenthesis () to indicate that the range is either open or unbounded then a < range < b can be represented by (a..b) and a < range <= b by (a..b]

  1. open(a, b) – It represents a < range < b, and in notation form, (a,b).
  2. closed(a, b) – It represents a <= range <= b, and in notation form, [a,b].
  3. openClosed(a, b) – It represents a < range <= b, and in notation form, (a,b].
  4. closedOpen(a, b) – It represents a <= range < b, and in notation form, [a,b).
  5. greaterThan(a) – It represents range > a, and in notation form, (a..+∞).
  6. atLeast(a, b) – It represents range >= a, and in notation form, [a..+∞).
  7. lessThan(a, b) – It represents range < b, and in notation form, (-∞..b).
  8. atMost(a, b) – It represents range <= b, and in notation form, (-∞..b].
  9. all() – It represents -∞ < range < +∞, and in notation form, (-∞..+∞).

One can check whether a value is within the range using Range.contains(value).

RangesExample:

package com.javarticles.guava;

import com.google.common.collect.Range;

public class RangesExample {

    public static void main(String[] args) {
        System.out
                .println("-----------open(1,4) ==> 1<value<4--------------------");

        // 1<value<4
        Range<Integer> range = Range.open(1, 4);
        System.out.println("Conatins 1? " + range.contains(1));
        System.out.println("Conatins 2? " + range.contains(2));
        System.out.println("Conatins 3? " + range.contains(3));
        System.out.println("Conatins 4? " + range.contains(4));
        System.out
                .println("-----------closed(1,4) ==> 1<=value<=4----------------");

        // 1<=value<=4
        range = Range.closed(1, 4);
        System.out.println("Conatins 0? " + range.contains(0));
        System.out.println("Conatins 1? " + range.contains(1));
        System.out.println("Conatins 2? " + range.contains(2));
        System.out.println("Conatins 3? " + range.contains(3));
        System.out.println("Conatins 4? " + range.contains(4));
        System.out.println("Conatins 5? " + range.contains(5));
        System.out
                .println("-----------openClosed(1,4) ==> 1<value<=4-------------");

        // 1<value<=4
        range = Range.openClosed(1, 4);
        System.out.println("Conatins 0? " + range.contains(0));
        System.out.println("Conatins 1? " + range.contains(1));
        System.out.println("Conatins 2? " + range.contains(2));
        System.out.println("Conatins 3? " + range.contains(3));
        System.out.println("Conatins 4? " + range.contains(4));
        System.out.println("Conatins 5? " + range.contains(5));
        System.out
                .println("-----------closedOpen(1,4) ==> 1<=value<4-------------");

        // 1<=value<4
        range = Range.closedOpen(1, 4);
        System.out.println("Conatins 0? " + range.contains(0));
        System.out.println("Conatins 1? " + range.contains(1));
        System.out.println("Conatins 2? " + range.contains(2));
        System.out.println("Conatins 3? " + range.contains(3));
        System.out.println("Conatins 4? " + range.contains(4));
        System.out.println("Conatins 5? " + range.contains(5));

        System.out
                .println("----------------------2<value-------------------------");
        // 2<value
        range = Range.greaterThan(2);
        System.out.println("Conatins 2? " + range.contains(2));
        System.out.println("Conatins 2000? " + range.contains(2000));

        System.out
                .println("----------------------2<=value-------------------------");
        // 2<=value
        range = Range.atLeast(2);
        System.out.println("Conatins 2? " + range.contains(2));
        System.out.println("Conatins 2000? " + range.contains(2000));

        System.out
                .println("----------------------2>value-------------------------");
        // 2>value
        range = Range.lessThan(2);
        System.out.println("Conatins 2? " + range.contains(2));
        System.out.println("Conatins 1? " + range.contains(1));
        System.out.println("Conatins -1? " + range.contains(-1));

        System.out
                .println("----------------------2<=value-------------------------");
        // 2<=value
        range = Range.atMost(2);
        System.out.println("Conatins 2? " + range.contains(2));
        System.out.println("Conatins 1? " + range.contains(1));
        System.out.println("Conatins -1? " + range.contains(-1));

        System.out
                .println("-----------------------(-infinity<value<+infinity)-------------------------");
        // -infinity<value<+infinity
        range = Range.all();
        System.out.println("Conatins -200? " + range.contains(-200));
        System.out.println("Conatins 200? " + range.contains(200));
    }
}

Output:

-----------open(1,4) ==> 1<value<4--------------------
Conatins 1? false
Conatins 2? true
Conatins 3? true
Conatins 4? false
-----------closed(1,4) ==> 1<=value<=4----------------
Conatins 0? false
Conatins 1? true
Conatins 2? true
Conatins 3? true
Conatins 4? true
Conatins 5? false
-----------openClosed(1,4) ==> 1<value<=4-------------
Conatins 0? false
Conatins 1? false
Conatins 2? true
Conatins 3? true
Conatins 4? true
Conatins 5? false
-----------closedOpen(1,4) ==> 1<=value<4-------------
Conatins 0? false
Conatins 1? true
Conatins 2? true
Conatins 3? true
Conatins 4? false
Conatins 5? false
----------------------2<value-------------------------
Conatins 2? false
Conatins 2000? true
----------------------2<=value-------------------------
Conatins 2? true
Conatins 2000? true
----------------------2>value-------------------------
Conatins 2? false
Conatins 1? true
Conatins -1? true
----------------------2<=value-------------------------
Conatins 2? true
Conatins 1? true
Conatins -1? true
-----------------------(-infinity<value<+infinity)-------------------------
Conatins -200? true
Conatins 200? true

Download the source code

This was an example about Guava Range.

You can download the source code: guavaRangeExamples.zip
Share.

Comments are closed.