1. Overview
1.概述
In this tutorial, we’ll look at some ways to check whether an integer exists within a given range. We’ll do that using operators as well as several utility classes.
在本教程中,我们将研究一些方法来检查一个整数是否存在于一个给定的范围内。我们将使用运算符以及几个实用类来实现这一目标。
2. Range Types
2.射程类型
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 it’s lower bound
- left-closed right-open range – includes its lower bound and excludes it’s 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.
例如,假设我们想知道整数20是否出现在这两个范围内。R1 = [10, 2o),一个左闭右开的范围,以及R2 = (10, 20],一个左开右闭的范围。由于R1不包含其上限,整数20只存在于R2中。
3. Using the < and <= Operators
3.使用<和<=操作符
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.
我们的目标是确定一个数字是否在一个给定的下限和上限之间。我们将从使用基本的Java操作符来检查这一点开始。
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:
让我们测试一下我们的static isInOpenClosedRange()方法。我们将指定左开右闭的范围(10,20],传入10作为下限,20作为上限。
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.
在我们的第一个测试中,我们成功地验证了整数20存在于(10,20]范围内,这包括它的上界。然后我们确认整数10不存在于同一范围内,这就排除了它的下限。
4. Using Range Classes
4.使用范围类
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.
作为使用Java操作符的替代方法,我们也可以使用表示范围的实用类。使用预定义类的主要好处是,范围类为上述的一些或所有范围类型提供了开箱即用的实现。
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.
另一方面,我们下面要看的两个范围类是在外部库中的,在我们使用它们之前必须导入我们的项目。
4.1. Using java.time.temporal.ValueRange
4.1.使用java.time.temporal.ValueRange
A range class that does not require importing an external library is java.time.temporal.ValueRange, introduced in JDK 1.8:
一个不需要导入外部库的范围类是java.time.temporal.ValueRange,在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.
正如我们在上面看到的,我们通过将lowerBound和upperBound传递给static of()方法来创建ValueRange对象。然后,我们通过使用每个对象的isValidIntValue()方法,检查number是否存在于每个范围内。
We should note that ValueRange only supports closed range checks out of the box. Because of that, we must validate left-open ranges by incrementing lowerBound, and right-open ranges by decrementing upperBound, as we do above.
我们应该注意,ValueRange只支持开箱即用的封闭范围检查。正因为如此,我们必须通过递增lowerBound来验证左开的范围,通过递减upperBound来验证右开的范围,就像我们上面做的那样。
4.2. Using Apache Commons
4.2.使用Apache Commons
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:
让我们继续讨论一些我们可以从第三方库中使用的范围类。首先,我们将把Apache Commons依赖性添加到我们的项目中。
<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:
在这里,我们实现了与之前相同的行为,但使用了Apache Commons的Range类。
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.
与ValueRange的of()方法一样,我们将lowerBound和upperBound传递给Range的static between()方法以创建Range对象。然后我们使用contains()方法来检查number是否存在于每个对象的范围内。
The Apache Commons Range class also only supports closed intervals, but we simply adjusted lowerBound and upperBound again as we did with ValueRange.
Apache Commons的Range类也只支持封闭的区间,但我们只是像对待ValueRange那样再次调整lowerBound和upperBound。
Moreover, as a generic class, Range can be used not only for Integer but for any other type that implements Comparable.
此外,作为一个通用类,Range不仅可以用于Integer,还可以用于任何其他实现Comparable的类型。
4.3. Using Google Guava
4.3.使用Google Guava
Finally, let’s add the Google Guava dependency to our project:
最后,让我们把Google Guava依赖性添加到我们的项目中。
<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:
我们可以使用Guava的Range类来重新实现与之前一样的行为。
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 lowerBound and upperBound to the static closedOpen() method. For a half-open interval that excludes its lower bound, we used openClosed(). We then checked whether number existed in each range using the contains() method.
我们可以看到,Guava的Range类有四个独立的方法来创建我们之前讨论的每种范围类型。也就是说,与我们目前看到的其他范围类不同,Guava的Range类原生支持开放和半开放范围。例如,为了指定一个不包括其上限的半开区间,我们将lowerBound和upperBound传递给static closedOpen()方法。对于一个不包括其下限的半开区间,我们使用openClosed()。然后我们使用contains()方法检查number是否存在于每个区间。
5. Conclusion
5.总结
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.
一如既往,这些示例的源代码可在GitHub上获取。