What is Range.isBeforeRange() in Java?
Overview
isBeforeRange() is an instance method of the Range class in Java that is used to check whether a range is complete before the specified range given as a parameter.
How to import the Range class
The definition of Range class can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Please refer to the Maven Repository for other versions of the commons-lang package.
You can import the Range class as follows:
import org.apache.commons.lang3.Range;
Syntax
public boolean isBeforeRange(final Range<T> otherRange)
Parameters
final Range<T> otherRange: The range to check.
Return value
This method returns true if the range object is completed before the specified range. Otherwise, it returns false.
Code
import org.apache.commons.lang3.Range;public class Main{public static void main(String[] args) {int fromValue = 100;int toValue = 200;Range<Integer> range1 = Range.between(fromValue, toValue);fromValue = 300;toValue = 400;Range<Integer> range2 = Range.between(fromValue, toValue);// Example 1System.out.printf("%s.isBeforeRange(%s) = %s", range1, range2, range1.isBeforeRange(range2));System.out.println();// Example 2System.out.printf("%s.isBeforeRange(%s) = %s", range2, range1, range2.isBeforeRange(range1));System.out.println();}}
Explanation
Example 1
range1 = [100..200]range2 = [300..400]
The method returns true because range1 occurs before range2.
Example 2
range1 = [100..200]range2 = [300..400]
The method returns false because range2 occurs after range1.
Output
The output of the code will be as follows:
[100..200].isBeforeRange([300..400]) = true
[300..400].isBeforeRange([100..200]) = false