Top Description Inners Fields Constructors Methods
groovy.lang

public Class ObjectRange

extends AbstractList<Comparable<T>>
implements Range<Comparable<T>>
Class Inheritance
All Implemented Interfaces
groovy.lang.Range, java.util.List, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable
Imports
org.codehaus.groovy.runtime.DefaultGroovyMethods, .InvokerHelper, .IteratorClosureAdapter, .ScriptBytecodeAdapter, org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation, .NumberMath, java.math.BigDecimal, .BigInteger, java.util.AbstractList, .Iterator, .List, .NoSuchElementException

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ObjectRange.StepIterator

Non-thread-safe iterator which lazily produces the next element only on calls of hasNext() or next()

Field Summary

Modifier and TypeField and Description
private final Comparable<T>
from

The first value in the range.

private final boolean
reverse

true if the range counts backwards from to to from.

private int
size

The cached size, or -1 if not yet computed

private final Comparable<T>
to

The last value in the range.

Inherited from java.util.AbstractList:
modCount

Constructor Summary

AccessConstructor and Description
public
ObjectRange(Comparable<T>
the first value in the range.
from
,
Comparable<T>
the last value in the range.
to
)

Creates a new ObjectRange.

public
ObjectRange(Comparable<T> smaller, Comparable<T> larger, boolean reverse)

Creates a new ObjectRange assumes smaller <= larger, else behavior is undefined.

private
ObjectRange(Comparable<T>
start of the range, must no be larger than to when reverse != null
smaller
,
Comparable<T>
end of the range, must be larger than from when reverse != null
larger
,
Boolean
direction of the range. If null, causes direction to be computed (can be expensive).
reverse
)

Constructs a Range, computing reverse if not provided.

Method Summary

Modifier and TypeMethod and Description
private static boolean
protected void
checkBoundaryCompatibility()

throws IllegalArgumentException if to and from are incompatible, meaning they e.g. (likely) produce infinite sequences.

protected int
compareTo(Comparable<T> first, Comparable<T> second)

public boolean
contains(Object
element whose presence in this list is to be tested
value
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.List.contains, java.util.Collection.contains.

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

Returns:

true if the value is within the bounds
containsWithinBounds
(Object
the value of interest
value
)

Implements groovy.lang.Range.containsWithinBounds.

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

Returns:

the decremented value
decrement
(Object
the value to decrement
value
)

Decrements by one

public boolean
equals(Object
the object to be compared for equality with this list
that
)

Overrides java.util.AbstractList.equals.

Implements java.util.List.equals, java.util.Collection.equals.

Compares the specified object with this list for equality.
public boolean

Returns:

true if the ranges are equal
equals
(ObjectRange
the object to check equality with
that
)

Compares an ObjectRange to another ObjectRange.

public Comparable<T>
get(int
index of the element to return
index
)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

Returns the element at the specified position in this list.
public Comparable<T>
getFrom()

Implements groovy.lang.Range.getFrom.

The lower value in the range.
public Comparable<T>
getTo()

Implements groovy.lang.Range.getTo.

The upper value in the range.
protected Object

Returns:

the incremented value
increment
(Object
the value to increment
value
)

Increments by one

public String
public boolean
isReverse()

Implements groovy.lang.Range.isReverse.

Indicates whether this is a reverse range which iterates backwards starting from the to value and ending on the from value
public Iterator<Comparable<T>>
iterator()

Overrides java.util.AbstractList.iterator.

Implements java.util.List.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this list in proper sequence.
private static Comparable<T>
normaliseStringType(final Comparable<T> operand)

if operand is a Character or a String with one character, return that character's int value.

private void
setSize(int size)

protection against calls from Groovy

public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Returns the number of elements in this list.
public void
step(int
the amount by which to step. If negative, steps through the range backwards.
step
,
Closure<V>
the Closure to call
closure
)

Implements groovy.lang.Range.step.

Steps through the range, calling a closure for each item.
public List<Comparable<T>>
step(int
the amount by which to step. If negative, steps through the range backwards.
step
)

Implements groovy.lang.Range.step.

Forms a list by stepping through the range by the indicated interval.
public List<Comparable<T>>
subList(int
low endpoint (inclusive) of the subList
fromIndex
,
int
high endpoint (exclusive) of the subList
toIndex
)

Overrides java.util.AbstractList.subList.

Implements java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
public String
toString()

Overrides java.util.AbstractCollection.toString.

Returns a string representation of this collection.
Inherited from java.util.AbstractList:
addaddaddAllclearhashCodeindexOflastIndexOflistIteratorlistIteratorremoveremoveRangeset

Field Detail

fromback to summary
private final Comparable<T> from

The first value in the range.

reverseback to summary
private final boolean reverse

true if the range counts backwards from to to from.

sizeback to summary
private int size

The cached size, or -1 if not yet computed

toback to summary
private final Comparable<T> to

The last value in the range.

Constructor Detail

ObjectRangeback to summary
public ObjectRange(Comparable<T> from, Comparable<T> to)

Creates a new ObjectRange. Creates a reversed range if from < to.

Parameters
from:Comparable<T>

the first value in the range.

to:Comparable<T>

the last value in the range.

ObjectRangeback to summary
public ObjectRange(Comparable<T> smaller, Comparable<T> 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.

ObjectRangeback to summary
private ObjectRange(Comparable<T> smaller, Comparable<T> larger, Boolean reverse)

Constructs a Range, computing reverse if not provided. When providing reverse, 'smaller' must not be larger than 'larger'.

Parameters
smaller:Comparable<T>

start of the range, must no be larger than to when reverse != null

larger:Comparable<T>

end of the range, must be larger than from when reverse != null

reverse:Boolean

direction of the range. If null, causes direction to be computed (can be expensive).

Method Detail

areReversedback to summary
private static boolean areReversed(Comparable<T> from, Comparable<T> to)
checkBoundaryCompatibilityback to summary
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).

compareToback to summary
protected int compareTo(Comparable<T> first, Comparable<T> second)
containsback to summary
public boolean contains(Object value)

Overrides java.util.AbstractCollection.contains.

Implements java.util.List.contains, java.util.Collection.contains.

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

Parameters
value:Object

Doc from java.util.List.contains.

element whose presence in this list is to be tested

Returns:boolean

Doc from java.util.List.contains.

true if this list contains the specified element

Annotations
@Override
See Also
containsWithinBounds(Object)
containsWithinBoundsback to summary
public boolean containsWithinBounds(Object value)

Implements groovy.lang.Range.containsWithinBounds.

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

Parameters
value:Object

the value of interest

Returns:boolean

true if the value is within the bounds

Annotations
@Override
decrementback to summary
protected Object decrement(Object value)

Decrements by one

Parameters
value:Object

the value to decrement

Returns:Object

the decremented value

equalsback to summary
public boolean equals(Object that)

Overrides java.util.AbstractList.equals.

Implements java.util.List.equals, java.util.Collection.equals.

Doc from java.util.List.equals.

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 Objects.equals(e1, e2).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Parameters
that:Object

the object to be compared for equality with this list

Returns:boolean

true if the specified object is equal to this list

equalsback to summary
public boolean equals(ObjectRange that)

Compares an ObjectRange to another ObjectRange.

Parameters
that:ObjectRange

the object to check equality with

Returns:boolean

true if the ranges are equal

getback to summary
public Comparable<T> get(int index)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

Doc from java.util.List.get.

Returns the element at the specified position in this list.

Parameters
index:int

index of the element to return

Returns:Comparable<T>

the element at the specified position in this list

Annotations
@Override
getFromback to summary
public Comparable<T> getFrom()

Implements groovy.lang.Range.getFrom.

Doc from groovy.lang.Range.getFrom.

The lower value in the range.

Returns:Comparable<T>

the lower value in the range.

Annotations
@Override
getToback to summary
public Comparable<T> getTo()

Implements groovy.lang.Range.getTo.

Doc from groovy.lang.Range.getTo.

The upper value in the range.

Returns:Comparable<T>

the upper value in the range

Annotations
@Override
incrementback to summary
protected Object increment(Object value)

Increments by one

Parameters
value:Object

the value to increment

Returns:Object

the incremented value

inspectback to summary
public String inspect()

Implements groovy.lang.Range.inspect.

Returns:String

Doc from groovy.lang.Range.inspect.

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

Annotations
@Override
isReverseback to summary
public boolean isReverse()

Implements groovy.lang.Range.isReverse.

Doc from groovy.lang.Range.isReverse.

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

Returns:boolean

true if this is a reverse range

Annotations
@Override
iteratorback to summary
public Iterator<Comparable<T>> iterator()

Overrides java.util.AbstractList.iterator.

Implements java.util.List.iterator, java.util.Collection.iterator.

Doc from java.util.List.iterator.

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

Returns:Iterator<Comparable<T>>

an iterator over the elements in this list in proper sequence

Annotations
@Override
normaliseStringTypeback to summary
private static Comparable<T> normaliseStringType(final Comparable<T> operand)

if operand is a Character or a String with one character, return that character's int value.

setSizeback to summary
private void setSize(int size)

protection against calls from Groovy

Annotations
@SuppressWarnings:unused
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Doc from java.util.List.size.

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this list

Annotations
@Override
stepback to summary
public void step(int step, Closure<V> closure)

Implements groovy.lang.Range.step.

Doc from groovy.lang.Range.step.

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

Parameters
step:int

the amount by which to step. If negative, steps through the range backwards.

closure:Closure<V>

the Closure to call

Annotations
@Override
stepback to summary
public List<Comparable<T>> step(int step)

Implements groovy.lang.Range.step.

Doc from groovy.lang.Range.step.

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

Parameters
step:int

the amount by which to step. If negative, steps through the range backwards.

Returns:List<Comparable<T>>

the list formed by stepping through the range by the indicated interval.

Annotations
@Override
subListback to summary
public List<Comparable<T>> subList(int fromIndex, int toIndex)

Overrides java.util.AbstractList.subList.

Implements java.util.List.subList.

Doc from java.util.List.subList.

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

Parameters
fromIndex:int

low endpoint (inclusive) of the subList

toIndex:int

high endpoint (exclusive) of the subList

Returns:List<Comparable<T>>

a view of the specified range within this list

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.util.AbstractCollection.toString.

Doc from java.util.AbstractCollection.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(Object).

Returns:String

a string representation of this collection

groovy.lang back to summary

private final Class ObjectRange.StepIterator

extends Object
implements Iterator<Comparable<T>>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Non-thread-safe iterator which lazily produces the next element only on calls of hasNext() or next()

Field Summary

Modifier and TypeField and Description
private int
private boolean
private final ObjectRange
private final int
private Comparable<T>

Constructor Summary

AccessConstructor and Description
private
StepIterator(ObjectRange range, final int desiredStep)

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public Comparable<T>
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
private Comparable<T>
peek()

public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

indexback to summary
private int index
nextFetchedback to summary
private boolean nextFetched
rangeback to summary
private final ObjectRange range
stepback to summary
private final int step
valueback to summary
private Comparable<T> value

Constructor Detail

StepIteratorback to summary
private StepIterator(ObjectRange range, final int desiredStep)

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
nextback to summary
public Comparable<T> next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:Comparable<T>

the next element in the iteration

Annotations
@Override
peekback to summary
private Comparable<T> peek()
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

Annotations
@Override