1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtIncompatible;
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.IOException;
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.ObjectInputStream;
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.ObjectOutputStream;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.HashMap;
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Map;
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * A {@link BiMap} backed by two {@link HashMap} instances. This implementation
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * allows null keys and values. A {@code HashBiMap} and its inverse are both
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * serializable.
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Mike Bostock
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true)
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class HashBiMap<K, V> extends AbstractBiMap<K, V> {
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a new, empty {@code HashBiMap} with the default initial capacity
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * (16).
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> HashBiMap<K, V> create() {
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new HashBiMap<K, V>();
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Constructs a new, empty bimap with the specified expected size.
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param expectedSize the expected number of entries
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if the specified expected size is
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     negative
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> HashBiMap<K, V> create(int expectedSize) {
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new HashBiMap<K, V>(expectedSize);
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Constructs a new bimap containing initial values from {@code map}. The
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * bimap is created with an initial capacity sufficient to hold the mappings
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * in the specified map.
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> HashBiMap<K, V> create(
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Map<? extends K, ? extends V> map) {
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    HashBiMap<K, V> bimap = create(map.size());
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    bimap.putAll(map);
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return bimap;
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private HashBiMap() {
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    super(new HashMap<K, V>(), new HashMap<V, K>());
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private HashBiMap(int expectedSize) {
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    super(
781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Maps.<K, V>newHashMapWithExpectedSize(expectedSize),
791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Maps.<V, K>newHashMapWithExpectedSize(expectedSize));
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Override these two methods to show that keys and values may be null
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public V put(@Nullable K key, @Nullable V value) {
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return super.put(key, value);
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public V forcePut(@Nullable K key, @Nullable V value) {
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return super.forcePut(key, value);
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @serialData the number of entries, first key, first value, second key,
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     second value, and so on.
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.io.ObjectOutputStream")
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private void writeObject(ObjectOutputStream stream) throws IOException {
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    stream.defaultWriteObject();
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Serialization.writeMap(this, stream);
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.io.ObjectInputStream")
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private void readObject(ObjectInputStream stream)
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      throws IOException, ClassNotFoundException {
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    stream.defaultReadObject();
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int size = Serialization.readCount(stream);
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    setDelegates(Maps.<K, V>newHashMapWithExpectedSize(size),
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        Maps.<V, K>newHashMapWithExpectedSize(size));
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Serialization.populateMap(this, stream, size);
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("Not needed in emulated source")
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final long serialVersionUID = 0;
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
115