public class IntRange extends AbstractList implements Range, Serializable
Represents a list of Integer objects starting at and potentially including a specified
from
value up (or down) to and potentially including a given to
value.
Instances of this class may be either inclusive aware or non-inclusive aware. See the relevant constructors for creating each type. Inclusive aware IntRange instances are suitable for use with Groovy's range indexing - in particular if the from or to values might be negative. This normally happens underneath the covers but is worth keeping in mind if creating these ranges yourself explicitly.
Note: the design of this class might seem a little strange at first. It contains Boolean
flags, inclusiveLeft
and inclusiveRight
, which can be true
,
false
or null
. This design is for backwards compatibility reasons.
Groovy uses this class under the covers to represent range indexing, e.g.
someList[x..y]
and someString[x..<y]
. In early versions of Groovy the
ranges in these expressions were represented under the covers by the
new IntRange(x, y)
and new IntRange(x, y-1)
. This turns out to be a
lossy abstraction when x and/or y are negative values. Now the latter case is
represented by new IntRange(false, x, y)
.
Note: This class is a copy of ObjectRange optimized for int
. If you make any
changes to this class, you might consider making parallel changes to ObjectRange.
Constructor and description |
---|
IntRange(int from, int to) Creates a new non-inclusive aware IntRange . |
protected IntRange(int from, int to, boolean reverse) Creates a new non-inclusive aware IntRange . |
IntRange(boolean inclusiveRight, int from, int to) Creates a new inclusive aware IntRange . |
IntRange(boolean inclusiveLeft, boolean inclusiveRight, int from, int to) Creates a new inclusive aware IntRange |
Type Params | Return Type | Name and description |
---|---|---|
<T extends Number & Comparable> |
public NumberRange |
by(T stepSize) Creates a new NumberRange with the same from and to as this
IntRange but with a step size of stepSize . |
|
public boolean |
contains(Object value) |
|
public boolean |
containsAll(Collection other) |
|
public boolean |
containsWithinBounds(Object o) |
|
public boolean |
equals(Object that) Determines if this object is equal to another object. |
|
public boolean |
equals(IntRange that) Compares an IntRange to another IntRange. |
|
public Integer |
get(int index) |
|
public Integer |
getFrom() |
|
public int |
getFromInt() Gets the 'from' value as a primitive integer. |
|
public Boolean |
getInclusive() Returns the same as getInclusiveRight , kept here for backwards compatibility. |
|
public Boolean |
getInclusiveLeft() Returns the inclusiveLeft flag. |
|
public Boolean |
getInclusiveRight() Returns the inclusiveRight flag. |
|
public Integer |
getTo() |
|
public int |
getToInt() Gets the 'to' value as a primitive integer. |
|
public int |
hashCode() |
|
public String |
inspect() |
|
public boolean |
isReverse() |
|
public Iterator<Integer> |
iterator() |
|
public int |
size() |
|
public void |
step(int step, Closure closure) |
|
public List<Integer> |
step(int step) |
|
public List<Integer> |
subList(int fromIndex, int toIndex) |
|
public RangeInfo |
subListBorders(int size) A method for determining from and to information when using this IntRange to index an aggregate object of the specified size. |
|
public String |
toString() |
Methods inherited from class | Name |
---|---|
class AbstractList |
add, add, remove, get, equals, hashCode, indexOf, clear, lastIndexOf, subList, iterator, addAll, set, listIterator, listIterator, remove, toString, isEmpty, size, toArray, toArray, contains, addAll, removeAll, retainAll, containsAll, wait, wait, wait, getClass, notify, notifyAll, toArray, stream, spliterator, removeIf, parallelStream, forEach, replaceAll, sort |
Creates a new non-inclusive aware IntRange
. If from
is greater than
to
, a reverse range is created with from
and to
swapped.
from
- the first number in the range.to
- the last number in the range. Creates a new non-inclusive aware IntRange
.
from
is greater than to
.from
- the first value in the range.to
- the last value in the range.reverse
- true
if the range should count from
to
to from
. Creates a new inclusive aware IntRange
.
from
- the first value in the range.to
- the last value in the range.inclusiveRight
- true
if the to value is included in the range. Creates a new inclusive aware IntRange
inclusiveLeft
- true
if the from value is included in the range.inclusiveRight
- true
if the to value is included in the range.from
- the first value in the range.to
- the last value in the range. Creates a new NumberRange with the same from
and to
as this
IntRange but with a step size of stepSize
.
stepSize
- the desired step size Determines if this object is equal to another object. Delegates to
AbstractList.equals if that
is anything
other than an IntRange.
It is not necessary to override hashCode
, as
AbstractList.hashCode provides a suitable hash code.
Note that equals is generally handled by DefaultGroovyMethods.equals instead of this method.
that
- the object to comparetrue
if the objects are equalCompares an IntRange to another IntRange.
that
- the object to compare for equalitytrue
if the ranges are equalGets the 'from' value as a primitive integer.
Returns the same as getInclusiveRight
, kept here for backwards compatibility.
Returns the inclusiveLeft flag. Null for non-inclusive aware ranges or non-null for inclusive aware ranges.
Returns the inclusiveRight flag. Null for non-inclusive aware ranges or non-null for inclusive aware ranges.
Gets the 'to' value as a primitive integer.
A method for determining from and to information when using this IntRange to index an aggregate object of the specified size. Normally only used internally within Groovy but useful if adding range indexing support for your own aggregates.
size
- the size of the aggregate being indexedCopyright © 2003-2022 The Apache Software Foundation. All rights reserved.