/*
 * Copyright 2000-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jetspeed.profiler.impl;

import java.util.Iterator;
import java.util.List;

import org.apache.jetspeed.profiler.ProfileLocatorProperty;
import org.apache.jetspeed.profiler.rules.RuleCriterion;

/**
 * ProfileFallbackIterator
 *
 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 * @version $Id: ProfileFallbackIterator.java,v 1.2 2004/10/15 22:58:43 ate Exp $
 */
public class ProfileFallbackIterator implements Iterator
{
    private ProfileLocatorControl locator;
    private int last = 0;
    private int state = RuleCriterion.FALLBACK_CONTINUE;     
    private ProfileFallbackIterator()
    {
    }
    
    public ProfileFallbackIterator(ProfileLocatorControl locator)
    {
        this.locator = locator;
        last = locator.getElements().size() - 1;
    }
    
    /* (non-Javadoc)
     * @see java.util.Iterator#remove()
     */
    public void remove()
    {
        // TODO Auto-generated method stub
    }
    
    /* (non-Javadoc)
     * @see java.util.Iterator#hasNext()
     */
    public boolean hasNext()
    {
        boolean hasNext = false;
        
        List elements = locator.getElements();
        
        if (last < 0 || last >= elements.size())
        {
            state = RuleCriterion.FALLBACK_STOP;
            return false;
        }
        
        if (state == RuleCriterion.FALLBACK_STOP)
        {
            hasNext = false;
        }        
        else if (state == RuleCriterion.FALLBACK_CONTINUE ||
                 state == RuleCriterion.FALLBACK_LOOP)
        {
            hasNext = true;
        }
        
        ProfileLocatorPropertyImpl element = (ProfileLocatorPropertyImpl)elements.get(last);
        state = element.getFallbackType();
                
        return hasNext;
    }
    
    /* (non-Javadoc)
     * @see java.util.Iterator#next()
     */
    public Object next()
    {
        ProfileLocatorProperty [] properties = null;

        if (last >= 0)
        {
            List elements = locator.getElements();
            properties = new ProfileLocatorProperty[last+1];
            Iterator it = elements.listIterator();
            for (int count = 0; (count <= last) && it.hasNext(); count++)
                properties[count] = (ProfileLocatorProperty)it.next();
            last--;
        }

        return properties;
    }
}
