    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.LinkedHashMap;
    024import java.util.Map;
    025
    026import org.apache.commons.collections4.BidiMap;
    027
    028/**
    029 * Implements {@link BidiMap} with two {@link LinkedHashMap} instances.
    030 * <p>
    031 * Two {@link LinkedHashMap} instances are used in this class.
    032 * This provides fast lookups at the expense of storing two sets of map entries and two linked lists.
    033 * </p>
    034 *
    035 * @param <K> the type of the keys in the map
    036 * @param <V> the type of the values in the map
    037 * @since 4.0
    038 */
    039public class DualLinkedHashBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements Serializable {
    040
    041    /** Ensure serialization compatibility */
    042    private static final long serialVersionUID = 721969328361810L;
    043
    044    /**
    045     * Creates an empty {@code HashBidiMap}.
    046     */
    047    public DualLinkedHashBidiMap() {
    048        super(new LinkedHashMap<>(), new LinkedHashMap<>());
    049    }
    050
    051    /**
    052     * Constructs a {@code LinkedHashBidiMap} and copies the mappings from
    053     * specified {@link Map}.
    054     *
    055     * @param map the map whose mappings are to be placed in this map
    056     */
    057    public DualLinkedHashBidiMap(final Map<? extends K, ? extends V> map) {
    058        super(new LinkedHashMap<>(), new LinkedHashMap<>());
    059        putAll(map);
    060    }
    061
    062    /**
    063     * Constructs a {@code LinkedHashBidiMap} that decorates the specified maps.
    064     *
    065     * @param normalMap      the normal direction map
    066     * @param reverseMap     the reverse direction map
    067     * @param inverseBidiMap the inverse BidiMap
    068     */
    069    protected DualLinkedHashBidiMap(final Map<K, V> normalMap, final Map<V, K> reverseMap,
    070                                    final BidiMap<V, K> inverseBidiMap) {
    071        super(normalMap, reverseMap, inverseBidiMap);
    072    }
    073
    074    /**
    075     * Creates a new instance of this object.
    076     *
    077     * @param normalMap      the normal direction map
    078     * @param reverseMap     the reverse direction map
    079     * @param inverseBidiMap the inverse BidiMap
    080     * @return new bidi map
    081     */
    082    @Override
    083    protected BidiMap<V, K> createBidiMap(final Map<V, K> normalMap, final Map<K, V> reverseMap,
    084            final BidiMap<K, V> inverseBidiMap) {
    085        return new DualLinkedHashBidiMap<>(normalMap, reverseMap, inverseBidiMap);
    086    }
    087
    088    /**
    089     * Deserializes an instance from an ObjectInputStream.
    090     *
    091     * @param in The source ObjectInputStream.
    092     * @throws IOException            Any of the usual Input/Output related exceptions.
    093     * @throws ClassNotFoundException A class of a serialized object cannot be found.
    094     */
    095    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    096        in.defaultReadObject();
    097        normalMap = new LinkedHashMap<>();
    098        reverseMap = new LinkedHashMap<>();
    099        @SuppressWarnings("unchecked") // will fail at runtime if stream is incorrect
    100        final Map<K, V> map = (Map<K, V>) in.readObject();
    101        putAll(map);
    102    }
    103
    104    /**
    105     * Serializes this object to an ObjectOutputStream.
    106     *
    107     * @param out the target ObjectOutputStream.
    108     * @throws IOException thrown when an I/O errors occur writing to the target stream.
    109     */
    110    private void writeObject(final ObjectOutputStream out) throws IOException {
    111        out.defaultWriteObject();
    112        out.writeObject(normalMap);
    113    }
    114}