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

/**
 * Abstract base class for all attributes/sessions/cookies maps in external
 * contexts.
 * 
 * @author asmirnov
 * 
 */
public abstract class ContextAttributesMap extends ContextMap {
    public Object get(Object key) {
	if (null == key) {
	    throw new NullPointerException();
	}
	return getAttribute(key.toString());
    }

    public Object put(Object key, Object value) {
	if (null == key) {
	    throw new NullPointerException();
	}
	String stringKey = key.toString();
	Object oldValue = getAttribute(stringKey);
	setAttribute(stringKey, value);
	return oldValue;
    }

    public Object remove(Object key) {
	if (null == key) {
	    throw new NullPointerException();
	}
	String stringKey = key.toString();
	Object oldValue = getAttribute(stringKey);
	removeAttribute(stringKey);
	return oldValue;
    }

    protected abstract Object getAttribute(String name);

    protected abstract void setAttribute(String name, Object value);

    protected void removeAttribute(String name) {
	setAttribute(name, null);
    }
}
