/**
 * 
 */
package org.ajax4jsf.portlet.context;

import java.util.Enumeration;

/**
 * @author asmirnov
 *
 */
public class SessionAttributesNames implements Enumeration {
    
    private Enumeration names;
    
    private String prefix;
    
    private Object next;
    
    
    /**
     * @param names
     * @param prefix
     */
    public SessionAttributesNames(Enumeration names, String prefix) {
	this.names = names;
	this.prefix = prefix;
	setNext();
    }

    /* (non-Javadoc)
     * @see java.util.Enumeration#hasMoreElements()
     */
    public boolean hasMoreElements() {
	return null != next;
    }

    /* (non-Javadoc)
     * @see java.util.Enumeration#nextElement()
     */
    public Object nextElement() {
	Object nextName = next;
	setNext();
	return nextName;
    }
    
    private void setNext(){
	next = null;
	while (names.hasMoreElements() && null == next) {
	    String name = (String) names.nextElement();
	    if(name.startsWith(prefix)){
		next = name.substring(prefix.length());
	    }
	}
    }
}
