KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > FastHashMap


1 //$Id: FastHashMap.java,v 1.1 2004/06/03 16:31:30 steveebersole Exp $
2
/*
3  * ====================================================================
4  *
5  * The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution, if
23  * any, must include the following acknowlegement:
24  * "This product includes software developed by the
25  * Apache Software Foundation (http://www.apache.org/)."
26  * Alternately, this acknowlegement may appear in the software itself,
27  * if and wherever such third-party acknowlegements normally appear.
28  *
29  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
30  * Foundation" must not be used to endorse or promote products derived
31  * from this software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache"
35  * nor may "Apache" appear in their names without prior written
36  * permission of the Apache Group.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  *
57  */

58
59 // This class was taken from Apache commons-collections -
60
// I made it final + removed the "slow" mode...
61

62 package org.hibernate.util;
63
64 import java.io.Serializable JavaDoc;
65 import java.util.Collection JavaDoc;
66 import java.util.HashMap JavaDoc;
67 import java.util.Map JavaDoc;
68 import java.util.Set JavaDoc;
69
70 /**
71  * <p>A customized implementation of <code>java.util.HashMap</code> designed
72  * to operate in a multithreaded environment where the large majority of
73  * method calls are read-only, instead of structural changes.
74  * Read calls are non-synchronized and write calls perform the
75  * following steps:</p>
76  * <ul>
77  * <li>Clone the existing collection
78  * <li>Perform the modification on the clone
79  * <li>Replace the existing collection with the (modified) clone
80  * </ul>
81  * <p><strong>NOTE</strong>: If you are creating and accessing a
82  * <code>HashMap</code> only within a single thread, you should use
83  * <code>java.util.HashMap</code> directly (with no synchronization), for
84  * maximum performance.</p>
85  *
86  */

87
88 public final class FastHashMap implements Map JavaDoc, Serializable JavaDoc {
89
90     // ----------------------------------------------------------- Constructors
91

92     /**
93      * Construct a an empty map.
94      */

95     public FastHashMap() {
96
97         super();
98         this.map = new HashMap JavaDoc();
99
100     }
101
102     /**
103      * Construct an empty map with the specified capacity.
104      *
105      * @param capacity The initial capacity of the empty map
106      */

107     public FastHashMap(int capacity) {
108
109         super();
110         this.map = new HashMap JavaDoc(capacity);
111
112     }
113
114     /**
115      * Construct an empty map with the specified capacity and load factor.
116      *
117      * @param capacity The initial capacity of the empty map
118      * @param factor The load factor of the new map
119      */

120     public FastHashMap(int capacity, float factor) {
121
122         super();
123         this.map = new HashMap JavaDoc(capacity, factor);
124
125     }
126
127     /**
128      * Construct a new map with the same mappings as the specified map.
129      *
130      * @param map The map whose mappings are to be copied
131      */

132     public FastHashMap(Map JavaDoc map) {
133
134         super();
135         this.map = new HashMap JavaDoc(map);
136
137     }
138
139     // ----------------------------------------------------- Instance Variables
140

141     /**
142      * The underlying map we are managing.
143      */

144     private HashMap JavaDoc map = null;
145
146     // --------------------------------------------------------- Public Methods
147

148     /**
149      * Remove all mappings from this map.
150      */

151     public void clear() {
152
153         synchronized (this) {
154             HashMap JavaDoc temp = (HashMap JavaDoc) map.clone();
155             temp.clear();
156             map = temp;
157         }
158
159     }
160
161     /**
162      * Return a shallow copy of this <code>FastHashMap</code> instance.
163      * The keys and values themselves are not copied.
164      */

165     public Object JavaDoc clone() {
166
167         return new FastHashMap(map);
168
169     }
170
171     /**
172      * Return <code>true</code> if this map contains a mapping for the
173      * specified key.
174      *
175      * @param key Key to be searched for
176      */

177     public boolean containsKey(Object JavaDoc key) {
178
179         return map.containsKey(key);
180
181     }
182
183     /**
184      * Return <code>true</code> if this map contains one or more keys mapping
185      * to the specified value.
186      *
187      * @param value Value to be searched for
188      */

189     public boolean containsValue(Object JavaDoc value) {
190
191         return map.containsValue(value);
192
193     }
194
195     /**
196      * Return a collection view of the mappings contained in this map. Each
197      * element in the returned collection is a <code>Map.Entry</code>.
198      */

199     public Set JavaDoc entrySet() {
200
201         return map.entrySet();
202
203     }
204
205     /**
206      * Compare the specified object with this list for equality. This
207      * implementation uses exactly the code that is used to define the
208      * list equals function in the documentation for the
209      * <code>Map.equals</code> method.
210      *
211      * @param o Object to be compared to this list
212      */

213     public boolean equals(Object JavaDoc o) {
214
215         // Simple tests that require no synchronization
216
if (o == this)
217             return true;
218         else if ( !(o instanceof Map JavaDoc) )
219             return false;
220         Map JavaDoc mo = (Map JavaDoc) o;
221
222         // Compare the two maps for equality
223

224         if ( mo.size() != map.size() )
225             return false;
226         java.util.Iterator JavaDoc i = map.entrySet().iterator();
227         while ( i.hasNext() ) {
228             Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
229             Object JavaDoc key = e.getKey();
230             Object JavaDoc value = e.getValue();
231             if (value == null) {
232                 if ( !( mo.get(key) == null && mo.containsKey(key) ) )
233                     return false;
234             }
235             else {
236                 if ( !value.equals( mo.get(key) ) )
237                     return false;
238             }
239         }
240         return true;
241
242     }
243
244     /**
245      * Return the value to which this map maps the specified key. Returns
246      * <code>null</code> if the map contains no mapping for this key, or if
247      * there is a mapping with a value of <code>null</code>. Use the
248      * <code>containsKey()</code> method to disambiguate these cases.
249      *
250      * @param key Key whose value is to be returned
251      */

252     public Object JavaDoc get(Object JavaDoc key) {
253
254         return map.get(key);
255
256     }
257
258     /**
259      * Return the hash code value for this map. This implementation uses
260      * exactly the code that is used to define the list hash function in the
261      * documentation for the <code>Map.hashCode</code> method.
262      */

263     public int hashCode() {
264
265         int h = 0;
266         java.util.Iterator JavaDoc i = map.entrySet().iterator();
267         while ( i.hasNext() )
268             h += i.next().hashCode();
269         return h;
270
271     }
272
273     /**
274      * Return <code>true</code> if this map contains no mappings.
275      */

276     public boolean isEmpty() {
277
278         return map.isEmpty();
279
280     }
281
282     /**
283      * Return a set view of the keys contained in this map.
284      */

285     public Set JavaDoc keySet() {
286
287         return map.keySet();
288
289     }
290
291     /**
292      * Associate the specified value with the specified key in this map.
293      * If the map previously contained a mapping for this key, the old
294      * value is replaced and returned.
295      *
296      * @param key The key with which the value is to be associated
297      * @param value The value to be associated with this key
298      */

299     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
300
301         synchronized (this) {
302             HashMap JavaDoc temp = (HashMap JavaDoc) map.clone();
303             Object JavaDoc result = temp.put(key, value);
304             map = temp;
305             return (result);
306         }
307
308     }
309
310     /**
311      * Copy all of the mappings from the specified map to this one, replacing
312      * any mappings with the same keys.
313      *
314      * @param in Map whose mappings are to be copied
315      */

316     public void putAll(Map JavaDoc in) {
317
318         synchronized (this) {
319             HashMap JavaDoc temp = (HashMap JavaDoc) map.clone();
320             temp.putAll(in);
321             map = temp;
322         }
323
324     }
325
326     /**
327      * Remove any mapping for this key, and return any previously
328      * mapped value.
329      *
330      * @param key Key whose mapping is to be removed
331      */

332     public Object JavaDoc remove(Object JavaDoc key) {
333
334         synchronized (this) {
335             HashMap JavaDoc temp = (HashMap JavaDoc) map.clone();
336             Object JavaDoc result = temp.remove(key);
337             map = temp;
338             return (result);
339         }
340
341     }
342
343     /**
344      * Return the number of key-value mappings in this map.
345      */

346     public int size() {
347
348         return map.size();
349
350     }
351
352     /**
353      * Return a collection view of the values contained in this map.
354      */

355     public Collection JavaDoc values() {
356
357         return map.values();
358
359     }
360
361     public String JavaDoc toString() { return map.toString(); }
362
363 }
364
365
366
367
368
369
Popular Tags