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.
| Modifier and Type | Class and Description |
|---|---|
| private class | IntRange.IntRangeIterator
Iterates through each number in an |
| Modifier and Type | Field and Description |
|---|---|
| private final int | from
For non-inclusive aware ranges, the first number in the range; |
| private final Boolean | inclusive
If |
| private final boolean | reverse
If |
| private static final long | |
| private final int | to
For non-inclusive aware ranges, the last number in the range; |
| Access | Constructor and Description |
|---|---|
| public | IntRange(int
the first number in the range. from, int the last number in the range. to)Creates a new non-inclusive aware |
| protected | IntRange(int
the first value in the range. from, int the last value in the range. to, boolean true if the range should count from
to to from.Creates a new non-inclusive aware |
| public | IntRange(boolean
true if the to value is included in the range.the first value in the range. from, int the last value in the range. to)Creates a new inclusive aware |
| Modifier and Type | Method and Description |
|---|---|
| public <T extends Number & Comparable | Returns: a new NumberRangethe desired step size stepSize)Creates a new NumberRange with the same |
| private void | |
| public boolean | contains(Object
element whose presence in this list is to be tested value)Overrides java. Implements java. true if this list contains the specified element.
|
| public boolean | containsAll(Collection<E>
collection to be checked for containment in this list other)Overrides java. Implements java. true if this list contains all of the elements of the
specified collection.
|
| public boolean | containsWithinBounds(Object
the object to check against the boundaries of the range o)Implements groovy. from
value for the range and less than or equal to the to value.
|
| public boolean | Returns: true if the objects are equalthe object to compare that)Overrides java. Implements java. |
| public boolean | |
| public Integer | get(int
index of the element to return index)Implements abstract java. Implements java. |
| public Integer | |
| public int | Returns: the 'from' value as a primitive integer.Gets the 'from' value as a primitive integer. |
| public Boolean | |
| public Integer | |
| public int | Returns: the 'to' value as a primitive integer.Gets the 'to' value as a primitive integer. |
| public int | hashCode()
Overrides java. Implements java. |
| public String | |
| public boolean | isReverse()
Implements groovy. |
| public Iterator | iterator()
Overrides java. Implements java. |
| public int | size()
Implements abstract java. Implements java. |
| public void | step(int
the amount by which to step. If negative, steps through the range backwards. step, Closure<V> the closure)Closure to callImplements groovy. |
| public List | step(int
the amount by which to step. If negative, steps through the range backwards. step)Implements groovy. |
| public List | subList(int
low endpoint (inclusive) of the subList fromIndex, int high endpoint (exclusive) of the subList toIndex)Overrides java. Implements java. fromIndex, inclusive, and toIndex, exclusive.
|
| public RangeInfo | Returns: the calculated range information (with 1 added to the to value, ready for providing to subListthe size of the aggregate being indexed size)A method for determining from and to information when using this IntRange to index an aggregate object of the specified size. |
| pack-priv static RangeInfo | |
| public String | toString()
Overrides java. |
| from | back to summary |
|---|---|
| private final int from For non-inclusive aware ranges, the first number in the range; | |
| inclusive | back to summary |
|---|---|
| private final Boolean inclusive If Null for non-inclusive-aware ranges (which are inclusive). If true or false, the reverse flag is discarded. | |
| reverse | back to summary |
|---|---|
| private final boolean reverse If | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| to | back to summary |
|---|---|
| private final int to For non-inclusive aware ranges, the last number in the range; | |
| IntRange | back to summary |
|---|---|
| public IntRange(int from, int to) Creates a new non-inclusive aware
| |
| IntRange | back to summary |
|---|---|
| protected IntRange(int from, int to, boolean reverse) Creates a new non-inclusive aware
| |
| IntRange | back to summary |
|---|---|
| public IntRange(boolean inclusive, int from, int to) Creates a new inclusive aware
| |
| by | back to summary |
|---|---|
| public <T extends Number & Comparable Creates a new NumberRange with the same
| |
| checkSize | back to summary |
|---|---|
| private void checkSize() | |
| contains | back to summary |
|---|---|
| public boolean contains(Object value) Overrides java. Implements java. Doc from java. Returns | |
| containsAll | back to summary |
|---|---|
| public boolean containsAll(Collection<E> other) Overrides java. Implements java. Doc from java. Returns
| |
| containsWithinBounds | back to summary |
|---|---|
| public boolean containsWithinBounds(Object o) Implements groovy. Doc from groovy. Indicates whether an object is greater than or equal to the 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 | |
| equals | back to summary |
|---|---|
| public boolean equals(Object that) Overrides java. Implements java. Determines if this object is equal to another object. Delegates to
It is not necessary to override
Note that equals is generally handled by
| |
| equals | back to summary |
|---|---|
| public boolean equals(IntRange that) Compares an
| |
| get | back to summary |
|---|---|
| public Integer get(int index) Implements abstract java. Implements java. Doc from java. Returns the element at the specified position in this list. | |
| getFrom | back to summary |
|---|---|
| public Integer getFrom() Implements groovy. Doc from groovy. The lower value in the range. | |
| getFromInt | back to summary |
|---|---|
| public int getFromInt() Gets the 'from' value as a primitive integer.
| |
| getInclusive | back to summary |
|---|---|
| public Boolean getInclusive() Returns the inclusive flag. Null for non-inclusive aware ranges or non-null for inclusive aware ranges. | |
| getTo | back to summary |
|---|---|
| public Integer getTo() Implements groovy. Doc from groovy. The upper value in the range. | |
| getToInt | back to summary |
|---|---|
| public int getToInt() Gets the 'to' value as a primitive integer.
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Implements java. Doc from java. Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
This ensures that list1.equals(list2) implies that
list1.hashCode()==list2.hashCode() for any two lists,
list1 and list2, as required by the general
contract of Object#hashCode.
| |
| inspect | back to summary |
|---|---|
| public String inspect() Implements groovy. | |
| isReverse | back to summary |
|---|---|
| public boolean isReverse() Implements groovy. Doc from groovy. Indicates whether this is a reverse range which iterates backwards starting from the to value and ending on the from value
| |
| iterator | back to summary |
|---|---|
| public Iterator Overrides java. Implements java. Doc from java. Returns an iterator over the elements in this list in proper sequence. | |
| size | back to summary |
|---|---|
| public int size() Implements abstract java. Implements java. Doc from java. Returns the number of elements in this list. If this list contains
more than
| |
| step | back to summary |
|---|---|
| public void step(int step, Closure<V> closure) Implements groovy. Doc from groovy. Steps through the range, calling a closure for each item. | |
| step | back to summary |
|---|---|
| public List Implements groovy. Doc from groovy. Forms a list by stepping through the range by the indicated interval. | |
| subList | back to summary |
|---|---|
| public List Overrides java. Implements java. Doc from java. Returns a view of the portion of this list between the specified
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
Similar idioms may be constructed for indexOf and
lastIndexOf, and all of the algorithms in the
Collections class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.) | |
| subListBorders | back to summary |
|---|---|
| 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. Normally only used internally within Groovy but useful if adding range indexing support for your own aggregates.
| |
| subListBorders | back to summary |
|---|---|
| pack-priv static RangeInfo subListBorders(int from, int to, boolean inclusive, int size) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of this collection. The string
representation consists of a list of the collection's elements in the
order they are returned by its iterator, enclosed in square brackets
(
| |
IntRange.
| Modifier and Type | Field and Description |
|---|---|
| private int | index
Counts from 0 up to size - 1. |
| private int | size
The number of values in the range. |
| private int | value
The next value to return. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public Integer | |
| public void |
| index | back to summary |
|---|---|
| private int index Counts from 0 up to size - 1. | |
| size | back to summary |
|---|---|
| private int size The number of values in the range. | |
| value | back to summary |
|---|---|
| private int value The next value to return. | |
| IntRangeIterator | back to summary |
|---|---|
| private IntRangeIterator() | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns
| |
| next | back to summary |
|---|---|
| public Integer next() Implements java. Doc from java. Returns the next element in the iteration. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. Not supported.
| |