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
17package com.google.common.collect;
18
19import com.google.common.annotations.GwtCompatible;
20
21import java.util.EnumMap;
22import java.util.Map;
23
24import javax.annotation.Nullable;
25
26/**
27 * A {@code BiMap} backed by an {@code EnumMap} instance for keys-to-values, and
28 * a {@code HashMap} instance for values-to-keys. Null keys are not permitted,
29 * but null values are. An {@code EnumHashBiMap} and its inverse are both
30 * serializable.
31 *
32 * @author Mike Bostock
33 * @since 2.0 (imported from Google Collections Library)
34 */
35@GwtCompatible(emulated = true)
36public final class EnumHashBiMap<K extends Enum<K>, V>
37    extends AbstractBiMap<K, V> {
38  private transient Class<K> keyType;
39
40  /**
41   * Returns a new, empty {@code EnumHashBiMap} using the specified key type.
42   *
43   * @param keyType the key type
44   */
45  public static <K extends Enum<K>, V> EnumHashBiMap<K, V>
46      create(Class<K> keyType) {
47    return new EnumHashBiMap<K, V>(keyType);
48  }
49
50  /**
51   * Constructs a new bimap with the same mappings as the specified map. If the
52   * specified map is an {@code EnumHashBiMap} or an {@link EnumBiMap}, the new
53   * bimap has the same key type as the input bimap. Otherwise, the specified
54   * map must contain at least one mapping, in order to determine the key type.
55   *
56   * @param map the map whose mappings are to be placed in this map
57   * @throws IllegalArgumentException if map is not an {@code EnumBiMap} or an
58   *     {@code EnumHashBiMap} instance and contains no mappings
59   */
60  public static <K extends Enum<K>, V> EnumHashBiMap<K, V>
61      create(Map<K, ? extends V> map) {
62    EnumHashBiMap<K, V> bimap = create(EnumBiMap.inferKeyType(map));
63    bimap.putAll(map);
64    return bimap;
65  }
66
67  private EnumHashBiMap(Class<K> keyType) {
68    super(WellBehavedMap.wrap(
69        new EnumMap<K, V>(keyType)),
70        Maps.<V, K>newHashMapWithExpectedSize(
71            keyType.getEnumConstants().length));
72    this.keyType = keyType;
73  }
74
75  // Overriding these two methods to show that values may be null (but not keys)
76
77  @Override public V put(K key, @Nullable V value) {
78    return super.put(key, value);
79  }
80
81  @Override public V forcePut(K key, @Nullable V value) {
82    return super.forcePut(key, value);
83  }
84
85  /** Returns the associated key type. */
86  public Class<K> keyType() {
87    return keyType;
88  }
89}
90
91