public class NumberRange
extends AbstractList
implements Range, Serializable
Represents an immutable list of Numbers from a value to a value with a particular step size. In general, it isn't recommended using a NumberRange as a key to a map. The range 0..3 is deemed to be equal to 0.0..3.0 but they have different hashCode values, so storing a value using one of these ranges couldn't be retrieved using the other.
| Constructor and description |
|---|
NumberRange(T from, U to)Creates an inclusive NumberRange with step size 1. |
NumberRange(T from, U to, boolean inclusive)Creates a new NumberRange with step size 1. |
NumberRange(T from, U to, V stepSize)Creates an inclusive NumberRange. |
NumberRange(T from, U to, V stepSize, boolean inclusive)Creates a NumberRange. |
NumberRange(T from, U to, boolean inclusiveLeft, boolean inclusiveRight)Creates a NumberRange. |
NumberRange(T from, U to, V stepSize, boolean inclusiveLeft, boolean inclusiveRight)Creates a NumberRange. |
| Type Params | Return Type | Name and description |
|---|---|---|
<T extends Number & Comparable> |
public NumberRange |
by(T stepSize)For a NumberRange with step size 1, creates a new NumberRange with the same from and to as this NumberRange
but with a step size of stepSize. |
|
public boolean |
contains(Object value)iterates over all values and returns true if one value matches. |
|
public boolean |
containsWithinBounds(Object value)Checks whether a value is between the from and to values of a Range |
|
public boolean |
equals(Object that)An object is deemed equal to this NumberRange if it represents a List of items and those items equal the list of discrete items represented by this NumberRange. |
|
public boolean |
fastEquals(NumberRange that)Compares a NumberRange to another NumberRange using only a strict comparison of the NumberRange properties. |
|
public Comparable |
get(int index)Returns the element at the specified position in this list.
|
|
public Comparable |
getFrom()* The lower value in the range. * *
|
|
public Comparable |
getStepSize()Returns the increment used when traversing this range. |
|
public Comparable |
getTo()* The upper value in the range. * *
|
|
public int |
hashCode()A NumberRange's hashCode is based on hashCode values of the discrete items it represents. |
|
public String |
inspect()* |
|
public boolean |
isReverse()* Indicates whether this is a reverse range which iterates backwards * starting from the to value and ending on the from value * *
|
|
public Iterator<Comparable> |
iterator()Returns an iterator over the elements in this list in proper sequence.
|
|
public int |
size() |
|
public void |
step(int numSteps, Closure closure)* Steps through the range, calling a closure for each item. * *
|
|
public List<Comparable> |
step(int numSteps)* Forms a list by stepping through the range by the indicated interval. * *
|
|
public List<Comparable> |
subList(int fromIndex, int toIndex)Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If
fromIndex and toIndex are equal, the returned list is
empty.) The returned list is backed by this list, so non-structural
changes in the returned list are reflected in this list, and vice-versa.
The returned list supports all of the optional list operations supported
by this list.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.)
|
|
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()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 ( "[]"). Adjacent elements are separated by the characters
", " (comma and space). Elements are converted to strings as
by String.valueOf.
|
| Methods inherited from class | Name |
|---|---|
class AbstractList |
add, add, addAll, addAll, clear, contains, containsAll, equals, forEach, get, getClass, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, notify, notifyAll, parallelStream, remove, remove, removeAll, removeIf, replaceAll, retainAll, set, size, sort, spliterator, stream, subList, toArray, toArray, toArray, toString, wait, wait, wait |
Creates an inclusive NumberRange with step size 1.
Creates a reversed range if from < to.
from - the first value in the rangeto - the last value in the range Creates a new NumberRange with step size 1.
Creates a reversed range if from < to.
from - start of the rangeto - end of the rangeinclusive - whether the range is inclusive Creates an inclusive NumberRange.
Creates a reversed range if from < to.
from - start of the rangeto - end of the rangestepSize - the gap between discrete elements in the range Creates a NumberRange.
Creates a reversed range if from < to.
from - start of the rangeto - end of the rangestepSize - the gap between discrete elements in the rangeinclusive - whether the range is inclusive (upper bound) Creates a NumberRange.
Creates a reversed range if from < to.
from - start of the rangeto - end of the rangeinclusiveLeft - whether the range is includes the lower boundinclusiveRight - whether the range is includes the upper bound Creates a NumberRange.
Creates a reversed range if from < to.
from - start of the rangeto - end of the rangestepSize - the gap between discrete elements in the rangeinclusiveLeft - whether the range is includes the lower boundinclusiveRight - whether the range is includes the upper bound For a NumberRange with step size 1, creates a new NumberRange with the same
from and to as this NumberRange
but with a step size of stepSize.
stepSize - the desired step sizeiterates over all values and returns true if one value matches. Also see containsWithinBounds.
Checks whether a value is between the from and to values of a Range
value - the value of interestAn object is deemed equal to this NumberRange if it represents a List of items and those items equal the list of discrete items represented by this NumberRange.
that - the object to be compared for equality with this NumberRangetrue if the specified object is equal to this NumberRangeCompares a NumberRange to another NumberRange using only a strict comparison of the NumberRange properties. This won't return true for some ranges which represent the same discrete items, use equals instead for that but will be much faster for large lists.
that - the NumberRange to check equality withtrue if the ranges are equalReturns the element at the specified position in this list.
index < 0 || index >= size())index < 0 || index >= size())index - index of the element to returnindex - index of the element to return* The lower value in the range. * *
Returns the increment used when traversing this range.
* The upper value in the range. * *
A NumberRange's hashCode is based on hashCode values of the discrete items it represents.
*
* Indicates whether this is a reverse range which iterates backwards * starting from the to value and ending on the from value * *
true if this is a reverse rangeReturns an iterator over the elements in this list in proper sequence.
size(),
get(int), and remove(int) methods.
Note that the iterator returned by this method will throw an
UnsupportedOperationException in response to its
remove method unless the list's remove(int) method is
overridden.
This implementation can be made to throw runtime exceptions in the face of concurrent modification, as described in the specification for the (protected) modCount field.
* Steps through the range, calling a closure for each item. * *
step - the amount by which to step. If negative, steps through the range backwards.
*closure - the Closure to call* Forms a list by stepping through the range by the indicated interval. * *
step - the amount by which to step. If negative, steps through the range backwards.
* Returns a view of the portion of this list between the specified
fromIndex, inclusive, and toIndex, exclusive. (If
fromIndex and toIndex are equal, the returned list is
empty.) The returned list is backed by this list, so non-structural
changes in the returned list are reflected in this list, and vice-versa.
The returned list supports all of the optional list operations supported
by this list.
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:
list.subList(from, to).clear();
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.)
fromIndex < 0 || toIndex > size ||
fromIndex > toIndex)(fromIndex < 0 || toIndex > size)(fromIndex > toIndex)fromIndex - low endpoint (inclusive) of the subListtoIndex - high endpoint (exclusive) of the subListAbstractList. The subclass stores, in private fields, the
size of the subList (which can change over its lifetime), and the
expected modCount value of the backing list. There are two
variants of the subclass, one of which implements RandomAccess.
If this list implements RandomAccess the returned list will
be an instance of the subclass that implements RandomAccess.
The subclass's set(int, E), get(int),
add(int, E), remove(int), addAll(int,
Collection) and removeRange(int, int) methods all
delegate to the corresponding methods on the backing abstract list,
after bounds-checking the index and adjusting for the offset. The
addAll(Collection c) method merely returns addAll(size,
c).
The listIterator(int) method returns a "wrapper object"
over a list iterator on the backing list, which is created with the
corresponding method on the backing list. The iterator method
merely returns listIterator(), and the size method
merely returns the subclass's size field.
All methods first check to see if the actual modCount of
the backing list is equal to its expected value, and throw a
ConcurrentModificationException if it is not.
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 indexed 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
("[]"). Adjacent elements are separated by the characters
", " (comma and space). Elements are converted to strings as
by String.valueOf.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.