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