public class IntRange extends java.util.AbstractList<java.lang.Integer> implements Range<java.lang.Integer>
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
.
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.
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
IntRange . |
protected |
IntRange(int from,
int to,
boolean reverse)
Creates a new non-inclusive aware
IntRange . |
Modifier and Type | Method and Description |
---|---|
boolean |
contains(java.lang.Object value) |
boolean |
containsAll(java.util.Collection other) |
boolean |
containsWithinBounds(java.lang.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(java.lang.Object that)
Determines if this object is equal to another object.
|
java.lang.Integer |
get(int index) |
java.lang.Integer |
getFrom()
The lower value in the range.
|
int |
getFromInt()
Gets the 'from' value as a primitive integer.
|
java.lang.Boolean |
getInclusive()
Returns the inclusive flag.
|
java.lang.Integer |
getTo()
The upper value in the range.
|
int |
getToInt()
Gets the 'to' value as a primitive integer.
|
int |
hashCode() |
java.lang.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
|
java.util.Iterator<java.lang.Integer> |
iterator() |
int |
size() |
java.util.List<java.lang.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 number.
|
java.util.List<java.lang.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.
|
java.lang.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
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.java.lang.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
.java.lang.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 RangeInfo subListBorders(int size)
size
- the size of the aggregate being indexedpublic boolean equals(java.lang.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 java.util.Collection<java.lang.Integer>
equals
in interface java.util.List<java.lang.Integer>
equals
in class java.util.AbstractList<java.lang.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 java.lang.Integer getFrom()
Range
public java.lang.Integer getTo()
Range
public java.lang.Boolean getInclusive()
public int getFromInt()
public int getToInt()
public boolean isReverse()
Range
public boolean containsWithinBounds(java.lang.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<java.lang.Integer>
o
- the object to check against the boundaries of the rangetrue
if the object is between the from and to valuespublic java.lang.Integer get(int index)
get
in interface java.util.List<java.lang.Integer>
get
in class java.util.AbstractList<java.lang.Integer>
public int size()
size
in interface java.util.Collection<java.lang.Integer>
size
in interface java.util.List<java.lang.Integer>
size
in class java.util.AbstractCollection<java.lang.Integer>
public java.util.Iterator<java.lang.Integer> iterator()
iterator
in interface java.lang.Iterable<java.lang.Integer>
iterator
in interface java.util.Collection<java.lang.Integer>
iterator
in interface java.util.List<java.lang.Integer>
iterator
in class java.util.AbstractList<java.lang.Integer>
public java.util.List<java.lang.Integer> subList(int fromIndex, int toIndex)
subList
in interface java.util.List<java.lang.Integer>
subList
in class java.util.AbstractList<java.lang.Integer>
public java.lang.String toString()
toString
in class java.util.AbstractCollection<java.lang.Integer>
public java.lang.String inspect()
public boolean contains(java.lang.Object value)
contains
in interface java.util.Collection<java.lang.Integer>
contains
in interface java.util.List<java.lang.Integer>
contains
in class java.util.AbstractCollection<java.lang.Integer>
public boolean containsAll(java.util.Collection other)
containsAll
in interface java.util.Collection<java.lang.Integer>
containsAll
in interface java.util.List<java.lang.Integer>
containsAll
in class java.util.AbstractCollection<java.lang.Integer>
public void step(int step, Closure closure)
Range
public java.util.List<java.lang.Integer> step(int step)
Range
public int hashCode()
hashCode
in interface java.util.Collection<java.lang.Integer>
hashCode
in interface java.util.List<java.lang.Integer>
hashCode
in class java.util.AbstractList<java.lang.Integer>