Range Class | Guava | Java
Last Updated :
02 Jun, 2018
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.
Declaration : The declaration for
com.google.common.collect.Range<C> class is :
@GwtCompatible
public final class Range<C extends Comparable>
extends Object
implements Predicate<C>, Serializable
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.
- open(a, b) : It represents a < range < b, and in notation form, (a, b).
- closed(a, b) : It represents a <= range <= b, and in notation form, [a, b].
- openClosed(a, b) : It represents a < range <= b, and in notation form, (a, b].
- closedOpen(a, b) : It represents a <= range < b, and in notation form, [a, b).
- greaterThan(a) : It represents range > a, and in notation form, (a..+inf).
- atLeast(a, b) : It represents range >= a, and in notation form, [a..+inf).
- lessThan(a, b) : It represents range < b, and in notation form, (-inf..b).
- atMost(a, b) : It represents range <= b, and in notation form, (-inf..b].
- all() : It represents -inf < range < +inf, and in notation form, (-inf..+inf).
Below given are some methods provided by Range Class of Guava :
Note : When both endpoints exist, the upper endpoint may not be less than the lower. The endpoints may be equal only if at least one of the bounds is closed :
- [a..a] : a singleton range.
- [a..a) or (a..a] : empty ranges, also valid.
- (a..a) : invalid, an exception will be thrown.
Some other methods provided by Range Class of Guava are :
Exceptions :
- open : IllegalArgumentException if lower is greater than or equal to upper.
- closed : IllegalArgumentException if lower is greater than upper.
- closedOpen: IllegalArgumentException if lower is greater than upper.
- openClosed : IllegalArgumentException if lower is greater than upper.
- range : IllegalArgumentException if lower is greater than upper.
- encloseAll : ClassCastException if the parameters are not mutually comparable, NoSuchElementException if values is empty, NullPointerException if any of values is null.
- lowerEndpoint : IllegalStateException if this range is unbounded below (that is, hasLowerBound() returns false).
- lowerBoundType : IllegalStateException if this range is unbounded below (that is, hasLowerBound() returns false).
- upperEndpoint : IllegalStateException if this range is unbounded above (that is, hasUpperBound() returns false).
- upperBoundType : IllegalStateException if this range is unbounded above (that is, hasUpperBound() returns false).
- intersection : IllegalArgumentException if isConnected(connectedRange) is false.

Below given are some examples to understand the implementation in a better way :
Example 1 :
Java
// Java code to show implementation
// of Range class of Guava
import com.google.common.collect.Range;
class GFG {
// Driver code
public static void main(String[] args)
{
// Taking range (1, 5)
// Note that here 1 and 5 are not included
Range<Integer> range = Range.open(1, 5);
// Checking if range contains 1 or not
System.out.println(range.contains(1));
// Checking if range contains 2 or not
System.out.println(range.contains(2));
// Checking if range contains 3 or not
System.out.println(range.contains(3));
// Checking if range contains 4 or not
System.out.println(range.contains(4));
}
}
Output :
false
true
true
true
Example 2 :
Java
// Java code to show implementation
// of Range class of Guava
import com.google.common.collect.Range;
class GFG {
// Driver code
public static void main(String[] args)
{
// Taking range [1, 5]
// Note that here 1 and 5 are included
Range<Integer> range = Range.closed(1, 5);
// Checking if range contains 1 or not
System.out.println(range.contains(1));
// Checking if range contains 2 or not
System.out.println(range.contains(2));
// Checking if range contains 3 or not
System.out.println(range.contains(3));
// Checking if range contains 5 or not
System.out.println(range.contains(5));
}
}
Output :
true
true
true
true
Example 3 :
Java
// Java code to show implementation
// of Range class of Guava
import com.google.common.collect.Range;
class GFG {
// Driver code
public static void main(String[] args)
{
// Taking range (2, +inf)
// Note that numbers less than equal to 2
// are not included
Range<Integer> range = Range.greaterThan(2);
// Checking if range contains 1 or not
System.out.println(range.contains(1));
// Checking if range contains 2 or not
System.out.println(range.contains(2));
// Checking if range contains 130 or not
System.out.println(range.contains(130));
// Checking if range contains 500 or not
System.out.println(range.contains(500));
}
}
Output :
false
false
true
true
Example 4 :
Java
// Java code to show implementation
// of Range class of Guava
import com.google.common.collect.Range;
class GFG {
// Driver code
public static void main(String[] args)
{
// Taking range (-inf, 2]
// Note that only numbers less than equal to 2
// are included
Range<Integer> range = Range.atMost(2);
// Checking if range contains 1 or not
System.out.println(range.contains(1));
// Checking if range contains 2 or not
System.out.println(range.contains(2));
// Checking if range contains -1 or not
System.out.println(range.contains(-1));
// Checking if range contains 5 or not
System.out.println(range.contains(5));
}
}
Output :
true
true
true
false
Similar Reads
Longs Class | Guava | Java Longs is a utility class for primitive type long. It provides Static utility methods pertaining to long primitives, that are not already found in either Long or Arrays. Declaration : @GwtCompatible(emulated=true) public final class Longs extends Object Below table shows the Field summary for Guava L
3 min read
Ordering Class | Guava | Java A comparator, with additional methods to support common operations. This is an "enriched" version of Comparator. The common ways to get an instance of Ordering are : Subclass it and implement compare(T, T) instead of implementing Comparator directly. Pass a pre-existing Comparator instance to from(C
4 min read
Ordering Class | Guava | Java A comparator, with additional methods to support common operations. This is an "enriched" version of Comparator. The common ways to get an instance of Ordering are : Subclass it and implement compare(T, T) instead of implementing Comparator directly. Pass a pre-existing Comparator instance to from(C
4 min read
Joiner class | Guava | Java Guava's Joiner class provides various methods to handle joining operations on string, objects, etc. This class provides advanced functionality for the join operation. Declaration: Following is the declaration for com.google.common.base.Joiner class : @GwtCompatible public class Joiner extends Object
2 min read
Joiner class | Guava | Java Guava's Joiner class provides various methods to handle joining operations on string, objects, etc. This class provides advanced functionality for the join operation. Declaration: Following is the declaration for com.google.common.base.Joiner class : @GwtCompatible public class Joiner extends Object
2 min read
LongMath Class | Guava | Java LongMath is used to perform mathematical operations on Long values. Basic standalone math functions are divided into the classes IntMath, LongMath, DoubleMath, and BigIntegerMath based on the primary numeric type involved. These classes have parallel structure, but each supports only the relevant su
3 min read