au.com.zip.cs
Class IteratorUtil

java.lang.Object
  extended by au.com.zip.cs.IteratorUtil

public class IteratorUtil
extends Object

Assorted static methods to do things with Iterators.

Author:
Cameron Simpson <cs@zip.com.au> 30jul2003

Method Summary
static Object byIndex(Iterator iter, int index)
          Return the specified Object by doing a seqeuntial search through an Iterator.
static boolean equals(Iterator i1, Iterator i2)
          Compare two Iterators for equality.
static Object getIteration(Iterator iter, int n)
          Return the nth next Object from an Iterator.
static int indexOf(Iterator iter, Object o)
          Return the index of the specified Object in the Iterator.
static int indexOf(List list, Object o)
          Returns the index in this List of the first occurrence of the specified element, or -1 if this list does not contain this element.
static int indexOfComparable(Iterator iter, Comparable c)
          Return the index of a comparable Object in the Iterator.
static int indexOfEquivalent(Iterator iter, Object o)
          Return the index of an equivalent Object in the Iterator.
static int lastIndexOfEquivalent(Iterator iter, Object o)
          Return the last index of an equivalent Object in the Iterator.
static boolean skip(Iterator iter, int count)
          Skip forward count elements of the Iterator.
static Object[] toArray(Iterator iter, int size)
          Create and fill an Object[] with the elements from an Iterator.
static Object[] toArray(Iterator iter, Object[] ary)
          Fill an Object[] with the elements from an Iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toArray

public static Object[] toArray(Iterator iter,
                               int size)
Create and fill an Object[] with the elements from an Iterator.

Parameters:
iter - the Iterator
size - the size of the array to create
Returns:
the array of Objects from the Iterator

toArray

public static Object[] toArray(Iterator iter,
                               Object[] ary)
Fill an Object[] with the elements from an Iterator.

Parameters:
iter - the Iterator
ary - the array to fill
Returns:
the supplied array

byIndex

public static Object byIndex(Iterator iter,
                             int index)
Return the specified Object by doing a seqeuntial search through an Iterator.

Parameters:
iter - the Iterator to search
index - the Object to seek, counting from 0
Returns:
the Object, or null if there aren't that many Objects
Throws:
IllegalArgumentException - if the index is negative

indexOf

public static int indexOf(Iterator iter,
                          Object o)
Return the index of the specified Object in the Iterator. This does a sequential search for the Object. Since this uses up the Iterator, a new one, identically constructed, will be required to make use of the index returned.

Parameters:
iter - the Iterator to search
o - the Object to seek
Returns:
the index, counting from 0, or -1 if it is not present

indexOf

public static int indexOf(List list,
                          Object o)
Returns the index in this List of the first occurrence of the specified element, or -1 if this list does not contain this element. Implemented by indexOf(Iterator,Object).

Parameters:
list - the List to search
Returns:
the index of the element, or -1 if not present
See Also:
List.indexOf(Object)

indexOfEquivalent

public static int indexOfEquivalent(Iterator iter,
                                    Object o)
Return the index of an equivalent Object in the Iterator. This does a sequential search for an Object whose Object.equals(Object) method returns true.

Parameters:
iter - the Iterator to search
o - the Object whose equivalent is sought
Returns:
the index, counting from 0, or -1 if it is not present

lastIndexOfEquivalent

public static int lastIndexOfEquivalent(Iterator iter,
                                        Object o)
Return the last index of an equivalent Object in the Iterator. This does a sequential search for an Object whose Object.equals(Object) method returns true.

Parameters:
iter - the Iterator to search
o - the Object whose last equivalent is sought
Returns:
the last index, counting from 0, or -1 if it is not present

indexOfComparable

public static int indexOfComparable(Iterator iter,
                                    Comparable c)
Return the index of a comparable Object in the Iterator. This does a sequential search for an Object which returns 0 from the supplied Comparable's Comparable.compareTo(Object) method. Objects returned by the Iterator which cannot be compared are counted but not matched.

Parameters:
iter - the Iterator to search
c - the reference Comparable
Returns:
the index, counting from 0, or -1 if it is not present

equals

public static boolean equals(Iterator i1,
                             Iterator i2)
Compare two Iterators for equality. Compares each element from the first Iterator with the corresponding element from the other Iterator.

Parameters:
i1 - the first Iterator
i2 - the second Iterator
Returns:
true if every element passes Object.equals(Object) and the Iterators are the same length

skip

public static boolean skip(Iterator iter,
                           int count)
Skip forward count elements of the Iterator.

Parameters:
iter - the Iterator
count - the number of elements to skip
Returns:
true if that many elements were skipped, false if the end of the Iterator was reached

getIteration

public static final Object getIteration(Iterator iter,
                                        int n)
Return the nth next Object from an Iterator. n counts from 0, so getIteration(iter,0) is equivalent to iter.next().

Parameters:
iter - the Iterator
n - the number of items to skip
Returns:
the next item