001    /*
002     * Copyright (C) 2007 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.collect;
018    
019    import com.google.common.annotations.GwtCompatible;
020    import com.google.common.annotations.GwtIncompatible;
021    
022    import java.io.IOException;
023    import java.io.ObjectInputStream;
024    import java.io.ObjectOutputStream;
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import javax.annotation.Nullable;
029    
030    /**
031     * A {@link BiMap} backed by two {@link HashMap} instances. This implementation
032     * allows null keys and values. A {@code HashBiMap} and its inverse are both
033     * serializable.
034     *
035     * @author Mike Bostock
036     * @since 2 (imported from Google Collections Library)
037     */
038    @GwtCompatible(emulated = true)
039    public final class HashBiMap<K, V> extends AbstractBiMap<K, V> {
040    
041      /**
042       * Returns a new, empty {@code HashBiMap} with the default initial capacity
043       * (16).
044       */
045      public static <K, V> HashBiMap<K, V> create() {
046        return new HashBiMap<K, V>();
047      }
048    
049      /**
050       * Constructs a new, empty bimap with the specified expected size.
051       *
052       * @param expectedSize the expected number of entries
053       * @throws IllegalArgumentException if the specified expected size is
054       *     negative
055       */
056      public static <K, V> HashBiMap<K, V> create(int expectedSize) {
057        return new HashBiMap<K, V>(expectedSize);
058      }
059    
060      /**
061       * Constructs a new bimap containing initial values from {@code map}. The
062       * bimap is created with an initial capacity sufficient to hold the mappings
063       * in the specified map.
064       */
065      public static <K, V> HashBiMap<K, V> create(
066          Map<? extends K, ? extends V> map) {
067        HashBiMap<K, V> bimap = create(map.size());
068        bimap.putAll(map);
069        return bimap;
070      }
071    
072      private HashBiMap() {
073        super(new HashMap<K, V>(), new HashMap<V, K>());
074      }
075    
076      private HashBiMap(int expectedSize) {
077        super(new HashMap<K, V>(Maps.capacity(expectedSize)),
078            new HashMap<V, K>(Maps.capacity(expectedSize)));
079      }
080    
081      // Override these two methods to show that keys and values may be null
082    
083      @Override public V put(@Nullable K key, @Nullable V value) {
084        return super.put(key, value);
085      }
086    
087      @Override public V forcePut(@Nullable K key, @Nullable V value) {
088        return super.forcePut(key, value);
089      }
090    
091      /**
092       * @serialData the number of entries, first key, first value, second key,
093       *     second value, and so on.
094       */
095      @GwtIncompatible("java.io.ObjectOutputStream")
096      private void writeObject(ObjectOutputStream stream) throws IOException {
097        stream.defaultWriteObject();
098        Serialization.writeMap(this, stream);
099      }
100    
101      @GwtIncompatible("java.io.ObjectInputStream")
102      private void readObject(ObjectInputStream stream)
103          throws IOException, ClassNotFoundException {
104        stream.defaultReadObject();
105        int size = Serialization.readCount(stream);
106        setDelegates(Maps.<K, V>newHashMapWithExpectedSize(size),
107            Maps.<V, K>newHashMapWithExpectedSize(size));
108        Serialization.populateMap(this, stream, size);
109      }
110    
111      @GwtIncompatible("Not needed in emulated source")
112      private static final long serialVersionUID = 0;
113    }