isBefore() is an instance method of the Range class that is used to check whether the given range is before the specified element or not.
RangeThe definition of Range 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>
For other versions of the commons-lang package, refer to the Maven Repository.
You can import the Range class with the following command:
import org.apache.commons.lang3.Range;
public boolean isBefore(final T element)
final T element: The element to check.The isBefore() method returns true if the range is entirely before the specified element. Otherwise, it returns false.
import org.apache.commons.lang3.Range;public class Main{public static void main(String[] args) {int fromValue = 100;int toValue = 200;Range<Integer> range = Range.between(fromValue, toValue);// Example 1int element = 150;System.out.printf("%s.isBefore(%s) = %s", range, element, range.isBefore(element));System.out.println();// Example 2element = 55;System.out.printf("%s.isBefore(%s) = %s", range, element, range.isBefore(element));System.out.println();// Example 3element = 300;System.out.printf("%s.isBefore(%s) = %s", range, element, range.isBefore(element));}}
range = [100..200]element = 150The method returns false because the entire range is not before the element.
range = [100..200]element = 55The method returns false because the entire range is not before the element.
range = [100..200]element = 300The method returns true because the entire range is before the element.
The output of the code will be as follows:
[100..200].isBefore(150) = false
[100..200].isBefore(55) = false
[100..200].isBefore(300) = true