public final class Range<T extends Comparable> extends Object implements Serializable
The class has some very simple methods for usability. For example contains(Comparable)
method can tell user whether
this range contains argument or not. The contains(Range)
helps to find out whether this range fully
enclosing argument or not.
For more details about how to use it, check out this article on vladmihalcea.com.
Modifier and Type | Method and Description |
---|---|
String |
asString() |
static Range<BigDecimal> |
bigDecimalRange(String range)
Creates the
BigDecimal range from provided string: |
static <T extends Comparable<?>> |
closed(T lower,
T upper)
Creates the closed range with provided bounds.
|
static <T extends Comparable<?>> |
closedInfinite(T lower)
Creates the left-bounded, left-closed and right-unbounded range with provided lower bound.
|
static <T extends Comparable<?>> |
closedOpen(T lower,
T upper)
Creates the left-closed, right-open range with provided bounds.
|
boolean |
contains(Range<T> range)
Determines whether this range contains this point or not.
|
boolean |
contains(T point)
Determines whether this range contains this point or not.
|
boolean |
equals(Object o) |
int |
hashCode() |
boolean |
hasLowerBound() |
boolean |
hasUpperBound() |
static <T extends Comparable<?>> |
infinite(Class<T> cls)
Creates the unbounded at both ends range with provided upper bound.
|
static <T extends Comparable<?>> |
infiniteClosed(T upper)
Creates the left-unbounded, right-bounded and right-closed range with provided upper bound.
|
static <T extends Comparable<?>> |
infiniteOpen(T upper)
Creates the left-unbounded, right-bounded and right-open range with provided upper bound.
|
static Range<Integer> |
integerRange(String range)
Creates the
Integer range from provided string: |
static Range<LocalDate> |
localDateRange(String range)
Creates the
LocalDate range from provided string: |
static Range<LocalDateTime> |
localDateTimeRange(String range)
Creates the
LocalDateTime range from provided string: |
static Range<Long> |
longRange(String range)
Creates the
Long range from provided string: |
T |
lower()
Returns the lower bound of this range.
|
static <T extends Comparable> |
ofString(String str,
Function<String,T> converter,
Class<T> cls) |
static <T extends Comparable<?>> |
open(T lower,
T upper)
Creates the open range with provided bounds.
|
static <T extends Comparable<?>> |
openClosed(T lower,
T upper)
Creates the left-open, right-closed range with provided bounds.
|
static <T extends Comparable<?>> |
openInfinite(T lower)
Creates the left-bounded, left-open and right-unbounded range with provided lower bound.
|
String |
toString() |
T |
upper()
Returns the upper bound of this range.
|
static Range<ZonedDateTime> |
zonedDateTimeRange(String rangeStr)
Creates the
ZonedDateTime range from provided string: |
public static <T extends Comparable<?>> Range<T> closed(T lower, T upper)
The mathematical equivalent will be:
[a, b] = {x | a <= x <= b}
.T
- The type of bounds.lower
- The lower bound, never null.upper
- The upper bound, never null.public static <T extends Comparable<?>> Range<T> open(T lower, T upper)
The mathematical equivalent will be:
(a, b) = {x | a < x < b}
T
- The type of bounds.lower
- The lower bound, never null.upper
- The upper bound, never null.public static <T extends Comparable<?>> Range<T> openClosed(T lower, T upper)
The mathematical equivalent will be:
(a, b] = {x | a < x <= b}
T
- The type of bounds.lower
- The lower bound, never null.upper
- The upper bound, never null.public static <T extends Comparable<?>> Range<T> closedOpen(T lower, T upper)
The mathematical equivalent will be:
[a, b) = {x | a <= x < b}
T
- The type of bounds.lower
- The lower bound, never null.upper
- The upper bound, never null.public static <T extends Comparable<?>> Range<T> openInfinite(T lower)
The mathematical equivalent will be:
(a, +∞) = {x | x > a}
T
- The type of bounds.lower
- The lower bound, never null.public static <T extends Comparable<?>> Range<T> closedInfinite(T lower)
The mathematical equivalent will be:
[a, +∞) = {x | x >= a}
T
- The type of bounds.lower
- The lower bound, never null.public static <T extends Comparable<?>> Range<T> infiniteOpen(T upper)
The mathematical equivalent will be:
(-∞, b) = {x | x < b}
T
- The type of bounds.upper
- The upper bound, never null.public static <T extends Comparable<?>> Range<T> infiniteClosed(T upper)
The mathematical equivalent will be:
(-∞, b] = {x | x =< b}
T
- The type of bounds.upper
- The upper bound, never null.public static <T extends Comparable<?>> Range<T> infinite(Class<T> cls)
The mathematical equivalent will be:
(-∞, +∞) = ℝ
T
- The type of bounds.cls
- The range class, never null.public static <T extends Comparable> Range<T> ofString(String str, Function<String,T> converter, Class<T> cls)
public static Range<BigDecimal> bigDecimalRange(String range)
BigDecimal
range from provided string:
Range<BigDecimal> closed = Range.bigDecimalRange("[0.1,1.1]");
Range<BigDecimal> halfOpen = Range.bigDecimalRange("(0.1,1.1]");
Range<BigDecimal> open = Range.bigDecimalRange("(0.1,1.1)");
Range<BigDecimal> leftUnbounded = Range.bigDecimalRange("(,1.1)");
range
- The range string, for example "[5.5,7.8]".BigDecimal
s.NumberFormatException
- when one of the bounds are invalid.public static Range<Integer> integerRange(String range)
Integer
range from provided string:
Range<Integer> closed = Range.integerRange("[1,5]");
Range<Integer> halfOpen = Range.integerRange("(-1,1]");
Range<Integer> open = Range.integerRange("(1,2)");
Range<Integer> leftUnbounded = Range.integerRange("(,10)");
Range<Integer> unbounded = Range.integerRange("(,)");
range
- The range string, for example "[5,7]".Integer
s.NumberFormatException
- when one of the bounds are invalid.public static Range<Long> longRange(String range)
Long
range from provided string:
Range<Long> closed = Range.longRange("[1,5]");
Range<Long> halfOpen = Range.longRange("(-1,1]");
Range<Long> open = Range.longRange("(1,2)");
Range<Long> leftUnbounded = Range.longRange("(,10)");
Range<Long> unbounded = Range.longRange("(,)");
range
- The range string, for example "[5,7]".Long
s.NumberFormatException
- when one of the bounds are invalid.public static Range<LocalDateTime> localDateTimeRange(String range)
LocalDateTime
range from provided string:
Range<LocalDateTime> closed = Range.localDateTimeRange("[2014-04-28 16:00:49,2015-04-28 16:00:49]");
Range<LocalDateTime> quoted = Range.localDateTimeRange("[\"2014-04-28 16:00:49\",\"2015-04-28 16:00:49\"]");
Range<LocalDateTime> iso = Range.localDateTimeRange("[\"2014-04-28T16:00:49.2358\",\"2015-04-28T16:00:49\"]");
The valid formats for bounds are:
range
- The range string, for example "[2014-04-28 16:00:49,2015-04-28 16:00:49]".LocalDateTime
s.DateTimeParseException
- when one of the bounds are invalid.public static Range<LocalDate> localDateRange(String range)
LocalDate
range from provided string:
Range<LocalDate> closed = Range.localDateRange("[2014-04-28,2015-04-289]");
Range<LocalDate> quoted = Range.localDateRange("[\"2014-04-28\",\"2015-04-28\"]");
Range<LocalDate> iso = Range.localDateRange("[\"2014-04-28\",\"2015-04-28\"]");
The valid formats for bounds are:
range
- The range string, for example "[2014-04-28,2015-04-28]".LocalDate
s.DateTimeParseException
- when one of the bounds are invalid.public static Range<ZonedDateTime> zonedDateTimeRange(String rangeStr)
ZonedDateTime
range from provided string:
Range<ZonedDateTime> closed = Range.zonedDateTimeRange("[2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00]");
Range<ZonedDateTime> quoted = Range.zonedDateTimeRange("[\"2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00\"]");
Range<ZonedDateTime> iso = Range.zonedDateTimeRange("[2011-12-03T10:15:30+01:00[Europe/Paris], 2012-12-03T10:15:30+01:00[Europe/Paris]]");
The valid formats for bounds are:
rangeStr
- The range string, for example "[2011-12-03T10:15:30+01:00,2012-12-03T10:15:30+01:00]".ZonedDateTime
s.DateTimeParseException
- when one of the bounds are invalid.IllegalArgumentException
- when bounds time zones are different.public boolean hasLowerBound()
public boolean hasUpperBound()
public T lower()
null
is returned then this range is left-unbounded.public T upper()
null
is returned then this range is right-unbounded.public boolean contains(T point)
For example:
assertTrue(integerRange("[1,2]").contains(1))
assertTrue(integerRange("[1,2]").contains(2))
assertTrue(integerRange("[-1,1]").contains(0))
assertTrue(infinity(Integer.class).contains(Integer.MAX_VALUE))
assertTrue(infinity(Integer.class).contains(Integer.MIN_VALUE))
assertFalse(integerRange("(1,2]").contains(1))
assertFalse(integerRange("(1,2]").contains(3))
assertFalse(integerRange("[-1,1]").contains(0))
point
- The point to check.point
in this range or not.public boolean contains(Range<T> range)
For example:
assertTrue(integerRange("[-2,2]").contains(integerRange("[-1,1]")))
assertTrue(integerRange("(,)").contains(integerRange("(,)"))
assertFalse(integerRange("[-2,2)").contains(integerRange("[-1,2]")))
assertFalse(integerRange("(-2,2]").contains(1))
range
- The range to check.range
in this range or not.public String asString()
Copyright © 2019. All rights reserved.