Package groovy.lang
Interface Range<T extends java.lang.Comparable>
-
- All Superinterfaces:
java.util.Collection<T>,java.lang.Iterable<T>,java.util.List<T>
- All Known Implementing Classes:
EmptyRange,IntRange,NumberRange,ObjectRange
public interface Range<T extends java.lang.Comparable> extends java.util.List<T>A Range represents the list of discrete items between some starting (orfrom) value and working up towards some ending (orto) value. For a reverse range, the list is obtained by starting at thetovalue and working down towards thefromvalue. 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'snext()method while working down involves calling theprevious()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]; but1..<3 == [1, 2]. In general, the second boundary may not be contained in the range, anda..bmay produce a different set of elements than(b..a).reversed(). E.g.1..2.5 == [1, 2]; but2.5..1 == [2.5, 1.5]. Implementations can be memory efficient by storing just thefromandtoboundary 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 thejava.util.Listinterface or the additionalstepmethods in theRangeinterface). In addition to the methods related to a Range's "discrete items" abstraction, there is a method,containsWithinBoundswhich, for numerical ranges, allows checking within the continuous interval between the Range's boundary values.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleancontainsWithinBounds(java.lang.Object o)Indicates whether an object is greater than or equal to thefromvalue for the range and less than or equal to thetovalue.TgetFrom()The lower value in the range.TgetTo()The upper value in the range.java.lang.Stringinspect()booleanisReverse()Indicates whether this is a reverse range which iterates backwards starting from the to value and ending on the from valuejava.util.List<T>step(int step)Forms a list by stepping through the range by the indicated interval.voidstep(int step, Closure closure)Steps through the range, calling a closure for each item.-
Methods inherited from interface java.util.List
add, add, addAll, addAll, clear, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray
-
-
-
-
Method Detail
-
getFrom
T getFrom()
The lower value in the range.- Returns:
- the lower value in the range.
-
getTo
T getTo()
The upper value in the range.- Returns:
- the upper value in the range
-
isReverse
boolean isReverse()
Indicates whether this is a reverse range which iterates backwards starting from the to value and ending on the from value- Returns:
trueif this is a reverse range
-
containsWithinBounds
boolean containsWithinBounds(java.lang.Object o)
Indicates whether an object is greater than or equal to thefromvalue for the range and less than or equal to thetovalue.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- the object to check against the boundaries of the range- Returns:
trueif the object is between the from and to values
-
step
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- theClosureto call
-
step
java.util.List<T> 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.
-
-