package org.mortbay.cometd;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.mortbay.util.ajax.JSON;

/* ------------------------------------------------------------ */
/** A Bayeux Message
 * A Map of String to Object that has been optimized for conversion to JSON messages.
 * 
 * @author gregw
 *
 */
public class Message extends HashMap<String, Object> implements org.mortbay.util.ajax.JSON.Generator
{
    String _json;

    /* ------------------------------------------------------------ */
    public void addJSON(StringBuffer buffer)
    {
        buffer.append(getJSON());
    }

    /* ------------------------------------------------------------ */
    public String getJSON()
    {
        if (_json==null)
            _json=JSON.toString(this);
        return _json;
    }
    
    /* ------------------------------------------------------------ */
    /* (non-Javadoc)
     * @see java.util.HashMap#clear()
     */
    public void clear()
    {
        _json=null;
        super.clear();
    }

    /* ------------------------------------------------------------ */
    /* (non-Javadoc)
     * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
     */
    public Object put(String key, Object value)
    {
        _json=null;
        return super.put(key,value);
    }

    /* ------------------------------------------------------------ */
    /* (non-Javadoc)
     * @see java.util.HashMap#putAll(java.util.Map)
     */
    public void putAll(Map<? extends String, ? extends Object> m)
    {
        _json=null;
        super.putAll(m);
    }

    /* ------------------------------------------------------------ */
    /* (non-Javadoc)
     * @see java.util.HashMap#remove(java.lang.Object)
     */
    public Object remove(Object key)
    {
        _json=null;
        return super.remove(key);
    }

    /* ------------------------------------------------------------ */
    /* (non-Javadoc)
     * @see java.util.HashMap#entrySet()
     */
    public Set<java.util.Map.Entry<String, Object>> entrySet()
    {
        return Collections.unmodifiableSet(super.entrySet());
    }

    /* ------------------------------------------------------------ */
    /* (non-Javadoc)
     * @see java.util.HashMap#keySet()
     */
    public Set<String> keySet()
    {
        return Collections.unmodifiableSet(super.keySet());
    }
    
    

}
