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 * Implementation of <code>BidiMap</code> that uses two <code>LinkedHashMap</code> instances. 030 * <p> 031 * Two <code>LinkedHashMap</code> 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 * 034 * @param <K> the type of the keys in the map 035 * @param <V> the type of the values in the map 036 * 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</code>. 046 */ 047 public DualLinkedHashBidiMap() { 048 super(new LinkedHashMap<K, V>(), new LinkedHashMap<V, K>()); 049 } 050 051 /** 052 * Constructs a <code>LinkedHashBidiMap</code> and copies the mappings from 053 * specified <code>Map</code>. 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<K, V>(), new LinkedHashMap<V, K>()); 059 putAll(map); 060 } 061 062 /** 063 * Constructs a <code>LinkedHashBidiMap</code> 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 // Serialization 089 //----------------------------------------------------------------------- 090 private void writeObject(final ObjectOutputStream out) throws IOException { 091 out.defaultWriteObject(); 092 out.writeObject(normalMap); 093 } 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}