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 */
017 package org.apache.commons.collections.bidimap;
018
019 import java.io.IOException;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022 import java.io.Serializable;
023 import java.util.HashMap;
024 import java.util.Map;
025
026 import org.apache.commons.collections.BidiMap;
027
028 /**
029 * Implementation of <code>BidiMap</code> that uses two <code>HashMap</code> instances.
030 * <p>
031 * Two <code>HashMap</code> 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 <code>BidiMap</code> interface.
035 * <p>
036 * NOTE: From Commons Collections 3.1, all subclasses will use <code>HashMap</code>
037 * and the flawed <code>createMap</code> method is ignored.
038 *
039 * @since Commons Collections 3.0
040 * @version $Id: DualHashBidiMap.java 646777 2008-04-10 12:33:15Z niallp $
041 *
042 * @author Matthew Hawthorne
043 * @author Stephen Colebourne
044 */
045 public class DualHashBidiMap
046 extends AbstractDualBidiMap implements Serializable {
047
048 /** Ensure serialization compatibility */
049 private static final long serialVersionUID = 721969328361808L;
050
051 /**
052 * Creates an empty <code>HashBidiMap</code>.
053 */
054 public DualHashBidiMap() {
055 super(new HashMap(), new HashMap());
056 }
057
058 /**
059 * Constructs a <code>HashBidiMap</code> and copies the mappings from
060 * specified <code>Map</code>.
061 *
062 * @param map the map whose mappings are to be placed in this map
063 */
064 public DualHashBidiMap(Map map) {
065 super(new HashMap(), new HashMap());
066 putAll(map);
067 }
068
069 /**
070 * Constructs a <code>HashBidiMap</code> that decorates the specified maps.
071 *
072 * @param normalMap the normal direction map
073 * @param reverseMap the reverse direction map
074 * @param inverseBidiMap the inverse BidiMap
075 */
076 protected DualHashBidiMap(Map normalMap, Map reverseMap, BidiMap 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 protected BidiMap createBidiMap(Map normalMap, Map reverseMap, BidiMap inverseBidiMap) {
089 return new DualHashBidiMap(normalMap, reverseMap, inverseBidiMap);
090 }
091
092 // Serialization
093 //-----------------------------------------------------------------------
094 private void writeObject(ObjectOutputStream out) throws IOException {
095 out.defaultWriteObject();
096 out.writeObject(maps[0]);
097 }
098
099 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
100 in.defaultReadObject();
101 maps[0] = new HashMap();
102 maps[1] = new HashMap();
103 Map map = (Map) in.readObject();
104 putAll(map);
105 }
106
107 }