Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2007 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.collect;
     18 
     19 import com.google.common.annotations.GwtCompatible;
     20 import com.google.common.annotations.GwtIncompatible;
     21 
     22 import java.io.IOException;
     23 import java.io.ObjectInputStream;
     24 import java.io.ObjectOutputStream;
     25 import java.util.HashMap;
     26 import java.util.Map;
     27 
     28 import javax.annotation.Nullable;
     29 
     30 /**
     31  * A {@link BiMap} backed by two {@link HashMap} instances. This implementation
     32  * allows null keys and values. A {@code HashBiMap} and its inverse are both
     33  * serializable.
     34  *
     35  * @author Mike Bostock
     36  * @since 2.0 (imported from Google Collections Library)
     37  */
     38 @GwtCompatible(emulated = true)
     39 public final class HashBiMap<K, V> extends AbstractBiMap<K, V> {
     40 
     41   /**
     42    * Returns a new, empty {@code HashBiMap} with the default initial capacity
     43    * (16).
     44    */
     45   public static <K, V> HashBiMap<K, V> create() {
     46     return new HashBiMap<K, V>();
     47   }
     48 
     49   /**
     50    * Constructs a new, empty bimap with the specified expected size.
     51    *
     52    * @param expectedSize the expected number of entries
     53    * @throws IllegalArgumentException if the specified expected size is
     54    *     negative
     55    */
     56   public static <K, V> HashBiMap<K, V> create(int expectedSize) {
     57     return new HashBiMap<K, V>(expectedSize);
     58   }
     59 
     60   /**
     61    * Constructs a new bimap containing initial values from {@code map}. The
     62    * bimap is created with an initial capacity sufficient to hold the mappings
     63    * in the specified map.
     64    */
     65   public static <K, V> HashBiMap<K, V> create(
     66       Map<? extends K, ? extends V> map) {
     67     HashBiMap<K, V> bimap = create(map.size());
     68     bimap.putAll(map);
     69     return bimap;
     70   }
     71 
     72   private HashBiMap() {
     73     super(new HashMap<K, V>(), new HashMap<V, K>());
     74   }
     75 
     76   private HashBiMap(int expectedSize) {
     77     super(
     78         Maps.<K, V>newHashMapWithExpectedSize(expectedSize),
     79         Maps.<V, K>newHashMapWithExpectedSize(expectedSize));
     80   }
     81 
     82   // Override these two methods to show that keys and values may be null
     83 
     84   @Override public V put(@Nullable K key, @Nullable V value) {
     85     return super.put(key, value);
     86   }
     87 
     88   @Override public V forcePut(@Nullable K key, @Nullable V value) {
     89     return super.forcePut(key, value);
     90   }
     91 
     92   /**
     93    * @serialData the number of entries, first key, first value, second key,
     94    *     second value, and so on.
     95    */
     96   @GwtIncompatible("java.io.ObjectOutputStream")
     97   private void writeObject(ObjectOutputStream stream) throws IOException {
     98     stream.defaultWriteObject();
     99     Serialization.writeMap(this, stream);
    100   }
    101 
    102   @GwtIncompatible("java.io.ObjectInputStream")
    103   private void readObject(ObjectInputStream stream)
    104       throws IOException, ClassNotFoundException {
    105     stream.defaultReadObject();
    106     int size = Serialization.readCount(stream);
    107     setDelegates(Maps.<K, V>newHashMapWithExpectedSize(size),
    108         Maps.<V, K>newHashMapWithExpectedSize(size));
    109     Serialization.populateMap(this, stream, size);
    110   }
    111 
    112   @GwtIncompatible("Not needed in emulated source")
    113   private static final long serialVersionUID = 0;
    114 }
    115