public class IntRange extends AbstractList<Integer> implements Range<Integer>
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 a Boolean
field, inclusive
, 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
.
modCount
Modifier | Constructor and Description |
---|---|
|
IntRange(boolean inclusive,
int from,
int to)
Creates a new inclusive aware
IntRange . |
|
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 . |
Modifier and Type | Method and Description |
---|---|
<T extends Number & Comparable> |
by(T stepSize)
Creates a new NumberRange with the same
from and to as this
IntRange but with a step size of stepSize . |
boolean |
contains(Object value) |
boolean |
containsAll(Collection other) |
boolean |
containsWithinBounds(Object o)
Indicates whether an object is greater than or equal to the
from
value for the range and less than or equal to the to value. |
boolean |
equals(IntRange that)
|
boolean |
equals(Object that)
Determines if this object is equal to another object.
|
Integer |
get(int index) |
Integer |
getFrom()
The lower value in the range.
|
int |
getFromInt()
Gets the 'from' value as a primitive integer.
|
Boolean |
getInclusive()
Returns the inclusive flag.
|
Integer |
getTo()
The upper value in the range.
|
int |
getToInt()
Gets the 'to' value as a primitive integer.
|
int |
hashCode() |
String |
inspect() |
boolean |
isReverse()
Indicates whether this is a reverse range which iterates backwards
starting from the to value and ending on the from value
|
Iterator<Integer> |
iterator() |
int |
size() |
List<Integer> |
step(int step)
Forms a list by stepping through the range by the indicated interval.
|
void |
step(int step,
Closure closure)
Steps through the range, calling a closure for each item.
|
List<Integer> |
subList(int fromIndex,
int toIndex) |
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.
|
String |
toString() |
add, add, addAll, clear, indexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, set
addAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
add, add, addAll, addAll, clear, indexOf, isEmpty, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, sort, spliterator, toArray, toArray
parallelStream, removeIf, stream
public IntRange(int from, int to)
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.IllegalArgumentException
- if the range would contain more than Integer.MAX_VALUE
values.protected IntRange(int from, int to, boolean reverse)
IntRange
.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
.IllegalArgumentException
- if from
is greater than to
.public IntRange(boolean inclusive, int from, int to)
IntRange
.from
- the first value in the range.to
- the last value in the range.inclusive
- true
if the to value is included in the range.public <T extends Number & Comparable> NumberRange by(T stepSize)
from
and to
as this
IntRange but with a step size of stepSize
.stepSize
- the desired step sizepublic RangeInfo subListBorders(int size)
size
- the size of the aggregate being indexedpublic boolean equals(Object that)
AbstractList.equals(Object)
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(List, List)
instead of this method.
equals
in interface Collection<Integer>
equals
in interface List<Integer>
equals
in class AbstractList<Integer>
that
- the object to comparetrue
if the objects are equalpublic boolean equals(IntRange that)
that
- the object to compare for equalitytrue
if the ranges are equalpublic Integer getFrom()
Range
public Boolean getInclusive()
public int getFromInt()
public int getToInt()
public boolean isReverse()
Range
public boolean containsWithinBounds(Object o)
Range
from
value for the range and less than or equal to the to
value.
This may be true even for values not contained in the range. Example: from = 1.5, to = 3, next() increments by 1 containsWithinBounds(2) == true contains(2) == false
containsWithinBounds
in interface Range<Integer>
o
- the object to check against the boundaries of the rangetrue
if the object is between the from and to valuespublic Integer get(int index)
public int size()
size
in interface Collection<Integer>
size
in interface List<Integer>
size
in class AbstractCollection<Integer>
public String toString()
toString
in class AbstractCollection<Integer>
public String inspect()
public boolean contains(Object value)
contains
in interface Collection<Integer>
contains
in interface List<Integer>
contains
in class AbstractCollection<Integer>
public boolean containsAll(Collection other)
containsAll
in interface Collection<Integer>
containsAll
in interface List<Integer>
containsAll
in class AbstractCollection<Integer>
public void step(int step, Closure closure)
Range
public List<Integer> step(int step)
Range
public int hashCode()
hashCode
in interface Collection<Integer>
hashCode
in interface List<Integer>
hashCode
in class AbstractList<Integer>