Package groovy.lang
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 from a specified int up (or down) to and including a given to.ObjectRange
optimized forint
. If you make any changes to this class, you might consider making parallel changes toObjectRange
.- Version:
- $Revision$
- Author:
- James Strachan
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(java.lang.Object value)
boolean
containsAll(java.util.Collection other)
boolean
containsWithinBounds(java.lang.Object o)
Indicates whether an object is greater than or equal to thefrom
value for the range and less than or equal to theto
value.boolean
equals(IntRange that)
boolean
equals(java.lang.Object that)
Determines if this object is equal to another object.java.lang.Integer
get(int index)
java.lang.Comparable
getFrom()
The lower value in the range.int
getFromInt()
Gets the 'from' value as an integer.java.lang.Comparable
getTo()
The upper value in the range.int
getToInt()
Gets the 'to' value as an integer.java.lang.String
inspect()
boolean
isReverse()
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()
int
size()
java.util.List<java.lang.Integer>
step(int step)
Forms a list by stepping through the range by the indicated interval.void
step(int step, Closure closure)
Steps through the range, calling a closure for each number.java.util.List<java.lang.Integer>
subList(int fromIndex, int toIndex)
java.lang.String
toString()
-
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, hashCode, 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 newIntRange
. Iffrom
is greater thanto
, a reverse range is created withfrom
andto
swapped.- 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_VALUE
values.
-
IntRange
protected IntRange(int from, int to, boolean reverse)
Creates a newIntRange
.- Parameters:
from
- the first value in the range.to
- the last value in the range.reverse
-true
if the range should count fromto
tofrom
.- Throws:
java.lang.IllegalArgumentException
- iffrom
is greater thanto
.
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object that)
Determines if this object is equal to another object. Delegates toAbstractList.equals(Object)
ifthat
is anything other than anIntRange
. It is not necessary to overridehashCode
, asAbstractList.hashCode()
provides a suitable hash code.DefaultGroovyMethods.equals(List,List)
instead of this method.- Specified by:
equals
in interfacejava.util.Collection<java.lang.Integer>
- Specified by:
equals
in interfacejava.util.List<java.lang.Integer>
- Overrides:
equals
in classjava.util.AbstractList<java.lang.Integer>
- Parameters:
that
- the object to compare- Returns:
true
if the objects are equal
-
equals
public boolean equals(IntRange that)
- Parameters:
that
- the object to compare for equality- Returns:
true
if the ranges are equal
-
getFrom
public java.lang.Comparable getFrom()
The lower value in the range.
-
getTo
public java.lang.Comparable getTo()
The upper value in the range.
-
getFromInt
public int getFromInt()
Gets the 'from' value as an integer.- Returns:
- the 'from' value as an integer.
-
getToInt
public int getToInt()
Gets the 'to' value as an integer.- Returns:
- the 'to' value as an integer.
-
isReverse
public boolean isReverse()
Indicates 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:Range
Indicates whether an object is greater than or equal to thefrom
value for the range and less than or equal to theto
value.- Specified by:
containsWithinBounds
in interfaceRange<java.lang.Integer>
- Parameters:
o
- the object to check against the boundaries of the range- Returns:
true
if the object is between the from and to values
-
get
public java.lang.Integer get(int index)
- Specified by:
get
in interfacejava.util.List<java.lang.Integer>
- Specified by:
get
in classjava.util.AbstractList<java.lang.Integer>
-
size
public int size()
- Specified by:
size
in interfacejava.util.Collection<java.lang.Integer>
- Specified by:
size
in interfacejava.util.List<java.lang.Integer>
- Specified by:
size
in classjava.util.AbstractCollection<java.lang.Integer>
-
iterator
public java.util.Iterator<java.lang.Integer> iterator()
- Specified by:
iterator
in interfacejava.util.Collection<java.lang.Integer>
- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Integer>
- Specified by:
iterator
in interfacejava.util.List<java.lang.Integer>
- Overrides:
iterator
in classjava.util.AbstractList<java.lang.Integer>
-
subList
public java.util.List<java.lang.Integer> subList(int fromIndex, int toIndex)
- Specified by:
subList
in interfacejava.util.List<java.lang.Integer>
- Overrides:
subList
in classjava.util.AbstractList<java.lang.Integer>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.util.AbstractCollection<java.lang.Integer>
-
inspect
public java.lang.String inspect()
-
contains
public boolean contains(java.lang.Object value)
- Specified by:
contains
in interfacejava.util.Collection<java.lang.Integer>
- Specified by:
contains
in interfacejava.util.List<java.lang.Integer>
- Overrides:
contains
in classjava.util.AbstractCollection<java.lang.Integer>
-
containsAll
public boolean containsAll(java.util.Collection other)
- Specified by:
containsAll
in interfacejava.util.Collection<java.lang.Integer>
- Specified by:
containsAll
in interfacejava.util.List<java.lang.Integer>
- Overrides:
containsAll
in classjava.util.AbstractCollection<java.lang.Integer>
-
step
public void step(int step, Closure closure)
Steps through the range, calling a closure for each number.
-
step
public java.util.List<java.lang.Integer> step(int step)
Forms a list by stepping through the range by the indicated interval.
-
-