Package groovy.lang
Class NumberRange
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<java.lang.Comparable>
groovy.lang.NumberRange
- All Implemented Interfaces:
- Range<java.lang.Comparable>,- java.io.Serializable,- java.lang.Iterable<java.lang.Comparable>,- java.util.Collection<java.lang.Comparable>,- java.util.List<java.lang.Comparable>
public class NumberRange extends java.util.AbstractList<java.lang.Comparable> implements Range<java.lang.Comparable>, java.io.Serializable
Represents an immutable list of Numbers from a value to a value with a particular step size.
 In general, it isn't recommended using a NumberRange as a key to a map. The range
 0..3 is deemed to be equal to 0.0..3.0 but they have different hashCode values,
 so storing a value using one of these ranges couldn't be retrieved using the other.
- Since:
- 2.5.0
- See Also:
- Serialized Form
- 
Field SummaryFields inherited from class java.util.AbstractListmodCount
- 
Constructor SummaryConstructors Constructor Description NumberRange(T from, U to)Creates an inclusiveNumberRangewith step size 1.NumberRange(T from, U to, boolean inclusive)Creates a newNumberRangewith step size 1.NumberRange(T from, U to, V stepSize)Creates an inclusiveNumberRange.NumberRange(T from, U to, V stepSize, boolean inclusive)Creates aNumberRange.
- 
Method SummaryModifier and Type Method Description <T extends java.lang.Number & java.lang.Comparable>
 NumberRangeby(T stepSize)For a NumberRange with step size 1, creates a new NumberRange with the samefromandtoas this NumberRange but with a step size ofstepSize.booleancontains(java.lang.Object value)iterates over all values and returns true if one value matches.booleancontainsWithinBounds(java.lang.Object value)Checks whether a value is between the from and to values of a Rangebooleanequals(java.lang.Object that)An object is deemed equal to this NumberRange if it represents a List of items and those items equal the list of discrete items represented by this NumberRange.booleanfastEquals(NumberRange that)Compares aNumberRangeto anotherNumberRangeusing only a strict comparison of the NumberRange properties.java.lang.Comparableget(int index)java.lang.ComparablegetFrom()The lower value in the range.java.lang.ComparablegetStepSize()java.lang.ComparablegetTo()The upper value in the range.inthashCode()A NumberRange's hashCode is based on hashCode values of the discrete items it represents.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.Iterator<java.lang.Comparable>iterator()intsize()java.util.List<java.lang.Comparable>step(int numSteps)Forms a list by stepping through the range by the indicated interval.voidstep(int numSteps, Closure closure)Steps through the range, calling a closure for each item.java.util.List<java.lang.Comparable>subList(int fromIndex, int toIndex)RangeInfosubListBorders(int size)A method for determining from and to information when using this IntRange to index an aggregate object of the specified size.java.lang.StringtoString()Methods inherited from class java.util.AbstractListadd, add, addAll, clear, indexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, setMethods inherited from class java.util.AbstractCollectionaddAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArrayMethods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.CollectionparallelStream, removeIf, stream, toArrayMethods inherited from interface java.lang.IterableforEachMethods inherited from interface java.util.Listadd, add, addAll, addAll, clear, containsAll, indexOf, isEmpty, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, sort, spliterator, toArray, toArray
- 
Constructor Details- 
NumberRangepublic NumberRange(T from, U to)- Parameters:
- from- the first value in the range
- to- the last value in the range
 
- 
NumberRangepublic NumberRange(T from, U to, boolean inclusive)- Parameters:
- from- start of the range
- to- end of the range
- inclusive- whether the range is inclusive
 
- 
NumberRangepublic NumberRange(T from, U to, V stepSize)- Parameters:
- from- start of the range
- to- end of the range
- stepSize- the gap between discrete elements in the range
 
- 
NumberRangepublic NumberRange(T from, U to, V stepSize, boolean inclusive)- Parameters:
- from- start of the range
- to- end of the range
- stepSize- the gap between discrete elements in the range
- inclusive- whether the range is inclusive
 
 
- 
- 
Method Details- 
subListBordersA method for determining from and to information when using this IntRange to index an aggregate object of the specified size. Normally only used internally within Groovy but useful if adding range indexing support for your own aggregates.- Parameters:
- size- the size of the aggregate being indexed
- Returns:
- the calculated range information (with 1 added to the to value, ready for providing to subList
 
- 
byFor a NumberRange with step size 1, creates a new NumberRange with the samefromandtoas this NumberRange but with a step size ofstepSize.- Parameters:
- stepSize- the desired step size
- Returns:
- a new NumberRange
 
- 
equalspublic boolean equals(java.lang.Object that)An object is deemed equal to this NumberRange if it represents a List of items and those items equal the list of discrete items represented by this NumberRange.- Specified by:
- equalsin interface- java.util.Collection<java.lang.Comparable>
- Specified by:
- equalsin interface- java.util.List<java.lang.Comparable>
- Overrides:
- equalsin class- java.util.AbstractList<java.lang.Comparable>
- Parameters:
- that- the object to be compared for equality with this NumberRange
- Returns:
- trueif the specified object is equal to this NumberRange
- See Also:
- fastEquals(NumberRange)
 
- 
hashCodepublic int hashCode()A NumberRange's hashCode is based on hashCode values of the discrete items it represents.- Specified by:
- hashCodein interface- java.util.Collection<java.lang.Comparable>
- Specified by:
- hashCodein interface- java.util.List<java.lang.Comparable>
- Overrides:
- hashCodein class- java.util.AbstractList<java.lang.Comparable>
- Returns:
- the hashCode value
 
- 
fastEqualsCompares aNumberRangeto anotherNumberRangeusing only a strict comparison of the NumberRange properties. This won't return true for some ranges which represent the same discrete items, use equals instead for that but will be much faster for large lists.- Parameters:
- that- the NumberRange to check equality with
- Returns:
- trueif the ranges are equal
 
- 
getFrompublic java.lang.Comparable getFrom()Description copied from interface:RangeThe lower value in the range.
- 
getTopublic java.lang.Comparable getTo()Description copied from interface:RangeThe upper value in the range.
- 
getStepSizepublic java.lang.Comparable getStepSize()
- 
isReversepublic boolean isReverse()Description copied from interface:RangeIndicates whether this is a reverse range which iterates backwards starting from the to value and ending on the from value
- 
getpublic java.lang.Comparable get(int index)- Specified by:
- getin interface- java.util.List<java.lang.Comparable>
- Specified by:
- getin class- java.util.AbstractList<java.lang.Comparable>
 
- 
containsWithinBoundspublic boolean containsWithinBounds(java.lang.Object value)Checks whether a value is between the from and to values of a Range- Specified by:
- containsWithinBoundsin interface- Range<java.lang.Comparable>
- Parameters:
- value- the value of interest
- Returns:
- true if the value is within the bounds
 
- 
sizepublic int size()- Specified by:
- sizein interface- java.util.Collection<java.lang.Comparable>
- Specified by:
- sizein interface- java.util.List<java.lang.Comparable>
- Specified by:
- sizein class- java.util.AbstractCollection<java.lang.Comparable>
 
- 
subListpublic java.util.List<java.lang.Comparable> subList(int fromIndex, int toIndex)- Specified by:
- subListin interface- java.util.List<java.lang.Comparable>
- Overrides:
- subListin class- java.util.AbstractList<java.lang.Comparable>
 
- 
toStringpublic java.lang.String toString()- Overrides:
- toStringin class- java.util.AbstractCollection<java.lang.Comparable>
 
- 
inspectpublic java.lang.String inspect()
- 
containspublic boolean contains(java.lang.Object value)iterates over all values and returns true if one value matches. Also see containsWithinBounds.- Specified by:
- containsin interface- java.util.Collection<java.lang.Comparable>
- Specified by:
- containsin interface- java.util.List<java.lang.Comparable>
- Overrides:
- containsin class- java.util.AbstractCollection<java.lang.Comparable>
 
- 
stepSteps through the range, calling a closure for each item.
- 
iteratorpublic java.util.Iterator<java.lang.Comparable> iterator()- Specified by:
- iteratorin interface- java.util.Collection<java.lang.Comparable>
- Specified by:
- iteratorin interface- java.lang.Iterable<java.lang.Comparable>
- Specified by:
- iteratorin interface- java.util.List<java.lang.Comparable>
- Overrides:
- iteratorin class- java.util.AbstractList<java.lang.Comparable>
 
- 
steppublic java.util.List<java.lang.Comparable> step(int numSteps)Description copied from interface:RangeForms a list by stepping through the range by the indicated interval.
 
-