Package groovy.lang
Class ObjectRange
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<java.lang.Comparable>
groovy.lang.ObjectRange
- All Implemented Interfaces:
Range<java.lang.Comparable>
,java.lang.Iterable<java.lang.Comparable>
,java.util.Collection<java.lang.Comparable>
,java.util.List<java.lang.Comparable>
public class ObjectRange extends java.util.AbstractList<java.lang.Comparable> implements Range<java.lang.Comparable>
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
Constructors Constructor Description ObjectRange(java.lang.Comparable from, java.lang.Comparable to)
Creates a newObjectRange
.ObjectRange(java.lang.Comparable smaller, java.lang.Comparable larger, boolean reverse)
Creates a newObjectRange
assumes smaller <= larger, else behavior is undefined. -
Method Summary
Modifier and Type Method Description protected void
checkBoundaryCompatibility()
throws IllegalArgumentException if to and from are incompatible, meaning they e.g.protected int
compareTo(java.lang.Comparable first, java.lang.Comparable second)
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 Rangeprotected java.lang.Object
decrement(java.lang.Object value)
Decrements by oneboolean
equals(ObjectRange that)
Compares anObjectRange
to anotherObjectRange
.boolean
equals(java.lang.Object that)
java.lang.Comparable
get(int index)
java.lang.Comparable
getFrom()
The lower value in the range.java.lang.Comparable
getTo()
The upper value in the range.protected java.lang.Object
increment(java.lang.Object value)
Increments by onejava.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 valuejava.util.Iterator<java.lang.Comparable>
iterator()
int
size()
java.util.List<java.lang.Comparable>
step(int step)
Forms a list by stepping through the range by the indicated interval.void
step(int step, Closure closure)
Steps through the range, calling a closure for each item.java.util.List<java.lang.Comparable>
subList(int fromIndex, int toIndex)
java.lang.String
toString()
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, hashCode, 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, hashCode, indexOf, isEmpty, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, sort, spliterator, toArray, toArray
-
Constructor Details
-
ObjectRange
public ObjectRange(java.lang.Comparable from, java.lang.Comparable to)- Parameters:
from
- the first value in the range.to
- the last value in the range.
-
ObjectRange
public ObjectRange(java.lang.Comparable smaller, java.lang.Comparable larger, boolean reverse)Creates a newObjectRange
assumes smaller <= larger, else behavior is undefined. Caution: Prefer the other constructor when in doubt.Optimized Constructor avoiding initial computation of comparison.
-
-
Method Details
-
checkBoundaryCompatibility
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). -
equals
public boolean equals(java.lang.Object that)- Specified by:
equals
in interfacejava.util.Collection<java.lang.Comparable>
- Specified by:
equals
in interfacejava.util.List<java.lang.Comparable>
- Overrides:
equals
in classjava.util.AbstractList<java.lang.Comparable>
-
equals
Compares anObjectRange
to anotherObjectRange
.- Parameters:
that
- the object 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. -
getTo
public java.lang.Comparable getTo()Description copied from interface:Range
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 -
get
public java.lang.Comparable get(int index)- Specified by:
get
in interfacejava.util.List<java.lang.Comparable>
- Specified by:
get
in classjava.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 interfaceRange<java.lang.Comparable>
- Parameters:
value
- the value of interest- Returns:
- true if the value is within the bounds
-
compareTo
protected int compareTo(java.lang.Comparable first, java.lang.Comparable second) -
size
public int size()- Specified by:
size
in interfacejava.util.Collection<java.lang.Comparable>
- Specified by:
size
in interfacejava.util.List<java.lang.Comparable>
- Specified by:
size
in classjava.util.AbstractCollection<java.lang.Comparable>
-
subList
public java.util.List<java.lang.Comparable> subList(int fromIndex, int toIndex)- Specified by:
subList
in interfacejava.util.List<java.lang.Comparable>
- Overrides:
subList
in classjava.util.AbstractList<java.lang.Comparable>
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.util.AbstractCollection<java.lang.Comparable>
-
inspect
public java.lang.String inspect() -
contains
public boolean contains(java.lang.Object value)Iterates over all values and returns true if one value matches.- Specified by:
contains
in interfacejava.util.Collection<java.lang.Comparable>
- Specified by:
contains
in interfacejava.util.List<java.lang.Comparable>
- Overrides:
contains
in classjava.util.AbstractCollection<java.lang.Comparable>
- See Also:
containsWithinBounds(Object)
-
step
Description copied from interface:Range
Steps through the range, calling a closure for each item. -
iterator
public java.util.Iterator<java.lang.Comparable> iterator()- Specified by:
iterator
in interfacejava.util.Collection<java.lang.Comparable>
- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Comparable>
- Specified by:
iterator
in interfacejava.util.List<java.lang.Comparable>
- Overrides:
iterator
in classjava.util.AbstractList<java.lang.Comparable>
-
step
public java.util.List<java.lang.Comparable> step(int step)Description copied from interface:Range
Forms a list by stepping through the range by the indicated interval. -
increment
protected java.lang.Object increment(java.lang.Object value)Increments by one- Parameters:
value
- the value to increment- Returns:
- the incremented value
-
decrement
protected java.lang.Object decrement(java.lang.Object value)Decrements by one- Parameters:
value
- the value to decrement- Returns:
- the decremented value
-