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