Package groovy.lang
Class NumberRange
- All Implemented Interfaces:
Range<Comparable>,Serializable,Iterable<Comparable>,Collection<Comparable>,List<Comparable>
public class NumberRange
extends AbstractList<Comparable>
implements Range<Comparable>, 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:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionNumberRange(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, boolean inclusiveLeft, boolean inclusiveRight) Creates aNumberRange.NumberRange(T from, U to, V stepSize) Creates an inclusiveNumberRange.NumberRange(T from, U to, V stepSize, boolean inclusive) Creates aNumberRange.NumberRange(T from, U to, V stepSize, boolean inclusiveLeft, boolean inclusiveRight) Creates aNumberRange. -
Method Summary
Modifier and TypeMethodDescription<T extends Number & 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.booleaniterates over all values and returns true if one value matches.booleancontainsWithinBounds(Object value) Checks whether a value is between the from and to values of a RangebooleanAn 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.get(int index) getFrom()The lower value in the range.getTo()The upper value in the range.inthashCode()A NumberRange's hashCode is based on hashCode values of the discrete items it represents.inspect()booleanIndicates whether this is a reverse range which iterates backwards starting from the to value and ending on the from valueiterator()intsize()step(int numSteps) Forms a list by stepping through the range by the indicated interval.voidSteps through the range, calling a closure for each item.subList(int fromIndex, int toIndex) subListBorders(int size) A method for determining from and to information when using this IntRange to index an aggregate object of the specified size.toString()Methods inherited from class java.util.AbstractList
add, add, addAll, clear, indexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, setMethods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArrayMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
add, add, addAll, addAll, clear, containsAll, indexOf, isEmpty, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, sort, spliterator, toArray, toArray
-
Constructor Details
-
NumberRange
public NumberRange(T from, U to) - Parameters:
from- the first value in the rangeto- the last value in the range
-
NumberRange
public NumberRange(T from, U to, boolean inclusive) - Parameters:
from- start of the rangeto- end of the rangeinclusive- whether the range is inclusive
-
NumberRange
public NumberRange(T from, U to, V stepSize) - Parameters:
from- start of the rangeto- end of the rangestepSize- the gap between discrete elements in the range
-
NumberRange
public NumberRange(T from, U to, V stepSize, boolean inclusive) - Parameters:
from- start of the rangeto- end of the rangestepSize- the gap between discrete elements in the rangeinclusive- whether the range is inclusive (upper bound)
-
NumberRange
public NumberRange(T from, U to, boolean inclusiveLeft, boolean inclusiveRight) - Parameters:
from- start of the rangeto- end of the rangeinclusiveLeft- whether the range is includes the lower boundinclusiveRight- whether the range is includes the upper bound
-
NumberRange
public NumberRange(T from, U to, V stepSize, boolean inclusiveLeft, boolean inclusiveRight) - Parameters:
from- start of the rangeto- end of the rangestepSize- the gap between discrete elements in the rangeinclusiveLeft- whether the range is includes the lower boundinclusiveRight- whether the range is includes the upper bound
-
-
Method Details
-
subListBorders
A 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
-
by
For 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
-
equals
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 interfaceCollection<Comparable>- Specified by:
equalsin interfaceList<Comparable>- Overrides:
equalsin classAbstractList<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:
-
hashCode
public int hashCode()A NumberRange's hashCode is based on hashCode values of the discrete items it represents.- Specified by:
hashCodein interfaceCollection<Comparable>- Specified by:
hashCodein interfaceList<Comparable>- Overrides:
hashCodein classAbstractList<Comparable>- Returns:
- the hashCode value
-
fastEquals
Compares 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
-
getFrom
Description copied from interface:RangeThe lower value in the range.- Specified by:
getFromin interfaceRange<Comparable>- Returns:
- the lower value in the range.
-
getTo
Description copied from interface:RangeThe upper value in the range.- Specified by:
getToin interfaceRange<Comparable>- Returns:
- the upper value in the range
-
getStepSize
-
isReverse
public 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- Specified by:
isReversein interfaceRange<Comparable>- Returns:
trueif this is a reverse range
-
get
- Specified by:
getin interfaceList<Comparable>- Specified by:
getin classAbstractList<Comparable>
-
containsWithinBounds
Checks whether a value is between the from and to values of a Range- Specified by:
containsWithinBoundsin interfaceRange<Comparable>- Parameters:
value- the value of interest- Returns:
- true if the value is within the bounds
-
size
public int size()- Specified by:
sizein interfaceCollection<Comparable>- Specified by:
sizein interfaceList<Comparable>- Specified by:
sizein classAbstractCollection<Comparable>
-
subList
- Specified by:
subListin interfaceList<Comparable>- Overrides:
subListin classAbstractList<Comparable>
-
toString
- Overrides:
toStringin classAbstractCollection<Comparable>
-
inspect
-
contains
iterates over all values and returns true if one value matches. Also see containsWithinBounds.- Specified by:
containsin interfaceCollection<Comparable>- Specified by:
containsin interfaceList<Comparable>- Overrides:
containsin classAbstractCollection<Comparable>
-
step
Steps through the range, calling a closure for each item.- Specified by:
stepin interfaceRange<Comparable>- Parameters:
numSteps- the amount by which to step. If negative, steps through the range backwards.closure- theClosureto call
-
iterator
- Specified by:
iteratorin interfaceCollection<Comparable>- Specified by:
iteratorin interfaceIterable<Comparable>- Specified by:
iteratorin interfaceList<Comparable>- Overrides:
iteratorin classAbstractList<Comparable>
-
step
Description copied from interface:RangeForms a list by stepping through the range by the indicated interval.- Specified by:
stepin interfaceRange<Comparable>- Parameters:
numSteps- 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.
-