Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Validate.inclusiveBetween() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

inclusiveBetween() is a staticthe methods in Java that can be called without creating an object of the class. method of the Validate class that is used to check whether the specified argument object falls between the two specified inclusive values.

If the object does not fall between the two specified inclusive values, then the method throws an exception with the specified message.

How to import Validate

The definition of Validate 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 Validate class as follows:


import org.apache.commons.lang3.Validate;

Syntax


public static <T> void inclusiveBetween(final T start, final T end, final Comparable<T> value, final String message, final Object... values)

Parameters

  • final T start: The inclusive start value.

  • final T end: The inclusive end value.

  • final Comparable<T> value: The value to validate.

  • final String message: The exception message.

  • final Object... values: The optional values for the formatted exception message.

Return value

This method does not return anything.

Code

The code below shows how the inclusiveBetween() method works in Java:

import org.apache.commons.lang3.Validate;
public class Main{
public static void main(String[] args){
// Example 1
int start = 4;
int end = 10;
int valueToCheck = 5;
Validate.inclusiveBetween(start, end, valueToCheck);
// Example 2
String exceptionMessageFormat = "The value '%s' is not in the range [%s, %s]";
valueToCheck = 32;
Validate.inclusiveBetween(start, end, valueToCheck, exceptionMessageFormat, valueToCheck, start, end);
}
}

Output

The output of the code will be as follows:


Exception in thread "main" java.lang.IllegalArgumentException: The value '32' is not in the range [4, 10]
	at org.apache.commons.lang3.Validate.inclusiveBetween(Validate.java:998)
	at Main.main(Main.java:15)

Explanation

Example 1

  • start - 4
  • end - 10
  • valueToCheck - 5

As start >= valueToCheck >= end, the method does not throw an exception.

Example 2

  • start - 4
  • end - 10
  • valueToCheck - 32

As the valueToCheck is not in the range [start, end], the method throws IllegalArgumentException with the exception message as The value '32' is not in the range [4, 10].

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring