1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections.bidimap;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.commons.collections.BidiMap;
27
28 /**
29 * Implementation of {@link BidiMap} that uses two {@link HashMap} instances.
30 * <p>
31 * Two {@link HashMap} instances are used in this class.
32 * This provides fast lookups at the expense of storing two sets of map entries.
33 * Commons Collections would welcome the addition of a direct hash-based
34 * implementation of the {@link BidiMap} interface.
35 * <p>
36 * NOTE: From Commons Collections 3.1, all subclasses will use {@link HashMap}
37 * and the flawed <code>createMap</code> method is ignored.
38 *
39 * @since 3.0
40 * @version $Id: DualHashBidiMap.java 1443602 2013-02-07 17:00:23Z tn $
41 */
42 public class DualHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements Serializable {
43
44 /** Ensure serialization compatibility */
45 private static final long serialVersionUID = 721969328361808L;
46
47 /**
48 * Creates an empty <code>HashBidiMap</code>.
49 */
50 public DualHashBidiMap() {
51 super(new HashMap<K, V>(), new HashMap<V, K>());
52 }
53
54 /**
55 * Constructs a <code>HashBidiMap</code> and copies the mappings from
56 * specified <code>Map</code>.
57 *
58 * @param map the map whose mappings are to be placed in this map
59 */
60 public DualHashBidiMap(final Map<K, V> map) {
61 super(new HashMap<K, V>(), new HashMap<V, K>());
62 putAll(map);
63 }
64
65 /**
66 * Constructs a <code>HashBidiMap</code> that decorates the specified maps.
67 *
68 * @param normalMap the normal direction map
69 * @param reverseMap the reverse direction map
70 * @param inverseBidiMap the inverse BidiMap
71 */
72 protected DualHashBidiMap(final Map<K, V> normalMap, final Map<V, K> reverseMap,
73 final BidiMap<V, K> inverseBidiMap) {
74 super(normalMap, reverseMap, inverseBidiMap);
75 }
76
77 /**
78 * Creates a new instance of this object.
79 *
80 * @param normalMap the normal direction map
81 * @param reverseMap the reverse direction map
82 * @param inverseBidiMap the inverse BidiMap
83 * @return new bidi map
84 */
85 @Override
86 protected BidiMap<V, K> createBidiMap(final Map<V, K> normalMap, final Map<K, V> reverseMap,
87 final BidiMap<K, V> inverseBidiMap) {
88 return new DualHashBidiMap<V, K>(normalMap, reverseMap, inverseBidiMap);
89 }
90
91 // Serialization
92 //-----------------------------------------------------------------------
93 private void writeObject(final ObjectOutputStream out) throws IOException {
94 out.defaultWriteObject();
95 out.writeObject(normalMap);
96 }
97
98 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
99 in.defaultReadObject();
100 normalMap = new HashMap<K, V>();
101 reverseMap = new HashMap<V, K>();
102 @SuppressWarnings("unchecked") // will fail at runtime if stream is incorrect
103 final Map<K, V> map = (Map<K, V>) in.readObject();
104 putAll(map);
105 }
106
107 }