/*
Copyright 2002-2005 The Members of the OSS through Java(TM) Initiative.
All rights reserved. Use is subject to license terms.
 */
package ossj.common;

import java.lang.reflect.Array;
import java.util.Vector;

import javax.oss.ManagedEntityValue;
import javax.oss.ManagedEntityValueIterator;


/**
 * DOCUMENT ME!
 *
 * @author OSS through Java Initiative, Vincent Perrot Sun Microsystems Inc.
 * @version 1.1
 * @since March 2005
 */
public class ManagedEntityValueIteratorImpl implements ManagedEntityValueIterator {
    private Vector valueVector;
    private Class eltClass;
    
    /**
     * Creates an empty Iterator for ManagedEntityValues.
     * The CBEManagedEntityValueImpl is assumed for the element Class type.
     *
     */
    public ManagedEntityValueIteratorImpl() {
        valueVector = new Vector();
        eltClass = null;
    }
    
    /**
     * Creates a new ManagedEntityValueIteratorImpl object.
     *
     * @param values array of element to add. It Shall not be null.
     */
    public ManagedEntityValueIteratorImpl(ManagedEntityValue[] values) {
        valueVector = new Vector();
        if (values == null) {
            throw new IllegalArgumentException("null is not a valid attribute for this constructor");
        } else {
            String arrayName = values.getClass().getName();
            String eltName = arrayName.substring(2,arrayName.length()-1);
            try {
                eltClass = Class.forName(eltName);
            } catch (Exception e) {
                throw new IllegalArgumentException("Unable to get the element class for the given arry ["+arrayName+"]");
            }
            addManagedEntityValues(values);
        }
    }
    
    /**
     * Adds all the values specified in the given array, increasing its size
     * by the number of elements.
     *
     * @param results
     */
    public void addManagedEntityValues(ManagedEntityValue[] values) {
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                valueVector.addElement(values[i]);
            }
        } else {
            throw new IllegalArgumentException("null is not supported for Iterator");
        }
    }
    
    /**
     * Returns the next specified number of elements in the iteration.
     * @param howMany
     * @return
     * @throws java.rmi.RemoteException
     */
    public ManagedEntityValue[] getNext(int howMany) throws java.rmi.RemoteException {
        int sz = howMany;
        
        if (valueVector.size() < howMany) {
            sz = valueVector.size();
        }
        
        // Fix issue #45 issue #50
        //ManagedEntityValue[] retObjs = new ManagedEntityValue[sz];
        ManagedEntityValue[] retObjs = null;
        if (valueVector.isEmpty()){
            if (eltClass == null){
                retObjs = null;
            } else {
                retObjs = (ManagedEntityValue[])Array.newInstance(eltClass,0);
            }
        } else {
            if (eltClass == null){
                eltClass = valueVector.firstElement().getClass();
            }
            retObjs = (ManagedEntityValue[])Array.newInstance(eltClass,sz);
            
            for (int x = 0; x < sz; x++) {
                retObjs[x] = (ManagedEntityValue) valueVector.firstElement();
                valueVector.removeElementAt(0);
            }
        }
        return retObjs;
    }
    
    /**
     * Removes all the element from the Iterator.
     * @throws java.rmi.RemoteException
     * @throws javax.ejb.RemoveException
     */
    public void remove() throws java.rmi.RemoteException, javax.ejb.RemoveException {
        valueVector.clear();
    }
}
