[Java] Class IntRange
- groovy.lang.IntRange
- All Implemented Interfaces and Traits:
- Range
public class IntRange extends AbstractList
Represents a list of Integer objects starting at 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 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.
Methods Summary
| Type Params | Return Type | Name and description |
|---|---|---|
def |
IntRange(int from, int to)Creates a new non-inclusive aware IntRange. | |
protected def |
IntRange(int from, int to, boolean reverse)Creates a new non-inclusive aware IntRange. | |
def |
IntRange(boolean inclusive, int from, int to)Creates a new inclusive aware IntRange. | |
<T extends def> | NumberRange |
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) | |
boolean |
equals(Object that)Determines if this object is equal to another object. | |
boolean |
equals(IntRange that)Compares an IntRange to another IntRange. | |
Integer |
get(int index) | |
Integer |
getFrom() | |
int |
getFromInt()Gets the 'from' value as a primitive integer. | |
Boolean |
getInclusive()Returns the inclusive flag. | |
Integer |
getTo() | |
int |
getToInt()Gets the 'to' value as a primitive integer. | |
int |
hashCode() | |
String |
inspect() | |
boolean |
isReverse() | |
Iterator<Integer> |
iterator() | |
int |
size() | |
void |
step(int step, Closure closure) | |
List<Integer> |
step(int step) | |
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() |
Inherited Methods Summary
| Methods inherited from class | Name |
|---|---|
class AbstractList | add, add, remove, get, equals, hashCode, indexOf, clear, iterator, lastIndexOf, subList, addAll, set, listIterator, listIterator, remove, toString, contains, isEmpty, size, toArray, toArray, addAll, containsAll, removeAll, retainAll, wait, wait, wait, getClass, notify, notifyAll, stream, removeIf, parallelStream, forEach, replaceAll, size, spliterator, sort |
Method Detail
public def IntRange(int from, int to)
Creates a new non-inclusive aware IntRange. If from is greater than to, a reverse range is created with from and to swapped.
- throws:
- IllegalArgumentException if the range would contain more than Integer.MAX_VALUE values.
- Parameters:
-
from- the first number in the range. -
to- the last number in the range.
protected def IntRange(int from, int to, boolean reverse)
Creates a new non-inclusive aware IntRange.
- throws:
- IllegalArgumentException if
fromis greater thanto.
- Parameters:
-
from- the first value in the range. -
to- the last value in the range. -
reverse-trueif the range should count fromtotofrom.
public def IntRange(boolean inclusive, int from, int to)
Creates a new inclusive aware IntRange.
- Parameters:
-
from- the first value in the range. -
to- the last value in the range. -
inclusive-trueif the to value is included in the range.
<T extends def> 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.
- Parameters:
-
stepSize- the desired step size
- Returns:
- a new NumberRange
- Since:
- 2.5.0
@Override public boolean contains(Object value)
@Override public boolean containsAll(Collection other)
@Override public boolean containsWithinBounds(Object o)
public boolean equals(Object that)
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.
- Parameters:
-
that- the object to compare
- Returns:
-
trueif the objects are equal
public boolean equals(IntRange that)
Compares an IntRange to another IntRange.
- Parameters:
-
that- the object to compare for equality
- Returns:
-
trueif the ranges are equal
@Override public Integer get(int index)
@Override public Integer getFrom()
public int getFromInt()
Gets the 'from' value as a primitive integer.
- Returns:
- the 'from' value as a primitive integer.
public Boolean getInclusive()
Returns the inclusive flag. Null for non-inclusive aware ranges or non-null for inclusive aware ranges.
@Override public Integer getTo()
public int getToInt()
Gets the 'to' value as a primitive integer.
- Returns:
- the 'to' value as a primitive integer.
@Override public int hashCode()
@Override public String inspect()
@Override public boolean isReverse()
@Override public Iterator<Integer> iterator()
@Override public int size()
@Override public void step(int step, Closure closure)
@Override public List<Integer> step(int step)
@Override 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. Normally only used internally within Groovy but useful if adding range indexing support for your own aggregates.
- Parameters:
-
size- the size of the aggregate being indexed
- Returns:
- the calculated range information (with 1 added to the to value, ready for providing to subList
public String toString()
© 2003-2020 The Apache Software Foundation
Licensed under the Apache license.
https://docs.groovy-lang.org/2.5.14/html/gapi/groovy/lang/IntRange.html