Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2007 Google Inc.
      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 
     21 import java.io.IOException;
     22 import java.io.ObjectInputStream;
     23 import java.io.ObjectOutputStream;
     24 import java.util.HashMap;
     25 import java.util.Map;
     26 
     27 import javax.annotation.Nullable;
     28 
     29 /**
     30  * A {@link BiMap} backed by two {@link HashMap} instances. This implementation
     31  * allows null keys and values. A {@code HashBiMap} and its inverse are both
     32  * serializable.
     33  *
     34  * @author Mike Bostock
     35  * @since 2010.01.04 <b>stable</b> (imported from Google Collections Library)
     36  */
     37 @GwtCompatible
     38 public final class HashBiMap<K, V> extends AbstractBiMap<K, V> {
     39 
     40   /**
     41    * Returns a new, empty {@code HashBiMap} with the default initial capacity
     42    * (16).
     43    */
     44   public static <K, V> HashBiMap<K, V> create() {
     45     return new HashBiMap<K, V>();
     46   }
     47 
     48   /**
     49    * Constructs a new, empty bimap with the specified expected size.
     50    *
     51    * @param expectedSize the expected number of entries
     52    * @throws IllegalArgumentException if the specified expected size is
     53    *     negative
     54    */
     55   public static <K, V> HashBiMap<K, V> create(int expectedSize) {
     56     return new HashBiMap<K, V>(expectedSize);
     57   }
     58 
     59   /**
     60    * Constructs a new bimap containing initial values from {@code map}. The
     61    * bimap is created with an initial capacity sufficient to hold the mappings
     62    * in the specified map.
     63    */
     64   public static <K, V> HashBiMap<K, V> create(
     65       Map<? extends K, ? extends V> map) {
     66     HashBiMap<K, V> bimap = create(map.size());
     67     bimap.putAll(map);
     68     return bimap;
     69   }
     70 
     71   private HashBiMap() {
     72     super(new HashMap<K, V>(), new HashMap<V, K>());
     73   }
     74 
     75   private HashBiMap(int expectedSize) {
     76     super(new HashMap<K, V>(Maps.capacity(expectedSize)),
     77         new HashMap<V, K>(Maps.capacity(expectedSize)));
     78   }
     79 
     80   // Override these two methods to show that keys and values may be null
     81 
     82   @Override public V put(@Nullable K key, @Nullable V value) {
     83     return super.put(key, value);
     84   }
     85 
     86   @Override public V forcePut(@Nullable K key, @Nullable V value) {
     87     return super.forcePut(key, value);
     88   }
     89 
     90   /**
     91    * @serialData the number of entries, first key, first value, second key,
     92    *     second value, and so on.
     93    */
     94   private void writeObject(ObjectOutputStream stream) throws IOException {
     95     stream.defaultWriteObject();
     96     Serialization.writeMap(this, stream);
     97   }
     98 
     99   private void readObject(ObjectInputStream stream)
    100       throws IOException, ClassNotFoundException {
    101     stream.defaultReadObject();
    102     int size = Serialization.readCount(stream);
    103     setDelegates(Maps.<K, V>newHashMapWithExpectedSize(size),
    104         Maps.<V, K>newHashMapWithExpectedSize(size));
    105     Serialization.populateMap(this, stream, size);
    106   }
    107 
    108   private static final long serialVersionUID = 0;
    109 }
    110