Package groovy.lang

# Class NumberRange

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<java.lang.Comparable>
groovy.lang.NumberRange

All Implemented Interfaces:
  `Range<java.lang.Comparable>`, `java.io.Serializable`, `java.lang.Iterable<java.lang.Comparable>`, `java.util.Collection<java.lang.Comparable>`, `java.util.List<java.lang.Comparable>`

    public class NumberRange
    extends java.util.AbstractList<java.lang.Comparable>
    implements Range<java.lang.Comparable>, java.io.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.

Since:
  2.5.0
See Also:
  Serialized Form

- ## Field Summary

  ### Fields inherited from class java.util.AbstractList

  `modCount`

- ## Constructor Summary

  Constructors
  Constructor | 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`.

- ## Method Summary

  Modifier and Type | Method | Description
  --- | --- | ---
  `<T extends java.lang.Number & java.lang.Comparable> 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`.
  `boolean` | `contains​(java.lang.Object value)` | iterates over all values and returns true if one value matches.
  `boolean` | `containsWithinBounds​(java.lang.Object value)` | Checks whether a value is between the from and to values of a Range
  `boolean` | `equals​(java.lang.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.
  `boolean` | `fastEquals​(NumberRange that)` | Compares a `NumberRange` to another `NumberRange` using only a strict comparison of the NumberRange properties.
  `java.lang.Comparable` | `get​(int index)` |
  `java.lang.Comparable` | `getFrom()` | The lower value in the range.
  `java.lang.Comparable` | `getStepSize()` |
  `java.lang.Comparable` | `getTo()` | The upper value in the range.
  `int` | `hashCode()` | A NumberRange's hashCode is based on hashCode values of the discrete items it represents.
  `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.Comparable>` | `iterator()`
  `int` | `size()` |
  `java.util.List<java.lang.Comparable>` | `step​(int numSteps)` | Forms a list by stepping through the range by the indicated interval.
  `void` | `step​(int numSteps, Closure closure)` | Steps through the range, calling a closure for each item.
  `java.util.List<java.lang.Comparable>` | `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()` |

  ### Methods inherited from class java.util.AbstractList

  `add, add, addAll, clear, indexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, set`

  ### Methods inherited from class java.util.AbstractCollection

  `addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray`

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface java.util.Collection

  `parallelStream, removeIf, stream, toArray`

  ### Methods inherited from interface java.lang.Iterable

  `forEach`

  ### Methods inherited from interface java.util.List

  `add, add, addAll, addAll, clear, containsAll, indexOf, isEmpty, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, sort, spliterator, toArray, toArray`

- ## Constructor Details

  - ### NumberRange

    public NumberRange​(T from, U to)
    Creates an inclusive `NumberRange` with step size 1. Creates a reversed range if `from` < `to`.
    Parameters:
      `from` - the first value in the range
      `to` - the last value in the range

  - ### NumberRange

    public NumberRange​(T from, U to, boolean inclusive)
    Creates a new `NumberRange` with step size 1. Creates a reversed range if `from` < `to`.
    Parameters:
      `from` - start of the range
      `to` - end of the range
      `inclusive` - whether the range is inclusive

  - ### NumberRange

    public NumberRange​(T from, U to, V stepSize)
    Creates an inclusive `NumberRange`. Creates a reversed range if `from` < `to`.
    Parameters:
      `from` - start of the range
      `to` - end of the range
      `stepSize` - the gap between discrete elements in the range

  - ### NumberRange

    public NumberRange​(T from, U to, V stepSize, boolean inclusive)
    Creates a `NumberRange`. Creates a reversed range if `from` < `to`.
    Parameters:
      `from` - start of the range
      `to` - end of the range
      `stepSize` - the gap between discrete elements in the range
      `inclusive` - whether the range is inclusive

- ## Method Details

  - ### subListBorders

    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

  - ### by

    public <T extends java.lang.Number & java.lang.Comparable> 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`.
    Parameters:
      `stepSize` - the desired step size
    Returns:
      a new NumberRange

  - ### equals

    public boolean equals​(java.lang.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.
    Specified by:
      `equals` in interface `java.util.Collection<java.lang.Comparable>`
    Specified by:
      `equals` in interface `java.util.List<java.lang.Comparable>`
    Overrides:
      `equals` in class `java.util.AbstractList<java.lang.Comparable>`
    Parameters:
      `that` - the object to be compared for equality with this NumberRange
    Returns:
      `true` if the specified object is equal to this NumberRange
    See Also:
      `fastEquals(NumberRange)`

  - ### hashCode

    public int hashCode()
    A NumberRange's hashCode is based on hashCode values of the discrete items it represents.
    Specified by:
      `hashCode` in interface `java.util.Collection<java.lang.Comparable>`
    Specified by:
      `hashCode` in interface `java.util.List<java.lang.Comparable>`
    Overrides:
      `hashCode` in class `java.util.AbstractList<java.lang.Comparable>`
    Returns:
      the hashCode value

  - ### fastEquals

    public boolean fastEquals​(NumberRange that)
    Compares 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.
    Parameters:
      `that` - the NumberRange to check equality with
    Returns:
      `true` if the ranges are equal

  - ### getFrom

    public java.lang.Comparable getFrom()
    Description copied from interface: `Range`
    The lower value in the range.
    Specified by:
      `getFrom` in interface `Range<java.lang.Comparable>`
    Returns:
      the lower value in the range.

  - ### getTo

    public java.lang.Comparable getTo()
    Description copied from interface: `Range`
    The upper value in the range.
    Specified by:
      `getTo` in interface `Range<java.lang.Comparable>`
    Returns:
      the upper value in the range

  - ### getStepSize

    public java.lang.Comparable getStepSize()

  - ### isReverse

    public boolean isReverse()
    Description copied from interface: `Range`
    Indicates whether this is a reverse range which iterates backwards starting from the to value and ending on the from value
    Specified by:
      `isReverse` in interface `Range<java.lang.Comparable>`
    Returns:
      `true` if this is a reverse range

  - ### get

    public java.lang.Comparable get​(int index)
    Specified by:
      `get` in interface `java.util.List<java.lang.Comparable>`
    Specified by:
      `get` in class `java.util.AbstractList<java.lang.Comparable>`

  - ### containsWithinBounds

    public boolean containsWithinBounds​(java.lang.Object value)
    Checks whether a value is between the from and to values of a Range
    Specified by:
      `containsWithinBounds` in interface `Range<java.lang.Comparable>`
    Parameters:
      `value` - the value of interest
    Returns:
      true if the value is within the bounds

  - ### size

    public int size()
    Specified by:
      `size` in interface `java.util.Collection<java.lang.Comparable>`
    Specified by:
      `size` in interface `java.util.List<java.lang.Comparable>`
    Specified by:
      `size` in class `java.util.AbstractCollection<java.lang.Comparable>`

  - ### subList

    public java.util.List<java.lang.Comparable> subList​(int fromIndex, int toIndex)
    Specified by:
      `subList` in interface `java.util.List<java.lang.Comparable>`
    Overrides:
      `subList` in class `java.util.AbstractList<java.lang.Comparable>`

  - ### toString

    public java.lang.String toString()
    Overrides:
      `toString` in class `java.util.AbstractCollection<java.lang.Comparable>`

  - ### inspect

    public java.lang.String inspect()
    Specified by:
      `inspect` in interface `Range<java.lang.Comparable>`
    Returns:
      the verbose `String` representation of this `Range` as would be typed into a console to create the `Range` instance

  - ### contains

    public boolean contains​(java.lang.Object value)
    iterates over all values and returns true if one value matches. Also see containsWithinBounds.
    Specified by:
      `contains` in interface `java.util.Collection<java.lang.Comparable>`
    Specified by:
      `contains` in interface `java.util.List<java.lang.Comparable>`
    Overrides:
      `contains` in class `java.util.AbstractCollection<java.lang.Comparable>`

  - ### step

    public void step​(int numSteps, Closure closure)
    Steps through the range, calling a closure for each item.
    Specified by:
      `step` in interface `Range<java.lang.Comparable>`
    Parameters:
      `numSteps` - the amount by which to step. If negative, steps through the range backwards.
      `closure` - the `Closure` to call

  - ### iterator

    public java.util.Iterator<java.lang.Comparable> iterator()
    Specified by:
      `iterator` in interface `java.util.Collection<java.lang.Comparable>`
    Specified by:
      `iterator` in interface `java.lang.Iterable<java.lang.Comparable>`
    Specified by:
      `iterator` in interface `java.util.List<java.lang.Comparable>`
    Overrides:
      `iterator` in class `java.util.AbstractList<java.lang.Comparable>`

  - ### step

    public java.util.List<java.lang.Comparable> step​(int numSteps)
    Description copied from interface: `Range`
    Forms a list by stepping through the range by the indicated interval.
    Specified by:
      `step` in interface `Range<java.lang.Comparable>`
    Parameters:
      `numSteps` - the amount by which to step. If negative, steps through the range backwards.
    Returns:
      the list formed by stepping through the range by the indicated interval.