    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 * Implements {@link BidiMap} with 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 * <p>
    037 * NOTE: From Commons Collections 3.1, all subclasses will use {@link HashMap}
    038 * and the flawed {@code createMap} method is ignored.
    039 * </p>
    040 *
    041 * @param <K> the type of the keys in the map
    042 * @param <V> the type of the values in the map
    043 * @since 3.0
    044 */
    045public class DualHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements Serializable {
    046
    047    /** Ensure serialization compatibility */
    048    private static final long serialVersionUID = 721969328361808L;
    049
    050    /**
    051     * Creates an empty {@code HashBidiMap}.
    052     */
    053    public DualHashBidiMap() {
    054        super(new HashMap<>(), new HashMap<>());
    055    }
    056
    057    /**
    058     * Constructs a {@code HashBidiMap} and copies the mappings from
    059     * specified {@code Map}.
    060     *
    061     * @param map  the map whose mappings are to be placed in this map
    062     */
    063    public DualHashBidiMap(final Map<? extends K, ? extends V> map) {
    064        super(new HashMap<>(), new HashMap<>());
    065        putAll(map);
    066    }
    067
    068    /**
    069     * Constructs a {@code HashBidiMap} that decorates the specified maps.
    070     *
    071     * @param normalMap  the normal direction map
    072     * @param reverseMap  the reverse direction map
    073     * @param inverseBidiMap  the inverse BidiMap
    074     */
    075    protected DualHashBidiMap(final Map<K, V> normalMap, final Map<V, K> reverseMap,
    076                              final BidiMap<V, K> inverseBidiMap) {
    077        super(normalMap, reverseMap, inverseBidiMap);
    078    }
    079
    080    /**
    081     * Creates a new instance of this object.
    082     *
    083     * @param normalMap  the normal direction map
    084     * @param reverseMap  the reverse direction map
    085     * @param inverseBidiMap  the inverse BidiMap
    086     * @return new bidi map
    087     */
    088    @Override
    089    protected BidiMap<V, K> createBidiMap(final Map<V, K> normalMap, final Map<K, V> reverseMap,
    090                                          final BidiMap<K, V> inverseBidiMap) {
    091        return new DualHashBidiMap<>(normalMap, reverseMap, inverseBidiMap);
    092    }
    093
    094    /**
    095     * Deserializes an instance from an ObjectInputStream.
    096     *
    097     * @param in The source ObjectInputStream.
    098     * @throws IOException            Any of the usual Input/Output related exceptions.
    099     * @throws ClassNotFoundException A class of a serialized object cannot be found.
    100     */
    101    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    102        in.defaultReadObject();
    103        normalMap = new HashMap<>();
    104        reverseMap = new HashMap<>();
    105        @SuppressWarnings("unchecked") // will fail at runtime if stream is incorrect
    106        final Map<K, V> map = (Map<K, V>) in.readObject();
    107        putAll(map);
    108    }
    109
    110    /**
    111     * Serializes this object to an ObjectOutputStream.
    112     *
    113     * @param out the target ObjectOutputStream.
    114     * @throws IOException thrown when an I/O errors occur writing to the target stream.
    115     */
    116    private void writeObject(final ObjectOutputStream out) throws IOException {
    117        out.defaultWriteObject();
    118        out.writeObject(normalMap);
    119    }
    120
    121}