/*
 * @(#)IteratorFactory.java    2.3  8 December 2003
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.quick;

import java.util.*;

/**
 * <P><CODE>IteratorFactory</CODE> encapsulates a static method to construct
 * a Java <CODE>Iterator</CODE> from an <CODE>Object[]</CODE> array.</P>
 *
 * <P>This class cannot be instantiated.</P>
 *
 * @author  Richard Rasala
 * @version 2.3
 * @since   2.3
 */
public final class IteratorFactory {
    
    /** The private constructor to prevent instantiation. */
    private IteratorFactory() { }
    
    
    /**
     * <P>Returns an <CODE>Iterator</CODE> that iterates over the data
     * in the given array.  To make the iterator independent of the
     * original array, a shallow copy of the array data is made.</P>
     *
     * <P>Throws <CODE>UnsupportedOperationException</CODE> if the
     * method <CODE>remove()</CODE> is called.</P>
     *
     * @param  the array whose data will be the focus of iteration
     * @return the iterator that iterates over the array data
     */
    public static Iterator makeIterator(Object[] array) {
        int length = (array == null) ? 0 : array.length;
        
        final Object[] copy = new Object[length];
        
        for (int i = 0; i < length; i++)
            copy[i] = array[i];
        
        
        return new Iterator() {
            Object[] data = copy;
            
            private int size = data.length;
            private int index = 0;
            
            
            public boolean hasNext() {
                return (index < size);
            }
            
            
            public Object next() {
                if (index < size)
                    return data[index++];
                else
                    throw new NoSuchElementException
                        ("The iterator has no more data");
            }
            
            
            public void remove() {
                throw new UnsupportedOperationException
                    ("The iterator does not support remove()");
            }
        };
    }
    
}
