View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2002 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  
37  package org.exolab.castor.mapping.handlers;
38  
39  import org.exolab.castor.mapping.MapHandler;
40  import java.util.Enumeration;
41  import java.util.Hashtable;
42  
43  /**
44   * A Map handler for adding and retreiving key-value pairs from A map. A map handler is instantiated
45   * only once, must be thread safe and not use any synchronization.
46   *
47   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
48   * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
49   */
50  public final class J1MapHandler implements MapHandler {
51  
52    /**
53     * Creates a new Instance of the map represented by this MapHandler.
54     *
55     * @return the new map.
56     */
57    public Object create() {
58      return new Hashtable();
59    } // -- create
60  
61    /**
62     * Adds the given key-value pair to the map. Keys must be unique. Adding a key-value pair to the
63     * map, when an existing association for that key already exists will cause the existing
64     * association to be overwritten.
65     * 
66     * The map is provided as a parameter and is returned as the return value if the returned map is a
67     * different object. That way the handler can create a new map if necessary.
68     *
69     * @param map the map, null if no map has been created yet.
70     * @param key the key for the object.
71     * @param object the object to add to the map.
72     * @return The map with the new object if a different instance than the <tt>map</tt> parameter,
73     *         null otherwise
74     * @throws ClassCastException The MapHandler does not support maps of the given type.
75     */
76    public Object put(Object map, Object key, Object object) throws ClassCastException {
77      Object returnVal = null;
78      if (map == null) {
79        map = new Hashtable();
80        returnVal = map;
81      }
82      ((Hashtable) map).put(key, object);
83      return returnVal;
84    } // -- put
85  
86  
87    /**
88     * Returns an enumeration of all the objects in the Map.
89     *
90     * @param map The map instance for which to return the enumeration of elements for.
91     * @return An enumeration of all the elements in the Map.
92     * @throws ClassCastException The MapHandler does not support collections of this type
93     */
94    public Enumeration elements(Object map) throws ClassCastException {
95      if (map == null)
96        map = new Hashtable();
97      return ((Hashtable) map).elements();
98    } // -- elements
99  
100   /**
101    * Returns an enumeration of all the keys in the Map.
102    *
103    * @param map The map instance for which to return the enumeration of keys.
104    * @return An enumeration of all the keys in the Map.
105    * @throws ClassCastException The MapHandler does not support collections of this type
106    */
107   public Enumeration keys(Object map) throws ClassCastException {
108     if (map == null)
109       map = new Hashtable();
110     return ((Hashtable) map).keys();
111   } // -- keys
112 
113 
114   /**
115    * Returns the number of elements (key-value) in the map.
116    *
117    * @param map the map.
118    * @return Number of key-value associations in the Map
119    * @throws ClassCastException The MapHandler does not support collections of the given type.
120    */
121   public int size(Object map) throws ClassCastException {
122     if (map == null)
123       return 0;
124     return ((Hashtable) map).size();
125   } // -- size
126 
127 
128   /**
129    * Clears the map of all key-value pairs.
130    *
131    * @param map the map to clear.
132    * @throws ClassCastException The MapHandler does not support collections of the given type.
133    */
134   public void clear(Object map) throws ClassCastException {
135     if (map == null)
136       return;
137     ((Hashtable) map).clear();
138   } // -- clear
139 
140   /**
141    * Returns the object associated with the given key.
142    *
143    * @param map the map to return the object from.
144    * @param key the key for the object.
145    * @return the object associated with the given key, or null if no association was found in the
146    *         given map.
147    * @throws ClassCastException The MapHandler does not support maps of the given type.
148    */
149   public Object get(Object map, Object key) throws ClassCastException {
150     if (map == null)
151       return null;
152     return ((Hashtable) map).get(key);
153   } // -- get
154 
155 } // -- J1MapHandler
156 
157