Package groovy.lang

Class NumberRange

  • All Implemented Interfaces:
    Range<Comparable>, Iterable<Comparable>, Collection<Comparable>, List<Comparable>

    public class NumberRange
    extends AbstractList<Comparable>
    implements Range<Comparable>
    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
    • Constructor Detail

      • 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 Detail

      • 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 Number & ComparableNumberRange 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​(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 Collection<Comparable>
        Specified by:
        equals in interface List<Comparable>
        Overrides:
        equals in class AbstractList<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)
      • 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 Comparable getFrom()
        Description copied from interface: Range
        The lower value in the range.
        Specified by:
        getFrom in interface Range<Comparable>
        Returns:
        the lower value in the range.
      • getTo

        public Comparable getTo()
        Description copied from interface: Range
        The upper value in the range.
        Specified by:
        getTo in interface Range<Comparable>
        Returns:
        the upper value in the range
      • 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<Comparable>
        Returns:
        true if this is a reverse range
      • containsWithinBounds

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

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

        public List<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<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.