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.HashMap;
42  import java.util.Map;
43  import java.util.Iterator;
44  
45  /**
46   * A Map handler for adding and retreiving key-value pairs from A map. A map handler is instantiated
47   * only once, must be thread safe and not use any synchronization.
48   *
49   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
50   * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
51   */
52  public final class J2MapHandler implements MapHandler {
53  
54    /**
55     * Creates a new Instance of the map represented by this MapHandler.
56     *
57     * @return the new map.
58     */
59    public Object create() {
60      return new HashMap();
61    } // -- create
62  
63    /**
64     * Adds the given key-value pair to the map. Keys must be unique. Adding a key-value pair to the
65     * map, when an existing association for that key already exists will cause the existing
66     * association to be overwritten.
67     * 
68     * The map is provided as a parameter and is returned as the return value if the returned map is a
69     * different object. That way the handler can create a new map if necessary.
70     *
71     * @param map the map, null if no map has been created yet.
72     * @param key the key for the object.
73     * @param object the object to add to the map.
74     * @return The map with the new object if a different instance than the <tt>map</tt> parameter,
75     *         null otherwise
76     * @throws ClassCastException The MapHandler does not support maps of the given type.
77     */
78    public Object put(Object map, Object key, Object object) throws ClassCastException {
79      Object returnVal = null;
80      if (map == null) {
81        map = create();
82        returnVal = map;
83      }
84      ((Map) map).put(key, object);
85      return returnVal;
86    } // -- put
87  
88  
89    /**
90     * Returns an enumeration of all the objects in the Map.
91     *
92     * @param map The map instance for which to return the enumeration of elements for.
93     * @return An enumeration of all the elements in the Map.
94     * @throws ClassCastException The MapHandler does not support collections of this type
95     */
96    public Enumeration elements(Object map) throws ClassCastException {
97      if (map == null)
98        map = create();
99      return new IteratorEnumerator(((Map) map).values().iterator());
100   } // -- elements
101 
102   /**
103    * Returns an enumeration of all the keys in the Map.
104    *
105    * @param map The map instance for which to return the enumeration of keys.
106    * @return An enumeration of all the keys in the Map.
107    * @throws ClassCastException The MapHandler does not support collections of this type
108    */
109   public Enumeration keys(Object map) throws ClassCastException {
110     if (map == null)
111       map = create();
112     return new IteratorEnumerator(((Map) map).keySet().iterator());
113   } // -- keys
114 
115 
116   /**
117    * Returns the number of elements (key-value) in the map.
118    *
119    * @param map the map.
120    * @return Number of key-value associations in the Map
121    * @throws ClassCastException The MapHandler does not support collections of the given type.
122    */
123   public int size(Object map) throws ClassCastException {
124     if (map == null)
125       return 0;
126     return ((Map) map).size();
127   } // -- size
128 
129 
130   /**
131    * Clears the map of all key-value pairs.
132    *
133    * @param map the map to clear.
134    * @throws ClassCastException The MapHandler does not support collections of the given type.
135    */
136   public void clear(Object map) throws ClassCastException {
137     if (map == null)
138       return;
139     ((Map) map).clear();
140   } // -- clear
141 
142   /**
143    * Returns the object associated with the given key.
144    *
145    * @param map the map to return the object from.
146    * @param key the key for the object.
147    * @return the object associated with the given key, or null if no association was found in the
148    *         given map.
149    * @throws ClassCastException The MapHandler does not support maps of the given type.
150    */
151   public Object get(Object map, Object key) throws ClassCastException {
152     if (map == null)
153       return null;
154     return ((Map) map).get(key);
155   } // -- get
156 
157   /**
158    * Enumerator for an iterator.
159    */
160   static final class IteratorEnumerator implements Enumeration {
161 
162     private final Iterator _iterator;
163 
164     IteratorEnumerator(Iterator iterator) {
165       _iterator = iterator;
166     }
167 
168     public boolean hasMoreElements() {
169       return _iterator.hasNext();
170     }
171 
172     public Object nextElement() {
173       return _iterator.next();
174     }
175 
176   }
177 
178 } // -- J2MapHandler
179 
180