Class IntRange
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<java.lang.Integer>
-
- groovy.lang.IntRange
-
- All Implemented Interfaces:
Range<java.lang.Integer>,java.lang.Iterable<java.lang.Integer>,java.util.Collection<java.lang.Integer>,java.util.List<java.lang.Integer>
public class IntRange extends java.util.AbstractList<java.lang.Integer> implements Range<java.lang.Integer>
Represents a list of Integer objects starting at a specifiedfromvalue up (or down) to and potentially including a giventovalue.Instances of this class may be either inclusive aware or non-inclusive aware. See the relevant constructors for creating each type. Inclusive aware IntRange instances are suitable for use with Groovy's range indexing - in particular if the from or to values might be negative. This normally happens underneath the covers but is worth keeping in mind if creating these ranges yourself explicitly.
Note: the design of this class might seem a little strange at first. It contains a Boolean field,
inclusive, which can betrue,falseornull. This design is for backwards compatibility reasons. Groovy uses this class under the covers to represent range indexing, e.g.someList[x..y]andsomeString[x..<y]. In early versions of Groovy the ranges in these expressions were represented under the covers by thenew IntRange(x, y)andnew IntRange(x, y-1). This turns out to be a lossy abstraction when x and/or y are negative values. Now the latter case is represented bynew IntRange(false, x, y).Note: This class is a copy of
ObjectRangeoptimized forint. If you make any changes to this class, you might consider making parallel changes toObjectRange.
-
-
Constructor Summary
Constructors Modifier Constructor Description IntRange(boolean inclusive, int from, int to)Creates a new inclusive awareIntRange.IntRange(int from, int to)Creates a new non-inclusive awareIntRange.protectedIntRange(int from, int to, boolean reverse)Creates a new non-inclusive awareIntRange.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends java.lang.Number & java.lang.Comparable>
NumberRangeby(T stepSize)Creates a new NumberRange with the samefromandtoas this IntRange but with a step size ofstepSize.booleancontains(java.lang.Object value)booleancontainsAll(java.util.Collection other)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.booleanequals(IntRange that)booleanequals(java.lang.Object that)Determines if this object is equal to another object.java.lang.Integerget(int index)java.lang.IntegergetFrom()The lower value in the range.intgetFromInt()Gets the 'from' value as a primitive integer.java.lang.BooleangetInclusive()Returns the inclusive flag.java.lang.IntegergetTo()The upper value in the range.intgetToInt()Gets the 'to' value as a primitive integer.inthashCode()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.Integer>iterator()intsize()java.util.List<java.lang.Integer>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.java.util.List<java.lang.Integer>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.AbstractList
add, add, addAll, clear, indexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, set
-
Methods inherited from class java.util.AbstractCollection
addAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
-
-
-
-
Constructor Detail
-
IntRange
public IntRange(int from, int to)Creates a new non-inclusive awareIntRange. Iffromis greater thanto, a reverse range is created withfromandtoswapped.- Parameters:
from- the first number in the range.to- the last number in the range.- Throws:
java.lang.IllegalArgumentException- if the range would contain more thanInteger.MAX_VALUEvalues.
-
IntRange
protected IntRange(int from, int to, boolean reverse)Creates a new non-inclusive awareIntRange.- Parameters:
from- the first value in the range.to- the last value in the range.reverse-trueif the range should count fromtotofrom.- Throws:
java.lang.IllegalArgumentException- iffromis greater thanto.
-
IntRange
public IntRange(boolean inclusive, int from, int to)Creates a new inclusive awareIntRange.- Parameters:
from- the first value in the range.to- the last value in the range.inclusive-trueif the to value is included in the range.
-
-
Method Detail
-
by
public <T extends java.lang.Number & java.lang.Comparable> NumberRange by(T stepSize)
Creates a new NumberRange with the samefromandtoas this IntRange but with a step size ofstepSize.- Parameters:
stepSize- the desired step size- Returns:
- a new NumberRange
- Since:
- 2.5.0
-
subListBorders
public RangeInfo subListBorders(int size)
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
-
equals
public boolean equals(java.lang.Object that)
Determines if this object is equal to another object. Delegates toAbstractList.equals(Object)ifthatis anything other than anIntRange.It is not necessary to override
hashCode, asAbstractList.hashCode()provides a suitable hash code.Note that equals is generally handled by
DefaultGroovyMethods.equals(List, List)instead of this method.- Specified by:
equalsin interfacejava.util.Collection<java.lang.Integer>- Specified by:
equalsin interfacejava.util.List<java.lang.Integer>- Overrides:
equalsin classjava.util.AbstractList<java.lang.Integer>- Parameters:
that- the object to compare- Returns:
trueif the objects are equal
-
equals
public boolean equals(IntRange that)
- Parameters:
that- the object to compare for equality- Returns:
trueif the ranges are equal
-
getFrom
public java.lang.Integer getFrom()
Description copied from interface:RangeThe lower value in the range.
-
getTo
public java.lang.Integer getTo()
Description copied from interface:RangeThe upper value in the range.
-
getInclusive
public java.lang.Boolean getInclusive()
Returns the inclusive flag. Null for non-inclusive aware ranges or non-null for inclusive aware ranges.
-
getFromInt
public int getFromInt()
Gets the 'from' value as a primitive integer.- Returns:
- the 'from' value as a primitive integer.
-
getToInt
public int getToInt()
Gets the 'to' value as a primitive integer.- Returns:
- the 'to' value as a primitive integer.
-
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
-
containsWithinBounds
public boolean containsWithinBounds(java.lang.Object o)
Description copied from interface:RangeIndicates 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
- Specified by:
containsWithinBoundsin interfaceRange<java.lang.Integer>- Parameters:
o- the object to check against the boundaries of the range- Returns:
trueif the object is between the from and to values
-
get
public java.lang.Integer get(int index)
- Specified by:
getin interfacejava.util.List<java.lang.Integer>- Specified by:
getin classjava.util.AbstractList<java.lang.Integer>
-
size
public int size()
- Specified by:
sizein interfacejava.util.Collection<java.lang.Integer>- Specified by:
sizein interfacejava.util.List<java.lang.Integer>- Specified by:
sizein classjava.util.AbstractCollection<java.lang.Integer>
-
iterator
public java.util.Iterator<java.lang.Integer> iterator()
- Specified by:
iteratorin interfacejava.util.Collection<java.lang.Integer>- Specified by:
iteratorin interfacejava.lang.Iterable<java.lang.Integer>- Specified by:
iteratorin interfacejava.util.List<java.lang.Integer>- Overrides:
iteratorin classjava.util.AbstractList<java.lang.Integer>
-
subList
public java.util.List<java.lang.Integer> subList(int fromIndex, int toIndex)- Specified by:
subListin interfacejava.util.List<java.lang.Integer>- Overrides:
subListin classjava.util.AbstractList<java.lang.Integer>
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.util.AbstractCollection<java.lang.Integer>
-
inspect
public java.lang.String inspect()
-
contains
public boolean contains(java.lang.Object value)
- Specified by:
containsin interfacejava.util.Collection<java.lang.Integer>- Specified by:
containsin interfacejava.util.List<java.lang.Integer>- Overrides:
containsin classjava.util.AbstractCollection<java.lang.Integer>
-
containsAll
public boolean containsAll(java.util.Collection other)
- Specified by:
containsAllin interfacejava.util.Collection<java.lang.Integer>- Specified by:
containsAllin interfacejava.util.List<java.lang.Integer>- Overrides:
containsAllin classjava.util.AbstractCollection<java.lang.Integer>
-
step
public void step(int step, Closure closure)Description copied from interface:RangeSteps through the range, calling a closure for each item.
-
step
public java.util.List<java.lang.Integer> step(int step)
Description copied from interface:RangeForms a list by stepping through the range by the indicated interval.
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfacejava.util.Collection<java.lang.Integer>- Specified by:
hashCodein interfacejava.util.List<java.lang.Integer>- Overrides:
hashCodein classjava.util.AbstractList<java.lang.Integer>
-
-