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 * @param <K> the type of the keys in the map
040 * @param <V> the type of the values in the map
041 *
042 * @since 3.0
043 */
044public class DualHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements Serializable {
045
046    /** Ensure serialization compatibility */
047    private static final long serialVersionUID = 721969328361808L;
048
049    /**
050     * Creates an empty <code>HashBidiMap</code>.
051     */
052    public DualHashBidiMap() {
053        super(new HashMap<K, V>(), new HashMap<V, K>());
054    }
055
056    /**
057     * Constructs a <code>HashBidiMap</code> and copies the mappings from
058     * specified <code>Map</code>.
059     *
060     * @param map  the map whose mappings are to be placed in this map
061     */
062    public DualHashBidiMap(final Map<? extends K, ? extends V> map) {
063        super(new HashMap<K, V>(), new HashMap<V, K>());
064        putAll(map);
065    }
066
067    /**
068     * Constructs a <code>HashBidiMap</code> that decorates the specified maps.
069     *
070     * @param normalMap  the normal direction map
071     * @param reverseMap  the reverse direction map
072     * @param inverseBidiMap  the inverse BidiMap
073     */
074    protected DualHashBidiMap(final Map<K, V> normalMap, final Map<V, K> reverseMap,
075                              final BidiMap<V, K> inverseBidiMap) {
076        super(normalMap, reverseMap, inverseBidiMap);
077    }
078
079    /**
080     * Creates a new instance of this object.
081     *
082     * @param normalMap  the normal direction map
083     * @param reverseMap  the reverse direction map
084     * @param inverseBidiMap  the inverse BidiMap
085     * @return new bidi map
086     */
087    @Override
088    protected BidiMap<V, K> createBidiMap(final Map<V, K> normalMap, final Map<K, V> reverseMap,
089                                          final BidiMap<K, V> inverseBidiMap) {
090        return new DualHashBidiMap<>(normalMap, reverseMap, inverseBidiMap);
091    }
092
093    // Serialization
094    //-----------------------------------------------------------------------
095    private void writeObject(final ObjectOutputStream out) throws IOException {
096        out.defaultWriteObject();
097        out.writeObject(normalMap);
098    }
099
100    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
101        in.defaultReadObject();
102        normalMap = new HashMap<>();
103        reverseMap = new HashMap<>();
104        @SuppressWarnings("unchecked") // will fail at runtime if stream is incorrect
105        final Map<K, V> map = (Map<K, V>) in.readObject();
106        putAll(map);
107    }
108
109}