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 * <p>See the Guava User Guide article on <a href=
036 * "http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap">
037 * {@code BiMap}</a>.
038 *
039 * @author Mike Bostock
040 * @since 2.0 (imported from Google Collections Library)
041 */
042 @GwtCompatible(emulated = true)
043 public final class HashBiMap<K, V> extends AbstractBiMap<K, V> {
044
045 /**
046 * Returns a new, empty {@code HashBiMap} with the default initial capacity
047 * (16).
048 */
049 public static <K, V> HashBiMap<K, V> create() {
050 return new HashBiMap<K, V>();
051 }
052
053 /**
054 * Constructs a new, empty bimap with the specified expected size.
055 *
056 * @param expectedSize the expected number of entries
057 * @throws IllegalArgumentException if the specified expected size is
058 * negative
059 */
060 public static <K, V> HashBiMap<K, V> create(int expectedSize) {
061 return new HashBiMap<K, V>(expectedSize);
062 }
063
064 /**
065 * Constructs a new bimap containing initial values from {@code map}. The
066 * bimap is created with an initial capacity sufficient to hold the mappings
067 * in the specified map.
068 */
069 public static <K, V> HashBiMap<K, V> create(
070 Map<? extends K, ? extends V> map) {
071 HashBiMap<K, V> bimap = create(map.size());
072 bimap.putAll(map);
073 return bimap;
074 }
075
076 private HashBiMap() {
077 super(new HashMap<K, V>(), new HashMap<V, K>());
078 }
079
080 private HashBiMap(int expectedSize) {
081 super(
082 Maps.<K, V>newHashMapWithExpectedSize(expectedSize),
083 Maps.<V, K>newHashMapWithExpectedSize(expectedSize));
084 }
085
086 // Override these two methods to show that keys and values may be null
087
088 @Override public V put(@Nullable K key, @Nullable V value) {
089 return super.put(key, value);
090 }
091
092 @Override public V forcePut(@Nullable K key, @Nullable V value) {
093 return super.forcePut(key, value);
094 }
095
096 /**
097 * @serialData the number of entries, first key, first value, second key,
098 * second value, and so on.
099 */
100 @GwtIncompatible("java.io.ObjectOutputStream")
101 private void writeObject(ObjectOutputStream stream) throws IOException {
102 stream.defaultWriteObject();
103 Serialization.writeMap(this, stream);
104 }
105
106 @GwtIncompatible("java.io.ObjectInputStream")
107 private void readObject(ObjectInputStream stream)
108 throws IOException, ClassNotFoundException {
109 stream.defaultReadObject();
110 int size = Serialization.readCount(stream);
111 setDelegates(Maps.<K, V>newHashMapWithExpectedSize(size),
112 Maps.<V, K>newHashMapWithExpectedSize(size));
113 Serialization.populateMap(this, stream, size);
114 }
115
116 @GwtIncompatible("Not needed in emulated source")
117 private static final long serialVersionUID = 0;
118 }