Package: groovy.lang

## [Java] Class ObjectRange

- groovy.lang.ObjectRange

- All Implemented Interfaces and Traits:
    Range

      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 Summary

    Constructors
    Constructor and description
    `ObjectRange(Comparable from, Comparable to)` Creates a new ObjectRange.
    `ObjectRange(Comparable smaller, Comparable larger, boolean reverse)` Creates a new ObjectRange assumes smaller <= larger, else behavior is undefined.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `protected void` | `checkBoundaryCompatibility()` throws IllegalArgumentException if to and from are incompatible, meaning they e.g.
    `` | `protected int` | `compareTo(Comparable first, Comparable second)` Compares two values using Groovy's number-aware comparison.
    `` | `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
    `` | `protected Object` | `decrement(Object value)` Decrements by one
    `` | `public boolean` | `equals(Object that)` Compares the specified object with this list for equality. Returns `true` if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements `e1` and `e2` are equal if `(e1==null ? e2==null : e1.equals(e2))`.) In other words, two lists are defined to be equal if they contain the same elements in the same order. Implementation Requirements: This implementation first checks if the specified object is this list. If so, it returns `true`; if not, it checks if the specified object is a list. If not, it returns `false`; if so, it iterates over both lists, comparing corresponding pairs of elements. If any comparison returns `false`, this method returns `false`. If either iterator runs out of elements before the other it returns `false` (as the lists are of unequal length); otherwise it returns `true` when the iterations complete. Parameters: `o` - the object to be compared for equality with this list Returns: `true` if the specified object is equal to this list
    `` | `public boolean` | `equals(ObjectRange that)` Compares an ObjectRange to another ObjectRange.
    `` | `public Comparable` | `get(int index)` Returns the element at the specified position in this list. throws: IndexOutOfBoundsException if the index is out of range (`index < 0 || index >= size()`) throws: IndexOutOfBoundsException Returns the element at the specified position in this list. throws: IndexOutOfBoundsException if the index is out of range (`index < 0 || index >= size()`) Parameters: `index` - index of the element to return `index` - index of the element to return Returns: the element at the specified position in this list the element at the specified position in this list
    `` | `public Comparable` | `getFrom()` * The lower value in the range. * * Returns: the lower value in the range.
    `` | `public Comparable` | `getTo()` * The upper value in the range. * * Returns: the upper value in the range
    `` | `protected Object` | `increment(Object value)` Increments by one
    `` | `public String` | `inspect()` * Returns: the verbose String representation of this Range as would be typed into a console to create the Range instance
    `` | `public boolean` | `isReverse()` * Indicates whether this is a reverse range which iterates backwards * starting from the to value and ending on the from value * * Returns: `true` if this is a reverse range
    `` | `public Iterator<Comparable>` | `iterator()` Returns an iterator over the elements in this list in proper sequence. Implementation Requirements: This implementation returns a straightforward implementation of the iterator interface, relying on the backing list's `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. Returns: an iterator over the elements in this list in proper sequence
    `` | `public int` | `size()`
    `` | `public void` | `step(int step, Closure closure)` * Steps through the range, calling a closure for each item. * * Parameters: `step` - the amount by which to step. If negative, steps through the range backwards. * `closure` - the Closure to call
    `` | `public List<Comparable>` | `step(int step)` * Forms a list by stepping through the range by the indicated interval. * * 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.
    `` | `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:
        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.) throws: IndexOutOfBoundsException for an illegal endpoint index value (`fromIndex < 0 || toIndex > size || fromIndex > toIndex`) throws: IndexOutOfBoundsException if an endpoint index value is out of range `(fromIndex < 0 || toIndex > size)` throws: IllegalArgumentException if the endpoint indices are out of order `(fromIndex > toIndex)` Parameters: `fromIndex` - low endpoint (inclusive) of the subList `toIndex` - high endpoint (exclusive) of the subList Returns: a view of the specified range within this list Implementation Requirements: This implementation returns a list that subclasses `AbstractList`. 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.
    `` | `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. Returns: a string representation of this collection

  - ### Inherited Methods Summary

    Inherited Methods
    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`

- - ### Constructor Detail

    - #### 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.

    - #### public ObjectRange(Comparable smaller, Comparable larger, boolean reverse)

      Creates a new ObjectRange assumes smaller <= larger, else behavior is undefined. Caution: Prefer the other constructor when in doubt.

      Optimized Constructor avoiding initial computation of comparison.

  - ### Method Detail

    - #### protected void checkBoundaryCompatibility()

      throws IllegalArgumentException if to and from are incompatible, meaning they e.g. (likely) produce infinite sequences. Called at construction time, subclasses may override cautiously (using only members to and from).

    - #### protected int compareTo(Comparable first, Comparable second)

      Compares two values using Groovy's number-aware comparison.

      Parameters:
        `first` - the first value
        `second` - the second value
      Returns:
        the comparison result

    - #### public boolean contains(Object value)

      Iterates over all values and returns true if one value matches.

      See Also:
        containsWithinBounds(Object)

    - #### public boolean containsWithinBounds(Object value)

      Checks whether a value is between the from and to values of a Range

      Parameters:
        `value` - the value of interest
      Returns:
        true if the value is within the bounds

    - #### protected Object decrement(Object value)

      Decrements by one

      Parameters:
        `value` - the value to decrement
      Returns:
        the decremented value

    - #### public boolean equals(Object that)

      Compares the specified object with this list for equality. Returns `true` if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements `e1` and `e2` are equal if `(e1==null ? e2==null : e1.equals(e2))`.) In other words, two lists are defined to be equal if they contain the same elements in the same order.

      Implementation Requirements:
        This implementation first checks if the specified object is this list. If so, it returns `true`; if not, it checks if the specified object is a list. If not, it returns `false`; if so, it iterates over both lists, comparing corresponding pairs of elements. If any comparison returns `false`, this method returns `false`. If either iterator runs out of elements before the other it returns `false` (as the lists are of unequal length); otherwise it returns `true` when the iterations complete.
      Parameters:
        `o` - the object to be compared for equality with this list
      Returns:
        `true` if the specified object is equal to this list

    - #### 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

    - #### public Comparable get(int index)

      Returns the element at the specified position in this list.

      throws:
        IndexOutOfBoundsException if the index is out of range (`index < 0 || index >= size()`)
      throws:
        IndexOutOfBoundsException Returns the element at the specified position in this list.
      throws:
        IndexOutOfBoundsException if the index is out of range (`index < 0 || index >= size()`)
      Parameters:
        `index` - index of the element to return
        `index` - index of the element to return
      Returns:
        the element at the specified position in this list
        the element at the specified position in this list

    - #### public Comparable getFrom()

      * The lower value in the range. * *

      Returns:
        the lower value in the range.

    - #### public Comparable getTo()

      * The upper value in the range. * *

      Returns:
        the upper value in the range

    - #### protected Object increment(Object value)

      Increments by one

      Parameters:
        `value` - the value to increment
      Returns:
        the incremented value

    - #### public String inspect()

      *

      Returns:
        the verbose String representation of this Range as would be typed into a console to create the Range instance

    - #### public boolean isReverse()

      * Indicates whether this is a reverse range which iterates backwards * starting from the to value and ending on the from value * *

      Returns:
        `true` if this is a reverse range

    - #### public Iterator<Comparable> iterator()

      Returns an iterator over the elements in this list in proper sequence.

      Implementation Requirements:
        This implementation returns a straightforward implementation of the iterator interface, relying on the backing list's `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.

      Returns:
        an iterator over the elements in this list in proper sequence

    - #### public int size()

    - #### public void step(int step, Closure closure)

      * Steps through the range, calling a closure for each item. * *

      Parameters:
        `step` - the amount by which to step. If negative, steps through the range backwards. *
        `closure` - the Closure to call

    - #### public List<Comparable> step(int step)

      * Forms a list by stepping through the range by the indicated interval. * *

      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.

    - #### 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:

          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.)

      throws:
        IndexOutOfBoundsException for an illegal endpoint index value (`fromIndex < 0 || toIndex > size || fromIndex > toIndex`)
      throws:
        IndexOutOfBoundsException if an endpoint index value is out of range `(fromIndex < 0 || toIndex > size)`
      throws:
        IllegalArgumentException if the endpoint indices are out of order `(fromIndex > toIndex)`
      Parameters:
        `fromIndex` - low endpoint (inclusive) of the subList
        `toIndex` - high endpoint (exclusive) of the subList
      Returns:
        a view of the specified range within this list
      Implementation Requirements:
        This implementation returns a list that subclasses `AbstractList`. 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.

    - #### 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.

      Returns:
        a string representation of this collection