public final class Range extends Object implements Serializable
Modifier and Type | Method and Description |
---|---|
static Range |
between(int start,
int end)
Creates a range between two integers.
|
Range |
combineWith(Range other)
Combines two ranges to create a range containing all values in both
ranges, provided there are no gaps between the ranges.
|
boolean |
contains(int integer)
Checks whether an integer is found within this range.
|
boolean |
endsAfter(Range other)
Checks whether this range ends after the end of another range.
|
boolean |
endsBefore(Range other)
Checks whether this range ends before the start of another range.
|
boolean |
equals(Object obj) |
Range |
expand(int startDelta,
int endDelta)
Creates a range that is expanded the given amounts in both ends.
|
int |
getEnd()
Returns the exclusive end point of this range.
|
int |
getStart()
Returns the inclusive start point of this range.
|
int |
hashCode() |
boolean |
intersects(Range other)
Checks whether this range and another range are at least partially
covering the same values.
|
boolean |
isEmpty()
Checks whether the range has no elements between the start and end.
|
boolean |
isSubsetOf(Range other)
Checks whether this range is a subset of another range.
|
int |
length()
The number of integers contained in the range.
|
Range |
offsetBy(int offset)
Get a range that is based on this one, but offset by a number.
|
Range[] |
partitionWith(Range other)
Overlay this range with another one, and partition the ranges according
to how they position relative to each other.
|
Range |
restrictTo(Range bounds)
Limits this range to be within the bounds of the provided range.
|
Range[] |
splitAt(int integer)
Split the range into two at a certain integer.
|
Range[] |
splitAtFromStart(int length)
Split the range into two after a certain number of integers into the
range.
|
boolean |
startsAfter(Range other)
Checks whether this range starts after the end of another range.
|
boolean |
startsBefore(Range other)
Checks whether this range starts before the start of another range.
|
String |
toString() |
static Range |
withLength(int start,
int length)
Creates a range from a start point, with a given length.
|
static Range |
withOnly(int integer)
Creates a range object representing a single integer.
|
public static Range withOnly(int integer)
integer
- the number to represent as a rangeinteger
public static Range between(int start, int end) throws IllegalArgumentException
The range start is inclusive and the end is exclusive. So, a range "between" 0 and 5 represents the numbers 0, 1, 2, 3 and 4, but not 5.
start
- the start of the the range, inclusiveend
- the end of the range, exclusive[start..end[
IllegalArgumentException
- if start > end
public static Range withLength(int start, int length) throws IllegalArgumentException
start
- the first integer to include in the rangelength
- the length of the resulting rangestart
, with
length
number of integers followingIllegalArgumentException
- if length < 0public int getStart()
public int getEnd()
public int length()
public boolean isEmpty()
true
iff the range contains no elements.public boolean intersects(Range other)
other
- the other range to check againsttrue
if this and other
intersectpublic boolean contains(int integer)
integer
- an integer to test for presence in this rangetrue
iff integer
is in this rangepublic boolean isSubsetOf(Range other)
true
iff other
completely wraps this
rangepublic Range[] partitionWith(Range other)
The three partitions are returned as a three-element Range array:
other
.
other
.
other
- the other range to act as delimiters.public Range offsetBy(int offset)
offset
- the number to offset byoffset
public boolean startsBefore(Range other)
other
- the other range to compare againsttrue
iff this range starts before the
other
public boolean endsBefore(Range other)
other
- the other range to compare againsttrue
iff this range ends before the
other
public boolean endsAfter(Range other)
other
- the other range to compare againsttrue
iff this range ends after the
other
public boolean startsAfter(Range other)
other
- the other range to compare againsttrue
iff this range starts after the
other
public Range[] splitAt(int integer)
Example: [5..10[.splitAt(7) == [5..7[, [7..10[
integer
- the integer at which to split the range into two[start..integer[
in the
first element, and [integer..end[
in the second
element.
If integer
is less than start
, [empty,
this
] is returned. if integer
is equal to
or greater than end
, [this
, empty] is returned
instead.
public Range[] splitAtFromStart(int length)
Calling this method is equivalent to calling
splitAt
(getStart()
+length);
Example:
[5..10[.splitAtFromStart(2) == [5..7[, [7..10[
length
- the length at which to split this range into twolength
-first
elements of this range, and the second range having the rest. If
length
≤ 0, the first element will be empty, and
the second element will be this range. If length
≥ length()
, the first element will be this range,
and the second element will be empty.public Range combineWith(Range other) throws IllegalArgumentException
other
- the range to combine with this rangeIllegalArgumentException
- if the two ranges aren't connectedpublic Range expand(int startDelta, int endDelta) throws IllegalArgumentException
startDelta
- the amount to expand by in the beginning of the rangeendDelta
- the amount to expand by in the end of the rangeIllegalArgumentException
- if the new range would have start > end
public Range restrictTo(Range bounds)
This is basically an optimized way of calculating
without the overhead of
defining the parts that do not overlap.
partitionWith(Range)
[1]
If the two ranges do not intersect, an empty range is returned. There are no guarantees about the position of that range.
bounds
- the bounds that the returned range should be limited toCopyright © 2021 Magnolia International Ltd.. All rights reserved.