groovy.lang
Class IntRange

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList
          extended by groovy.lang.IntRange
All Implemented Interfaces:
Range, java.lang.Iterable, java.util.Collection, java.util.List

public class IntRange
extends java.util.AbstractList
implements Range

Represents a list of Integer objects from a specified int up (or down) to and including a given to.

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.

Version:
$Revision: 13757 $
Author:
James Strachan

Nested Class Summary
private  class IntRange.IntRangeIterator
          Iterates through each number in an IntRange.
 
Field Summary
private  int from
          The first number in the range.
private  boolean reverse
          If false, counts up from from to to.
private  int to
          The last number in the range.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
  IntRange(int from, int to)
          Creates a new IntRange.
protected IntRange(int from, int to, boolean reverse)
          Creates a new IntRange.
 
Method Summary
 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 the from value for the range and less than or equal to the to value.
 boolean equals(IntRange that)
          Compares an IntRange to another IntRange.
 boolean equals(java.lang.Object that)
          Determines if this object is equal to another object.
 java.lang.Object 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 value
 java.util.Iterator iterator()
          
 int size()
          
 java.util.List 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 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
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
add, add, addAll, addAll, clear, hashCode, indexOf, isEmpty, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, retainAll, set, toArray, toArray
 

Field Detail

from

private int from
The first number in the range. from is always less than or equal to to.


to

private int to
The last number in the range. to is always greater than or eqaul to from.


reverse

private boolean reverse
If false, counts up from from to to. Otherwise, counts down from to to from.

Constructor Detail

IntRange

public IntRange(int from,
                int to)
Creates a new IntRange. If from is greater than to, a reverse range is created with from and to 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 than Integer.MAX_VALUE values.

IntRange

protected IntRange(int from,
                   int to,
                   boolean reverse)
Creates a new IntRange.

Parameters:
from - the first value in the range.
to - the last value in the range.
reverse - true if the range should count from to to from.
Throws:
java.lang.IllegalArgumentException - if from is greater than to.
Method Detail

equals

public boolean equals(java.lang.Object that)
Determines if this object is equal to another object. Delegates to AbstractList.equals(Object) if that is anthing other than an IntRange.

It is not necessary to override hashCode, as AbstractList.hashCode() provides a suitable hash code.

Note that equals is generally handled by DefaultGroovyMethods.equals(List,List) instead of this method.

Specified by:
equals in interface java.util.Collection
Specified by:
equals in interface java.util.List
Overrides:
equals in class java.util.AbstractList
Parameters:
that - the object to compare
Returns:
true if the objects are equal

equals

public boolean equals(IntRange that)
Compares an IntRange to another IntRange.

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.

Specified by:
getFrom in interface Range
Returns:
the lower value in the range.

getTo

public java.lang.Comparable getTo()
The upper value in the range.

Specified by:
getTo in interface Range
Returns:
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

Specified by:
isReverse in interface Range
Returns:
true if this is a reverse range

containsWithinBounds

public boolean containsWithinBounds(java.lang.Object o)
Description copied from interface: Range
Indicates whether an object is greater than or equal to the from value for the range and less than or equal to the to value.

Specified by:
containsWithinBounds in interface Range
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.Object get(int index)

Specified by:
get in interface java.util.List
Specified by:
get in class java.util.AbstractList

size

public int size()

Specified by:
size in interface java.util.Collection
Specified by:
size in interface java.util.List
Specified by:
size in class java.util.AbstractCollection

iterator

public java.util.Iterator iterator()

Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Specified by:
iterator in interface java.util.List
Overrides:
iterator in class java.util.AbstractList

subList

public java.util.List subList(int fromIndex,
                              int toIndex)

Specified by:
subList in interface java.util.List
Overrides:
subList in class java.util.AbstractList

toString

public java.lang.String toString()

Overrides:
toString in class java.util.AbstractCollection

inspect

public java.lang.String inspect()

Specified by:
inspect in interface Range
Returns:
the verbose String representation of this Range as would be typed into a console to create the Range instance

contains

public boolean contains(java.lang.Object value)

Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.List
Overrides:
contains in class java.util.AbstractCollection

containsAll

public boolean containsAll(java.util.Collection other)

Specified by:
containsAll in interface java.util.Collection
Specified by:
containsAll in interface java.util.List
Overrides:
containsAll in class java.util.AbstractCollection

step

public void step(int step,
                 Closure closure)
Steps through the range, calling a closure for each number.

Specified by:
step in interface Range
Parameters:
step - the amount by which to step. If negative, steps through the range backwards.
closure - the Closure to call

step

public java.util.List step(int step)
Forms a list by stepping through the range by the indicated interval.

Specified by:
step in interface Range
Parameters:
step - 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.


Copyright © ${year} The Codehaus. All Rights Reserved.