Top Description Methods
groovy.lang

public Interface Range<T extends java.lang.Comparable<T>>

extends List<T extends Comparable<T>>
Known Direct Implementers
groovy.lang.EmptyRange, groovy.lang.IntRange, groovy.lang.NumberRange, groovy.lang.ObjectRange
Imports
java.util.List

A Range represents the list of discrete items between some starting (or from) value and working up towards some ending (or to) value. For a reverse range, the list is obtained by starting at the to value and working down towards the from value. The concept of working up and working down is dependent on the range implementation. In the general case, working up involves successive calls to the first item's next() method while working down involves calling the previous() method. Optimized numerical ranges may apply numeric addition or subtraction of some numerical step size. Particular range implementations may also support the notion of inclusivity and exclusivity with respect to the ending value in the range. E.g. 1..3 == [1, 2, 3]; but 1..<3 == [1, 2]. In general, the second boundary may not be contained in the range, and a..b may produce a different set of elements than (b..a).reversed(). E.g. 1..2.5 == [1, 2]; but 2.5..1 == [2.5, 1.5]. Implementations can be memory efficient by storing just the from and to boundary values rather than eagerly creating all discrete items in the conceptual list. The actual discrete items can be lazily calculated on an as needed basis (e.g. when calling methods from the java.util.List interface or the additional step methods in the Range interface). In addition to the methods related to a Range's "discrete items" abstraction, there is a method, containsWithinBounds which, for numerical ranges, allows checking within the continuous interval between the Range's boundary values.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the object is between the from and to values
containsWithinBounds
(Object
the object to check against the boundaries of the range
o
)

Indicates whether an object is greater than or equal to the from value for the range and less than or equal to the to value.

public T

Returns:

the lower value in the range.
getFrom
()

The lower value in the range.

public T

Returns:

the upper value in the range
getTo
()

The upper value in the range.

public String

Returns:

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

public boolean

Returns:

true if this is a reverse range
isReverse
()

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

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
)

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

public List<T>

Returns:

the list formed by stepping through the range by the indicated interval.
step
(int
the amount by which to step. If negative, steps through the range backwards.
step
)

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

Inherited from java.util.List:
addaddaddAlladdAlladdFirstaddLastclearcontainscontainsAllcopyOfequalsgetgetFirstgetLasthashCodeindexOfisEmptyiteratorlastIndexOflistIteratorlistIteratorofofofofofofofofofofofofremoveremoveremoveAllremoveFirstremoveLastreplaceAllretainAllreversedsetsizesortspliteratorsubListtoArraytoArray

Method Detail

containsWithinBoundsback to summary
public boolean containsWithinBounds(Object o)

Indicates whether an object is greater than or equal to the from value for the range and less than or equal to the to value.

This may be true even for values not contained in the range. Example: from = 1.5, to = 3, next() increments by 1 containsWithinBounds(2) == true contains(2) == false

Parameters
o:Object

the object to check against the boundaries of the range

Returns:boolean

true if the object is between the from and to values

getFromback to summary
public T getFrom()

The lower value in the range.

Returns:T

the lower value in the range.

getToback to summary
public T getTo()

The upper value in the range.

Returns:T

the upper value in the range

inspectback to summary
public String inspect()
Returns:String

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

isReverseback to summary
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:boolean

true if this is a reverse range

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

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

stepback to summary
public List<T> step(int 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<T>

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