001/*
002 * www.openamf.org
003 *
004 * Distributable under LGPL license. See terms of license at gnu.org.
005 */
006
007package flex.messaging.io;
008
009import java.util.HashMap;
010
011/**
012 * Implementation of MM's flashgateway.io.ASObject so that we can use
013 * ASTranslator
014 *
015 * @author Jason Calabrese <mail@jasoncalabrese.com>
016 * @author Sean C. Sullivan
017 *
018 * @version $Revision: 1.11 $, $Date: 2004/02/06 02:48:59 $
019 */
020public class ASObject extends HashMap<String, Object> {
021
022    /**
023     *
024     */
025    private static final long serialVersionUID = 1L;
026
027    /**
028     * Object type
029     */
030    private String type;
031
032    public ASObject() {
033        super();
034    }
035
036    /**
037     * Creates ASObject with type
038     *
039     * @param type
040     */
041    public ASObject(String type) {
042        super();
043        this.type = type;
044    }
045
046    /**
047     * Gets object type
048     *
049     * @return @see #setType(String)
050     *
051     */
052    public String getType() {
053        return type;
054    }
055
056    /**
057     * Sets object type
058     *
059     * @param type
060     *
061     * @see #getType()
062     *
063     */
064    public void setType(String type) {
065        this.type = type;
066    }
067
068    /**
069     * Returns <tt>true</tt> if this map contains a mapping for the specified
070     * key. <br>
071     *
072     * @param key
073     *                The key whose presence in this map is to be tested
074     * @return <tt>true</tt> if this map contains a mapping for the specified
075     *            key.
076     */
077    @Override
078    public boolean containsKey(Object key) {
079        return super.containsKey(toLowerCase(key));
080    }
081
082    /**
083     * Returns the value to which the specified key is mapped in this identity
084     * hash map, or <tt>null</tt> if the map contains no mapping for this
085     * key. A return value of <tt>null</tt> does not <i>necessarily</i>
086     * indicate that the map contains no mapping for the key; it is also
087     * possible that the map explicitly maps the key to <tt>null</tt>. The
088     * <tt>containsKey</tt> method may be used to distinguish these two
089     * cases.
090     *
091     * @param key
092     *                the key whose associated value is to be returned.
093     * @return the value to which this map maps the specified key, or <tt>null</tt>
094     *            if the map contains no mapping for this key.
095     * @see #put(Object, Object)
096     */
097    @Override
098    public Object get(Object key) {
099        return super.get(toLowerCase(key));
100    }
101
102    /**
103     * Associates the specified value with the specified key in this map. If
104     * the map previously contained a mapping for this key, the old value is
105     * replaced.
106     *
107     * @param key
108     *                key with which the specified value is to be associated.
109     * @param value
110     *                value to be associated with the specified key.
111     * @return previous value associated with specified key, or <tt>null</tt>
112     *            if there was no mapping for key. A <tt>null</tt> return can
113     *            also indicate that the HashMap previously associated <tt>null</tt>
114     *            with the specified key.
115     */
116    @Override
117    public Object put(String key, Object value) {
118        return super.put((String)toLowerCase(key), value);
119    }
120
121    /**
122     * Removes the mapping for this key from this map if present.
123     *
124     * @param key
125     *                key whose mapping is to be removed from the map.
126     * @return previous value associated with specified key, or <tt>null</tt>
127     *            if there was no mapping for key. A <tt>null</tt> return can
128     *            also indicate that the map previously associated <tt>null</tt>
129     *            with the specified key.
130     */
131    @Override
132    public Object remove(Object key) {
133        return super.remove(toLowerCase(key));
134    }
135
136    /**
137     * Gets lower case object if object was instance of String
138     *
139     * @param key
140     * @return
141     */
142    private Object toLowerCase(Object key) {
143        /*if (key != null
144            && key instanceof String
145            && amfSerializerConfig.forceLowerCaseKeys()) {
146            key = ((String) key).toLowerCase();
147        }*/
148        return key;
149    }
150
151    /**
152     * @return this method may return null
153     *
154     * @see #setType(String)
155     * @see #getType()
156     *
157     */
158    public Object instantiate() {
159        Object ret;
160        try {
161            ClassLoader loader = Thread.currentThread().getContextClassLoader();
162            Class<?> clazz = loader.loadClass(type);
163            ret = clazz.newInstance();
164        } catch (Exception e) {
165            ret = null;
166        }
167        return ret;
168    }
169
170    @Override
171    public String toString() {
172        return "ASObject[type=" + getType() + "," + super.toString() + "]";
173    }
174}