package com.twc.isa.ServerModule;

import java.util.Hashtable;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.omg.CORBA.BAD_PARAM;


final class ServantBaseIterator_impl extends ServantBaseIteratorPOA {
	
	private static final Logger log = Logger.getLogger(ServantBaseIterator_impl.class);
	
    private Vector m_Servants;
    private int m_iIdx;
    
    ServantBaseIterator_impl(int iHowMany,Hashtable hash) {
        m_iIdx  = iHowMany;
        m_Servants = new Vector( hash.values());
    }    
    
    synchronized public boolean 
    next_one(ServantBaseHolder sbh) {
        
        if (m_iIdx < m_Servants.size()) {
            m_Servants.get( m_iIdx);
            sbh.value = (ServantBase)m_Servants.get( m_iIdx++);
            return true;
        }
        
        sbh.value = null;
        return false;
        
    }
    
    synchronized public boolean 
    next_n(int how_many, ServantBaseListHolder sblh) {
        
        if (how_many == 0) {
        	throw new BAD_PARAM();
        }
        
        int len = m_Servants.size();
        int num = len - m_iIdx < how_many ? len - m_iIdx : how_many;
        
        sblh.value = new ServantBase[num];
        java.lang.Object [] temp  = m_Servants.subList(m_iIdx,m_iIdx+num).toArray();
        System.arraycopy(temp, 0, sblh.value, 0, num);
        m_iIdx += num;
        
        return num != 0;
    }
    
    synchronized public void 
    destroy() {
        ServantBase_.destroy(this);
    }
    
    public boolean 
    get_n(int start, int how_many, com.twc.isa.ServerModule.ServantBaseListHolder servantBaseListHolder) {
        
    	if(how_many == 0) {
    		throw new BAD_PARAM();
    	}
    	
        int len = m_Servants.size();
        
        if (start > len) {
        	throw new BAD_PARAM();       
        }
        
        int num = len - start < how_many ? len - start : how_many;  
        
        servantBaseListHolder.value = new ServantBase[num];    
        java.lang.Object [] temp  = m_Servants.subList(start,start+num).toArray();
        System.arraycopy(temp, 0, servantBaseListHolder.value, 0, num);
        
        return true;        
    } 
}
