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.0 (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( 078 Maps.<K, V>newHashMapWithExpectedSize(expectedSize), 079 Maps.<V, K>newHashMapWithExpectedSize(expectedSize)); 080 } 081 082 // Override these two methods to show that keys and values may be null 083 084 @Override public V put(@Nullable K key, @Nullable V value) { 085 return super.put(key, value); 086 } 087 088 @Override public V forcePut(@Nullable K key, @Nullable V value) { 089 return super.forcePut(key, value); 090 } 091 092 /** 093 * @serialData the number of entries, first key, first value, second key, 094 * second value, and so on. 095 */ 096 @GwtIncompatible("java.io.ObjectOutputStream") 097 private void writeObject(ObjectOutputStream stream) throws IOException { 098 stream.defaultWriteObject(); 099 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 }