Package groovy.lang

Class IntRange

All Implemented Interfaces:
Range<Integer>, Serializable, Iterable<Integer>, Collection<Integer>, List<Integer>

public class IntRange
extends AbstractList<Integer>
implements Range<Integer>, Serializable
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.

See Also:
Serialized Form
  • Constructor Details

    • IntRange

      public 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.
      Parameters:
      from - the first number in the range.
      to - the last number in the range.
      Throws:
      IllegalArgumentException - if the range would contain more than Integer.MAX_VALUE values.
    • IntRange

      protected IntRange​(int from, int to, boolean reverse)
      Creates a new non-inclusive aware IntRange.
      Parameters:
      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.
      Throws:
      IllegalArgumentException - if from is greater than to.
    • IntRange

      public 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 - true if the to value is included in the range.
  • Method Details