Package groovy.lang
Class ObjectRange
- All Implemented Interfaces:
Range<Comparable>
,Iterable<Comparable>
,Collection<Comparable>
,List<Comparable>
public class ObjectRange extends AbstractList<Comparable> implements Range<Comparable>
-
Field Summary
-
Constructor Summary
Constructors Constructor Description ObjectRange(Comparable from, Comparable to)
Creates a newObjectRange
.ObjectRange(Comparable smaller, 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(Comparable first, Comparable second)
boolean
contains(Object value)
Iterates over all values and returns true if one value matches.boolean
containsWithinBounds(Object value)
Checks whether a value is between the from and to values of a Rangeprotected Object
decrement(Object value)
Decrements by oneboolean
equals(ObjectRange that)
Compares anObjectRange
to anotherObjectRange
.boolean
equals(Object that)
Comparable
get(int index)
Comparable
getFrom()
The lower value in the range.Comparable
getTo()
The upper value in the range.protected Object
increment(Object value)
Increments by oneString
inspect()
boolean
isReverse()
Indicates whether this is a reverse range which iterates backwards starting from the to value and ending on the from valueIterator<Comparable>
iterator()
int
size()
List<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.List<Comparable>
subList(int fromIndex, int toIndex)
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.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
- Parameters:
from
- the first value in the range.to
- the last value in the range.
-
ObjectRange
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
- Specified by:
equals
in interfaceCollection<Comparable>
- Specified by:
equals
in interfaceList<Comparable>
- Overrides:
equals
in classAbstractList<Comparable>
-
equals
Compares anObjectRange
to anotherObjectRange
.- Parameters:
that
- the object to check equality with- Returns:
true
if the ranges are equal
-
getFrom
Description copied from interface:Range
The lower value in the range.- Specified by:
getFrom
in interfaceRange<Comparable>
- Returns:
- the lower value in the range.
-
getTo
Description copied from interface:Range
The upper value in the range.- Specified by:
getTo
in interfaceRange<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 interfaceRange<Comparable>
- Returns:
true
if this is a reverse range
-
get
- Specified by:
get
in interfaceList<Comparable>
- Specified by:
get
in classAbstractList<Comparable>
-
containsWithinBounds
Checks whether a value is between the from and to values of a Range- Specified by:
containsWithinBounds
in interfaceRange<Comparable>
- Parameters:
value
- the value of interest- Returns:
- true if the value is within the bounds
-
compareTo
-
size
public int size()- Specified by:
size
in interfaceCollection<Comparable>
- Specified by:
size
in interfaceList<Comparable>
- Specified by:
size
in classAbstractCollection<Comparable>
-
subList
- Specified by:
subList
in interfaceList<Comparable>
- Overrides:
subList
in classAbstractList<Comparable>
-
toString
- Overrides:
toString
in classAbstractCollection<Comparable>
-
inspect
-
contains
Iterates over all values and returns true if one value matches.- Specified by:
contains
in interfaceCollection<Comparable>
- Specified by:
contains
in interfaceList<Comparable>
- Overrides:
contains
in classAbstractCollection<Comparable>
- See Also:
containsWithinBounds(Object)
-
step
Description copied from interface:Range
Steps through the range, calling a closure for each item.- Specified by:
step
in interfaceRange<Comparable>
- Parameters:
step
- the amount by which to step. If negative, steps through the range backwards.closure
- theClosure
to call
-
iterator
- Specified by:
iterator
in interfaceCollection<Comparable>
- Specified by:
iterator
in interfaceIterable<Comparable>
- Specified by:
iterator
in interfaceList<Comparable>
- Overrides:
iterator
in classAbstractList<Comparable>
-
step
Description copied from interface:Range
Forms a list by stepping through the range by the indicated interval.- Specified by:
step
in interfaceRange<Comparable>
- 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
Increments by one- Parameters:
value
- the value to increment- Returns:
- the incremented value
-
decrement
Decrements by one- Parameters:
value
- the value to decrement- Returns:
- the decremented value
-