001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.bidimap;
018
019import java.io.IOException;
020import java.io.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.apache.commons.collections4.BidiMap;
027
028/**
029 * Implementation of {@link BidiMap} that uses two {@link HashMap} instances.
030 * <p>
031 * Two {@link HashMap} instances are used in this class.
032 * This provides fast lookups at the expense of storing two sets of map entries.
033 * Commons Collections would welcome the addition of a direct hash-based
034 * implementation of the {@link BidiMap} interface.
035 * <p>
036 * NOTE: From Commons Collections 3.1, all subclasses will use {@link HashMap}
037 * and the flawed <code>createMap</code> method is ignored.
038 *
039 * @since 3.0
040 * @version $Id: DualHashBidiMap.html 887892 2013-11-24 13:43:45Z tn $
041 */
042public class DualHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements Serializable {
043
044    /** Ensure serialization compatibility */
045    private static final long serialVersionUID = 721969328361808L;
046
047    /**
048     * Creates an empty <code>HashBidiMap</code>.
049     */
050    public DualHashBidiMap() {
051        super(new HashMap<K, V>(), new HashMap<V, K>());
052    }
053
054    /**
055     * Constructs a <code>HashBidiMap</code> and copies the mappings from
056     * specified <code>Map</code>.
057     *
058     * @param map  the map whose mappings are to be placed in this map
059     */
060    public DualHashBidiMap(final Map<? extends K, ? extends V> map) {
061        super(new HashMap<K, V>(), new HashMap<V, K>());
062        putAll(map);
063    }
064
065    /**
066     * Constructs a <code>HashBidiMap</code> that decorates the specified maps.
067     *
068     * @param normalMap  the normal direction map
069     * @param reverseMap  the reverse direction map
070     * @param inverseBidiMap  the inverse BidiMap
071     */
072    protected DualHashBidiMap(final Map<K, V> normalMap, final Map<V, K> reverseMap,
073                              final BidiMap<V, K> inverseBidiMap) {
074        super(normalMap, reverseMap, inverseBidiMap);
075    }
076
077    /**
078     * Creates a new instance of this object.
079     *
080     * @param normalMap  the normal direction map
081     * @param reverseMap  the reverse direction map
082     * @param inverseBidiMap  the inverse BidiMap
083     * @return new bidi map
084     */
085    @Override
086    protected BidiMap<V, K> createBidiMap(final Map<V, K> normalMap, final Map<K, V> reverseMap,
087                                          final BidiMap<K, V> inverseBidiMap) {
088        return new DualHashBidiMap<V, K>(normalMap, reverseMap, inverseBidiMap);
089    }
090
091    // Serialization
092    //-----------------------------------------------------------------------
093    private void writeObject(final ObjectOutputStream out) throws IOException {
094        out.defaultWriteObject();
095        out.writeObject(normalMap);
096    }
097
098    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
099        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}