KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bidimap > DualHashBidiMap


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.bidimap;
17
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.commons.collections.BidiMap;
26
27 /**
28  * Implementation of <code>BidiMap</code> that uses two <code>HashMap</code> instances.
29  * <p>
30  * Two <code>HashMap</code> instances are used in this class.
31  * This provides fast lookups at the expense of storing two sets of map entries.
32  * Commons Collections would welcome the addition of a direct hash-based
33  * implementation of the <code>BidiMap</code> interface.
34  * <p>
35  * NOTE: From Commons Collections 3.1, all subclasses will use <code>HashMap</code>
36  * and the flawed <code>createMap</code> method is ignored.
37  *
38  * @since Commons Collections 3.0
39  * @version $Id: DualHashBidiMap.java,v 1.7 2004/06/11 23:27:37 scolebourne Exp $
40  *
41  * @author Matthew Hawthorne
42  * @author Stephen Colebourne
43  */

44 public class DualHashBidiMap
45         extends AbstractDualBidiMap implements Serializable JavaDoc {
46
47     /** Ensure serialization compatibility */
48     private static final long serialVersionUID = 721969328361808L;
49
50     /**
51      * Creates an empty <code>HashBidiMap</code>.
52      */

53     public DualHashBidiMap() {
54         super(new HashMap JavaDoc(), new HashMap JavaDoc());
55     }
56
57     /**
58      * Constructs a <code>HashBidiMap</code> and copies the mappings from
59      * specified <code>Map</code>.
60      *
61      * @param map the map whose mappings are to be placed in this map
62      */

63     public DualHashBidiMap(Map JavaDoc map) {
64         super(new HashMap JavaDoc(), new HashMap JavaDoc());
65         putAll(map);
66     }
67     
68     /**
69      * Constructs a <code>HashBidiMap</code> that decorates the specified maps.
70      *
71      * @param normalMap the normal direction map
72      * @param reverseMap the reverse direction map
73      * @param inverseBidiMap the inverse BidiMap
74      */

75     protected DualHashBidiMap(Map JavaDoc normalMap, Map JavaDoc reverseMap, BidiMap inverseBidiMap) {
76         super(normalMap, reverseMap, inverseBidiMap);
77     }
78
79     /**
80      * Creates a new instance of this object.
81      *
82      * @param normalMap the normal direction map
83      * @param reverseMap the reverse direction map
84      * @param inverseBidiMap the inverse BidiMap
85      * @return new bidi map
86      */

87     protected BidiMap createBidiMap(Map JavaDoc normalMap, Map JavaDoc reverseMap, BidiMap inverseBidiMap) {
88         return new DualHashBidiMap(normalMap, reverseMap, inverseBidiMap);
89     }
90
91     // Serialization
92
//-----------------------------------------------------------------------
93
private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
94         out.defaultWriteObject();
95         out.writeObject(maps[0]);
96     }
97
98     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
99         in.defaultReadObject();
100         maps[0] = new HashMap JavaDoc();
101         maps[1] = new HashMap JavaDoc();
102         Map JavaDoc map = (Map JavaDoc) in.readObject();
103         putAll(map);
104     }
105
106 }
107
Popular Tags