Package groovy.lang

Class ObjectRange

All Implemented Interfaces:
Range, Iterable, Collection, List

public class ObjectRange extends AbstractList implements Range
Represents an inclusive list of objects from a value to a value using comparators.

Note: This class is similar to IntRange. If you make any changes to this class, you might consider making parallel changes to IntRange.

  • Constructor Details

    • ObjectRange

      public ObjectRange(Comparable from, Comparable to)
      Creates a new ObjectRange. Creates a reversed range if from < to.
      Parameters:
      from - the first value in the range.
      to - the last value in the range.
    • ObjectRange

      public ObjectRange(Comparable from, Comparable to, boolean reverse)
  • Method Details

    • equals

      public boolean equals(Object that)
      Specified by:
      equals in interface Collection
      Specified by:
      equals in interface List
      Overrides:
      equals in class AbstractList
    • equals

      public boolean equals(ObjectRange that)
      Compares an ObjectRange to another ObjectRange.
      Parameters:
      that - the object to check equality with
      Returns:
      true if the ranges are equal
    • getFrom

      public Comparable getFrom()
      The lower value in the range.
      Specified by:
      getFrom in interface Range
      Returns:
      the lower value in the range.
    • getTo

      public Comparable getTo()
      The upper value in the range.
      Specified by:
      getTo in interface Range
      Returns:
      the upper value in the range
    • isReverse

      public boolean isReverse()
      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
      Returns:
      true if this is a reverse range
    • get

      public Object get(int index)
      Specified by:
      get in interface List
      Specified by:
      get in class AbstractList
    • iterator

      public Iterator iterator()
      Specified by:
      iterator in interface Collection
      Specified by:
      iterator in interface Iterable
      Specified by:
      iterator in interface List
      Overrides:
      iterator in class AbstractList
    • 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
      Parameters:
      value - the value of interest
      Returns:
      true if the value is within the bounds
    • size

      public int size()
      Specified by:
      size in interface Collection
      Specified by:
      size in interface List
      Specified by:
      size in class AbstractCollection
    • subList

      public List subList(int fromIndex, int toIndex)
      Specified by:
      subList in interface List
      Overrides:
      subList in class AbstractList
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection
    • inspect

      public String inspect()
      Specified by:
      inspect in interface Range
      Returns:
      the verbose String representation of this Range as would be typed into a console to create the Range instance
    • contains

      public boolean contains(Object value)
      iterates over all values and returns true if one value matches. Also see containsWithinBounds.
      Specified by:
      contains in interface Collection
      Specified by:
      contains in interface List
      Overrides:
      contains in class AbstractCollection
    • step

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

      public List step(int step)
      Forms a list by stepping through the range by the indicated interval.
      Specified by:
      step in interface Range
      Parameters:
      step - 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.
    • increment

      protected Object increment(Object value)
      Increments by one
      Parameters:
      value - the value to increment
      Returns:
      the incremented value
    • decrement

      protected Object decrement(Object value)
      Decrements by one
      Parameters:
      value - the value to decrement
      Returns:
      the decremented value