/*
 * Created on 17-ene-2005
 *
 */
package es.dalocar.software;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Eduardo Millán
 *
 * A Map based Echo Context implementation 
 * 
 */
public class DefaultContextImpl implements Context {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1153721111406159949L;
	Map map = null;
    
    private class IteratedEnumeration implements Enumeration {

        Iterator iterator;
        /**
         * @param iterator The iterator to wrap
         *  
         */
        public IteratedEnumeration(Iterator iterator) {
            this.iterator = iterator;
        }
        
        /**
         * @see java.util.Enumeration#hasMoreElements()
         */
        public boolean hasMoreElements() {
            return iterator.hasNext();
        }

        /**
         * @see java.util.Enumeration#nextElement()
         */
        public Object nextElement() {
            return iterator.next();
        }
        
    }
    
    
    /** Default constructor */
    public DefaultContextImpl() {
        map = new HashMap();
    }
    
    /**
     * @see es.dalocar.software.saloni.echo.Context#setAttribute(java.lang.String, java.lang.Object)
     */
    public void setAttribute(String key, Object value) {
        map.put(key, value);
    }

    /**
     * @see es.dalocar.software.saloni.echo.Context#getAttribute(java.lang.String)
     */
    public Object getAttribute(String key) {
        return map.get(key);
    }

    /**
     * @see es.dalocar.software.saloni.echo.Context#clear()
     */
    public void clear() {
        map.clear();
    }

    /**
     * @see es.dalocar.software.saloni.echo.Context#getAttributeNames()
     */
    public Enumeration getAttributeNames() {
        return new IteratedEnumeration(map.keySet().iterator());
    }

    /**
     * @see es.dalocar.software.saloni.echo.Context#removeAttribute(java.lang.String)
     */
    public void removeAttribute(String name) {
        map.remove(name);
    }

}
