BAEL-5651: How to check if an integer is in a given range? (#12470)
* BAEL-5651: How to check if an integer is in a given range? * BAEL-5651: How to check if an integer is in a given range?
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.intrange;
|
||||
|
||||
import org.apache.commons.lang3.Range;
|
||||
|
||||
public class IntRangeApacheCommons {
|
||||
|
||||
public static boolean isInClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final Range<Integer> range = Range.between(lowerBound, upperBound);
|
||||
return range.contains(number);
|
||||
}
|
||||
|
||||
public static boolean isInOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final Range<Integer> range = Range.between(lowerBound + 1, upperBound - 1);
|
||||
return range.contains(number);
|
||||
}
|
||||
|
||||
public static boolean isInOpenClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final Range<Integer> range = Range.between(lowerBound + 1, upperBound);
|
||||
return range.contains(number);
|
||||
}
|
||||
|
||||
public static boolean isInClosedOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final Range<Integer> range = Range.between(lowerBound, upperBound - 1);
|
||||
return range.contains(number);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.intrange;
|
||||
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
public class IntRangeGoogleGuava {
|
||||
|
||||
public static boolean isInClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final Range<Integer> range = Range.closed(lowerBound, upperBound);
|
||||
return range.contains(number);
|
||||
}
|
||||
|
||||
public static boolean isInOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final Range<Integer> range = Range.open(lowerBound, upperBound);
|
||||
return range.contains(number);
|
||||
}
|
||||
|
||||
public static boolean isInOpenClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final Range<Integer> range = Range.openClosed(lowerBound, upperBound);
|
||||
return range.contains(number);
|
||||
}
|
||||
|
||||
public static boolean isInClosedOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final Range<Integer> range = Range.closedOpen(lowerBound, upperBound);
|
||||
return range.contains(number);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.intrange;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.intrange;
|
||||
|
||||
import java.time.temporal.ValueRange;
|
||||
|
||||
public class IntRangeValueRange {
|
||||
|
||||
public static boolean isInClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final ValueRange range = ValueRange.of(lowerBound, upperBound);
|
||||
return range.isValidIntValue(number);
|
||||
}
|
||||
|
||||
public static boolean isInOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final ValueRange range = ValueRange.of(lowerBound + 1, upperBound - 1);
|
||||
return range.isValidIntValue(number);
|
||||
}
|
||||
|
||||
public static boolean isInOpenClosedRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final ValueRange range = ValueRange.of(lowerBound + 1, upperBound);
|
||||
return range.isValidIntValue(number);
|
||||
}
|
||||
|
||||
public static boolean isInClosedOpenRange(Integer number, Integer lowerBound, Integer upperBound) {
|
||||
final ValueRange range = ValueRange.of(lowerBound, upperBound - 1);
|
||||
return range.isValidIntValue(number);
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package com.baeldung.intrange;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IntRangeApacheCommonsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIntRangeApacheCommons_whenIsInClosedRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeApacheCommons.isInClosedRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeApacheCommons.isInClosedRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(resultLowerBound);
|
||||
assertTrue(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeApacheCommons_whenIsNotInClosedRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeApacheCommons.isInClosedRange(8, 10, 20);
|
||||
boolean resultUpperBound = IntRangeApacheCommons.isInClosedRange(22, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeApacheCommons_whenIsInOpenRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeApacheCommons.isInOpenRange(11, 10, 20);
|
||||
boolean resultUpperBound = IntRangeApacheCommons.isInOpenRange(19, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(resultLowerBound);
|
||||
assertTrue(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeApacheCommons_whenIsNotInOpenRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeApacheCommons.isInOpenRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeApacheCommons.isInOpenRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeApacheCommons_whenIsInOpenClosedRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeApacheCommons.isInOpenClosedRange(11, 10, 20);
|
||||
boolean resultUpperBound = IntRangeApacheCommons.isInOpenClosedRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(resultLowerBound);
|
||||
assertTrue(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeApacheCommons_whenIsNotInOpenClosedRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeApacheCommons.isInOpenClosedRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeApacheCommons.isInOpenClosedRange(21, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeApacheCommons_whenIsInClosedOpenRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeApacheCommons.isInClosedOpenRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeApacheCommons.isInClosedOpenRange(19, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(resultLowerBound);
|
||||
assertTrue(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeApacheCommons_whenIsNotInClosedOpenRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeApacheCommons.isInClosedOpenRange(9, 10, 20);
|
||||
boolean resultUpperBound = IntRangeApacheCommons.isInClosedOpenRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.intrange;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IntRangeGoogleGuavaUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIntRangeGoogleGuava_whenIsInOpenRange_thenSuccess() {
|
||||
// when
|
||||
boolean result = IntRangeGoogleGuava.isInOpenRange(14, 10, 20);
|
||||
|
||||
//then
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeGoogleGuava_whenIsNotInOpenRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeGoogleGuava.isInOpenRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeGoogleGuava.isInOpenRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeGoogleGuava_whenIsInClosedRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeGoogleGuava.isInClosedRange(-10, -10, 5);
|
||||
boolean resultUpperBound = IntRangeGoogleGuava.isInClosedRange(5, -10, 5);
|
||||
|
||||
// then
|
||||
assertTrue(resultLowerBound);
|
||||
assertTrue(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeGoogleGuava_whenIsNotInClosedRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeGoogleGuava.isInClosedRange(-11, -10, 5);
|
||||
boolean resultUpperBound = IntRangeGoogleGuava.isInClosedRange(6, -10, 5);
|
||||
|
||||
//then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeGoogleGuava_whenIsInOpenClosedRange_thenSuccess() {
|
||||
// when
|
||||
boolean result = IntRangeGoogleGuava.isInOpenClosedRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeGoogleGuava_whenIsNotInOpenClosedRange_thenFailure() {
|
||||
// when
|
||||
boolean result = IntRangeGoogleGuava.isInOpenClosedRange(10, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeGoogleGuava_whenIsInClosedOpenRange_thenSuccess() {
|
||||
// when
|
||||
boolean result = IntRangeGoogleGuava.isInClosedOpenRange(10, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeGoogleGuava_whenIsNotInClosedOpenRange_thenFailure() {
|
||||
// when
|
||||
boolean result = IntRangeGoogleGuava.isInClosedOpenRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.intrange;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IntRangeOperatorsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIntRangeOperators_whenIsInOpenRange_thenSuccess() {
|
||||
// when
|
||||
boolean result = IntRangeOperators.isInOpenRange(11, 10, 20);
|
||||
|
||||
//then
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeOperators_whenIsNotInOpenRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeOperators.isInOpenRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeOperators.isInOpenRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeOperators_whenIsInClosedRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeOperators.isInClosedRange(-10, -10, 5);
|
||||
boolean resultUpperBound = IntRangeOperators.isInClosedRange(5, -10, 5);
|
||||
|
||||
// then
|
||||
assertTrue(resultUpperBound);
|
||||
assertTrue(resultLowerBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeOperators_whenIsNotInClosedRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeOperators.isInClosedRange(-11, -10, 5);
|
||||
boolean resultUpperBound = IntRangeOperators.isInClosedRange(6, -10, 5);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeOperators_whenIsInOpenClosedRange_thenSuccess() {
|
||||
// when
|
||||
boolean result = IntRangeOperators.isInOpenClosedRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeOperators_whenIsNotInOpenClosedRange_thenFailure() {
|
||||
// when
|
||||
boolean result = IntRangeOperators.isInOpenClosedRange(10, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeOperators_whenIsInClosedOpenRange_thenSuccess() {
|
||||
// when
|
||||
boolean result = IntRangeOperators.isInClosedOpenRange(10, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeOperators_whenIsNotInClosedOpenRange_thenFailure() {
|
||||
// when
|
||||
boolean result = IntRangeOperators.isInClosedOpenRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package com.baeldung.intrange;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IntRangeValueRangeUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIntRangeValueRange_whenIsInClosedRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeValueRange.isInClosedRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeValueRange.isInClosedRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(resultLowerBound);
|
||||
assertTrue(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeValueRange_whenIsNotInClosedRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeValueRange.isInClosedRange(9, 10, 20);
|
||||
boolean resultUpperBound = IntRangeValueRange.isInClosedRange(21, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeValueRange_whenIsInOpenRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeValueRange.isInOpenRange(11, 10, 20);
|
||||
boolean resultUpperBound = IntRangeValueRange.isInOpenRange(19, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(resultLowerBound);
|
||||
assertTrue(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeValueRange_whenIsNotInOpenRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeValueRange.isInOpenRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeValueRange.isInOpenRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeValueRange_whenIsInOpenClosedRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeValueRange.isInOpenClosedRange(11, 10, 20);
|
||||
boolean resultUpperBound = IntRangeValueRange.isInOpenClosedRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(resultLowerBound);
|
||||
assertTrue(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeValueRange_whenIsNotInOpenClosedRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeValueRange.isInOpenClosedRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeValueRange.isInOpenClosedRange(21, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeValueRange_whenIsInClosedOpenRange_thenSuccess() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeValueRange.isInClosedOpenRange(10, 10, 20);
|
||||
boolean resultUpperBound = IntRangeValueRange.isInClosedOpenRange(19, 10, 20);
|
||||
|
||||
// then
|
||||
assertTrue(resultLowerBound);
|
||||
assertTrue(resultUpperBound);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntRangeValueRange_whenIsNotInClosedOpenRange_thenFailure() {
|
||||
// when
|
||||
boolean resultLowerBound = IntRangeValueRange.isInClosedOpenRange(9, 10, 20);
|
||||
boolean resultUpperBound = IntRangeValueRange.isInClosedOpenRange(20, 10, 20);
|
||||
|
||||
// then
|
||||
assertFalse(resultLowerBound);
|
||||
assertFalse(resultUpperBound);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user