**Get started with Spring 5 and Spring Boot 2, through the ***Learn Spring* course:

*Learn Spring*course:

Last modified: August 3, 2022

In this tutorial, we'll look at some ways to check whether an integer exists within a given range. We'll do this using operators as well as several utility classes.

Before we use any of these methods, we have to be clear about what kind of range we're talking about. We'll focus on these four bounded range types throughout this tutorial:

**closed range**–**includes its lower and upper bounds****open range**–**excludes its lower and upper bounds****left-open right-closed range**–**includes its upper bound and excludes its lower bound****left-closed right-open range**–**includes its lower bound and excludes its upper bound**

For example, suppose we wanted to know whether the integer 20 occurs within these two ranges: *R1 = [10, 2o)*, a left-closed right-open range, and *R2 = (10, 20]*, a left-open right-closed range. Since *R1* does not contain its upper bound, the integer 20 exists only in *R2*.

Our goal is to determine whether a number is between a given lower and upper bound. We'll start by checking for this using basic Java operators.

Let's define a class that does this check for all four kinds of ranges:

```
public class IntRangeOperators {
public static boolean isInClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
return (lowerBound <= number && number <= upperBound);
}
public static boolean isInOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
return (lowerBound < number && number < upperBound);
}
public static boolean isInOpenClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
return (lowerBound < number && number <= upperBound);
}
public static boolean isInClosedOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
return (lowerBound <= number && number < upperBound);
}
}
```

Here, **by changing the operators to include or exclude the bounds, we can tune the interval to be open, closed, or half-open.**

Let's test our *static **isInOpenClosedRange()* method. We'll specify the left-open right-closed range *(10,20]* by passing in 10 for the lower bound and 20 for the upper bound:

```
assertTrue(IntRangeClassic.isInOpenClosedRange(20, 10, 20));
assertFalse(IntRangeClassic.isInOpenClosedRange(10, 10, 20));
```

In our first test, we successfully verified that the integer 20 exists in the *(10,20]* range, which includes its upper bound. We then confirmed that the integer 10 does not exist in the same range, which excludes its lower bound.

As an alternative to using Java operators, we can also use utility classes that represent ranges. The primary benefit to using pre-defined classes is that **range classes offer out-of-the-box implementations for some or all the range types described above.**

Additionally, **we can configure a range object with our defined bounds and reuse the object in other methods or classes**. By defining the range once, our code is less error prone if we need to do multiple checks against the same range throughout our code base.

On the other hand, two of the range classes we'll look at below are in external libraries that must be imported into our project before we can use them.

A range class that does not require importing an external library is *java.time.temporal.ValueRange*, introduced in JDK 1.8:

```
public class IntRangeValueRange {
public boolean isInClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
final ValueRange range = ValueRange.of(lowerBound, upperBound);
return range.isValidIntValue(number);
}
public boolean isInOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
final ValueRange range = ValueRange.of(lowerBound + 1, upperBound - 1);
return range.isValidIntValue(number);
}
public boolean isInOpenClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
final ValueRange range = ValueRange.of(lowerBound + 1, upperBound);
return range.isValidIntValue(number);
}
public boolean isInClosedOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
final ValueRange range = ValueRange.of(lowerBound, upperBound - 1);
return range.isValidIntValue(number);
}
}
```

As we can see above, we created *ValueRange* objects by passing *lowerBound* and *upperBound* to the *static* *of()* method. We then checked whether *number* existed within each range by using each object's *isValidIntValue() *method.

We should note that ** ValueRange only supports closed range checks out of the box**. Because of that,

Let's move on to some range classes we can use from third-party libraries. First, we'll add the Apache Commons dependency to our project:

```
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
```

Here, we're implementing the same behavior as before, but using the Apache Commons *Range* class:

```
public class IntRangeApacheCommons {
public boolean isInClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
final Range<Integer> range = Range.between(lowerBound, upperBound);
return range.contains(number);
}
public boolean isInOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
final Range<Integer> range = Range.between(lowerBound + 1, upperBound - 1);
return range.contains(number);
}
public boolean isInOpenClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
final Range<Integer> range = Range.between(lowerBound + 1, upperBound);
return range.contains(number);
}
public boolean isInClosedOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
final Range<Integer> range = Range.between(lowerBound, upperBound - 1);
return range.contains(number);
}
}
```

As with *ValueRange*‘s *of()* method, we passed *lowerBound *and *upperBound* to *Range*‘s *static between()* method to create *Range* objects. We then used the *contains()* method to check whether *number* existed within each object's range.

**The Apache Commons Range class also only supports closed intervals**, but we simply adjusted

Moreover, as a generic class, *Range* can be used not only for* Integer* but for any other type that implements *Comparable.*

Finally, let's add the Google Guava dependency to our project:

```
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
```

We can use Guava's *Range* class to reimplement the same behavior as before:

```
public class IntRangeGoogleGuava {
public boolean isInClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
final Range<Integer> range = Range.closed(lowerBound, upperBound);
return range.contains(number);
}
public boolean isInOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
final Range<Integer> range = Range.open(lowerBound, upperBound);
return range.contains(number);
}
public boolean isInOpenClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
final Range<Integer> range = Range.openClosed(lowerBound, upperBound);
return range.contains(number);
}
public boolean isInClosedOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
final Range<Integer> range = Range.closedOpen(lowerBound, upperBound);
return range.contains(number);
}
}
```

We can see above that Guava's *Range* class has four separate methods for creating each range type we discussed earlier. That is, unlike the other range classes we've seen so far, **Guava's Range class natively supports open and half-open ranges**. For example, to specify a half-open interval that excludes its upper bound, we passed

In this article, we learned how to use basic operators and range classes to check whether an integer falls within a given range. We also explored the pros and cons of the various approaches.

As always, the source code for these examples is available over on GitHub.

Login

0 Comments

Follow the Java Category

Follow the Java category to get regular info about the new articles and tutorials we publish here.