Class IntRange
- All Implemented Interfaces:
- Range<Integer>,- Serializable,- Iterable<Integer>,- Collection<Integer>,- List<Integer>
from value up (or down) to and potentially including a given to value.
 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 Boolean
 flags, inclusiveLeft and inclusiveRight, which can be true,
 false or null. This design is for backwards compatibility reasons.
 Groovy uses this class under the covers to represent range indexing, e.g.
 someList[x..y] and someString[x..<y]. In early versions of Groovy the
 ranges in these expressions were represented under the covers by the
 new IntRange(x, y) and new 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 by new IntRange(false, x, y).
 
 Note: This class is a copy of ObjectRange optimized for int. If you make any
 changes to this class, you might consider making parallel changes to ObjectRange.
- See Also:
- 
Field SummaryFields inherited from class java.util.AbstractListmodCount
- 
Constructor SummaryConstructorsModifierConstructorDescriptionIntRange(boolean inclusiveLeft, boolean inclusiveRight, int from, int to) Creates a new inclusive awareIntRangeIntRange(boolean inclusiveRight, 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 SummaryModifier and TypeMethodDescription<T extends Number & Comparable>
 NumberRangeby(T stepSize) Creates a new NumberRange with the samefromandtoas this IntRange but with a step size ofstepSize.booleanbooleancontainsAll(Collection other) booleanIndicates whether an object is greater than or equal to thefromvalue for the range and less than or equal to thetovalue.booleanbooleanDetermines if this object is equal to another object.get(int index) getFrom()The lower value in the range.intGets the 'from' value as a primitive integer.Returns the same asgetInclusiveRight, kept here for backwards compatibility.Returns the inclusiveLeft flag.Returns the inclusiveRight flag.getTo()The upper value in the range.intgetToInt()Gets the 'to' value as a primitive integer.inthashCode()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 step) 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.AbstractListadd, add, addAll, clear, indexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, setMethods inherited from class java.util.AbstractCollectionaddAll, 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.util.Listadd, add, addAll, addAll, clear, indexOf, isEmpty, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, sort, spliterator, toArray, toArray
- 
Constructor Details- 
IntRangepublic 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:
- IllegalArgumentException- if the range would contain more than- Integer.MAX_VALUEvalues.
 
- 
IntRangeprotected 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 from- toto- from.
- Throws:
- IllegalArgumentException- if- fromis greater than- to.
 
- 
IntRangepublic IntRange(boolean inclusiveRight, int from, int to) Creates a new inclusive awareIntRange.- Parameters:
- from- the first value in the range.
- to- the last value in the range.
- inclusiveRight-- trueif the to value is included in the range.
 
- 
IntRangepublic IntRange(boolean inclusiveLeft, boolean inclusiveRight, int from, int to) Creates a new inclusive awareIntRange- Parameters:
- inclusiveLeft-- trueif the from value is included in the range.
- inclusiveRight-- trueif the to value is included in the range.
- from- the first value in the range.
- to- the last value in the range.
 
 
- 
- 
Method Details- 
byCreates 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
 
- 
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
 
- 
equalsDetermines 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 interface- Collection<Integer>
- Specified by:
- equalsin interface- List<Integer>
- Overrides:
- equalsin class- AbstractList<Integer>
- Parameters:
- that- the object to compare
- Returns:
- trueif the objects are equal
 
- 
equals- Parameters:
- that- the object to compare for equality
- Returns:
- trueif the ranges are equal
 
- 
getFromDescription copied from interface:RangeThe lower value in the range.
- 
getToDescription copied from interface:RangeThe upper value in the range.
- 
getInclusiveReturns the same asgetInclusiveRight, kept here for backwards compatibility.
- 
getInclusiveRightReturns the inclusiveRight flag. Null for non-inclusive aware ranges or non-null for inclusive aware ranges.
- 
getInclusiveLeftReturns the inclusiveLeft flag. Null for non-inclusive aware ranges or non-null for inclusive aware ranges.
- 
getFromIntpublic int getFromInt()Gets the 'from' value as a primitive integer.- Returns:
- the 'from' value as a primitive integer.
 
- 
getToIntpublic int getToInt()Gets the 'to' value as a primitive integer.- Returns:
- the 'to' value as a primitive integer.
 
- 
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
- 
containsWithinBoundsDescription 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 interface- Range<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
- 
sizepublic int size()- Specified by:
- sizein interface- Collection<Integer>
- Specified by:
- sizein interface- List<Integer>
- Specified by:
- sizein class- AbstractCollection<Integer>
 
- 
iterator
- 
subList
- 
toString- Overrides:
- toStringin class- AbstractCollection<Integer>
 
- 
inspect
- 
contains- Specified by:
- containsin interface- Collection<Integer>
- Specified by:
- containsin interface- List<Integer>
- Overrides:
- containsin class- AbstractCollection<Integer>
 
- 
containsAll- Specified by:
- containsAllin interface- Collection<Integer>
- Specified by:
- containsAllin interface- List<Integer>
- Overrides:
- containsAllin class- AbstractCollection<Integer>
 
- 
stepDescription copied from interface:RangeSteps through the range, calling a closure for each item.
- 
stepDescription copied from interface:RangeForms a list by stepping through the range by the indicated interval.
- 
hashCodepublic int hashCode()- Specified by:
- hashCodein interface- Collection<Integer>
- Specified by:
- hashCodein interface- List<Integer>
- Overrides:
- hashCodein class- AbstractList<Integer>
 
 
-