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