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.collections4.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.LinkedHashMap;
24 import java.util.Map;
25
26 import org.apache.commons.collections4.BidiMap;
27
28 /**
29 * Implements {@link BidiMap} with two {@link LinkedHashMap} instances.
30 * <p>
31 * Two {@link LinkedHashMap} instances are used in this class.
32 * This provides fast lookups at the expense of storing two sets of map entries and two linked lists.
33 * </p>
34 *
35 * @param <K> the type of the keys in the map
36 * @param <V> the type of the values in the map
37 *
38 * @since 4.0
39 */
40 public class DualLinkedHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements Serializable {
41
42 /** Ensure serialization compatibility */
43 private static final long serialVersionUID = 721969328361810L;
44
45 /**
46 * Creates an empty {@code HashBidiMap}.
47 */
48 public DualLinkedHashBidiMap() {
49 super(new LinkedHashMap<>(), new LinkedHashMap<>());
50 }
51
52 /**
53 * Constructs a {@code LinkedHashBidiMap} and copies the mappings from
54 * specified {@link Map}.
55 *
56 * @param map the map whose mappings are to be placed in this map
57 */
58 public DualLinkedHashBidiMap(final Map<? extends K, ? extends V> map) {
59 super(new LinkedHashMap<>(), new LinkedHashMap<>());
60 putAll(map);
61 }
62
63 /**
64 * Constructs a {@code LinkedHashBidiMap} that decorates the specified maps.
65 *
66 * @param normalMap the normal direction map
67 * @param reverseMap the reverse direction map
68 * @param inverseBidiMap the inverse BidiMap
69 */
70 protected DualLinkedHashBidiMap(final Map<K, V> normalMap, final Map<V, K> reverseMap,
71 final BidiMap<V, K> inverseBidiMap) {
72 super(normalMap, reverseMap, inverseBidiMap);
73 }
74
75 /**
76 * Creates a new instance of this object.
77 *
78 * @param normalMap the normal direction map
79 * @param reverseMap the reverse direction map
80 * @param inverseBidiMap the inverse BidiMap
81 * @return new bidi map
82 */
83 @Override
84 protected BidiMap<V, K> createBidiMap(final Map<V, K> normalMap, final Map<K, V> reverseMap,
85 final BidiMap<K, V> inverseBidiMap) {
86 return new DualLinkedHashBidiMap<>(normalMap, reverseMap, inverseBidiMap);
87 }
88
89 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
90 in.defaultReadObject();
91 normalMap = new LinkedHashMap<>();
92 reverseMap = new LinkedHashMap<>();
93 @SuppressWarnings("unchecked") // will fail at runtime if stream is incorrect
94 final Map<K, V> map = (Map<K, V>) in.readObject();
95 putAll(map);
96 }
97
98 // Serialization
99 private void writeObject(final ObjectOutputStream out) throws IOException {
100 out.defaultWriteObject();
101 out.writeObject(normalMap);
102 }
103 }