Class Range
- All Implemented Interfaces:
Serializable
The range is treated as inclusive at the start, and exclusive at the end. I.e. the range [0..1[ has the length 1, and represents one integer: 0.
The range is considered empty
if the start is the same as
the end.
For internal use only. May be renamed or removed in a future release.
- Since:
- 1.0
- Author:
- Vaadin Ltd
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Range
between
(int start, int end) Creates a range between two integers.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
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
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.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.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.stream()
Returns the range as a stream of integers.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.
-
Method Details
-
partitionWith
Overlay this range with another one, and partition the ranges according to how they position relative to each other.The three partitions are returned as a three-element Range array:
- Elements in this range that occur before elements in
other
. - Elements that are shared between the two ranges.
- Elements in this range that occur after elements in
other
.
- Parameters:
other
- the other range to act as delimiters.- Returns:
- a three-element Range array of partitions depicting the elements before (index 0), shared/inside (index 1) and after (index 2).
- Elements in this range that occur before elements in
-
offsetBy
Get a range that is based on this one, but offset by a number.- Parameters:
offset
- the number to offset by- Returns:
- a copy of this range, offset by
offset
-
withOnly
Creates a range object representing a single integer.- Parameters:
integer
- the number to represent as a range- Returns:
- the range represented by
integer
-
between
Creates a range between two integers.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.
- Parameters:
start
- the start of the range, inclusiveend
- the end of the range, exclusive- Returns:
- a range representing
[start..end[
- Throws:
IllegalArgumentException
- ifstart > end
-
withLength
Creates a range from a start point, with a given length.- Parameters:
start
- the first integer to include in the rangelength
- the length of the resulting range- Returns:
- a range starting from
start
, withlength
number of integers following - Throws:
IllegalArgumentException
- if length < 0
-
getStart
public int getStart()Returns the inclusive start point of this range.- Returns:
- the start point of this range
-
getEnd
public int getEnd()Returns the exclusive end point of this range.- Returns:
- the end point of this range
-
length
public int length()The number of integers contained in the range.- Returns:
- the number of integers contained in the range
-
isEmpty
public boolean isEmpty()Checks whether the range has no elements between the start and end.- Returns:
true
if and only if the range contains no elements.
-
intersects
Checks whether this range and another range are at least partially covering the same values.- Parameters:
other
- the other range to check against- Returns:
true
if this andother
intersect
-
contains
public boolean contains(int integer) Checks whether an integer is found within this range.- Parameters:
integer
- an integer to test for presence in this range- Returns:
true
if and only ifinteger
is in this range
-
isSubsetOf
Checks whether this range is a subset of another range.- Parameters:
other
- the range to check against of- Returns:
true
if and only ifother
completely wraps this range
-
toString
-
hashCode
public int hashCode() -
equals
-
startsBefore
Checks whether this range starts before the start of another range.- Parameters:
other
- the other range to compare against- Returns:
true
if and only if this range starts before theother
-
endsBefore
Checks whether this range ends before the start of another range.- Parameters:
other
- the other range to compare against- Returns:
true
if and only if this range ends before theother
-
endsAfter
Checks whether this range ends after the end of another range.- Parameters:
other
- the other range to compare against- Returns:
true
if and only if this range ends after theother
-
startsAfter
Checks whether this range starts after the end of another range.- Parameters:
other
- the other range to compare against- Returns:
true
if and only if this range starts after theother
-
splitAt
Split the range into two at a certain integer.Example:
[5..10[.splitAt(7) == [5..7[, [7..10[
- Parameters:
integer
- the integer at which to split the range into two- Returns:
- an array of two ranges, with
[start..integer[
in the first element, and[integer..end[
in the second element.If
integer
is less thanstart
, [empty,this
] is returned. ifinteger
is equal to or greater thanend
, [this
, empty] is returned instead.
-
splitAtFromStart
Split the range into two after a certain number of integers into the range.Calling this method is equivalent to calling
splitAt
(getStart()
+length);Example:
[5..10[.splitAtFromStart(2) == [5..7[, [7..10[
- Parameters:
length
- the length at which to split this range into two- Returns:
- an array of two ranges, having the
length
-first elements of this range, and the second range having the rest. Iflength
≤ 0, the first element will be empty, and the second element will be this range. Iflength
≥length()
, the first element will be this range, and the second element will be empty.
-
combineWith
Combines two ranges to create a range containing all values in both ranges, provided there are no gaps between the ranges.- Parameters:
other
- the range to combine with this range- Returns:
- the combined range
- Throws:
IllegalArgumentException
- if the two ranges aren't connected
-
expand
Creates a range that is expanded the given amounts in both ends.- Parameters:
startDelta
- the amount to expand by in the beginning of the rangeendDelta
- the amount to expand by in the end of the range- Returns:
- an expanded range
- Throws:
IllegalArgumentException
- if the new range would havestart > end
-
restrictTo
Limits this range to be within the bounds of the provided range.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.
- Parameters:
bounds
- the bounds that the returned range should be limited to- Returns:
- a bounded range
-
stream
Returns the range as a stream of integers.The first element of the stream is the
getStart()
return value and the last one is thegetEnd()
return value.- Returns:
- the range as a stream
- See Also:
-