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 static com.google.common.base.Preconditions.checkArgument;
20
21import com.google.common.annotations.GwtCompatible;
22
23import java.util.EnumMap;
24import java.util.Map;
25
26/**
27 * A {@code BiMap} backed by two {@code EnumMap} instances. Null keys and values
28 * are not permitted. An {@code EnumBiMap} and its inverse are both
29 * serializable.
30 *
31 * @author Mike Bostock
32 * @since 2.0 (imported from Google Collections Library)
33 */
34@GwtCompatible(emulated = true)
35public final class EnumBiMap<K extends Enum<K>, V extends Enum<V>>
36    extends AbstractBiMap<K, V> {
37  private transient Class<K> keyType;
38  private transient Class<V> valueType;
39
40  /**
41   * Returns a new, empty {@code EnumBiMap} using the specified key and value
42   * types.
43   *
44   * @param keyType the key type
45   * @param valueType the value type
46   */
47  public static <K extends Enum<K>, V extends Enum<V>> EnumBiMap<K, V>
48      create(Class<K> keyType, Class<V> valueType) {
49    return new EnumBiMap<K, V>(keyType, valueType);
50  }
51
52  /**
53   * Returns a new bimap with the same mappings as the specified map. If the
54   * specified map is an {@code EnumBiMap}, the new bimap has the same types as
55   * the provided map. Otherwise, the specified map must contain at least one
56   * mapping, in order to determine the key and value types.
57   *
58   * @param map the map whose mappings are to be placed in this map
59   * @throws IllegalArgumentException if map is not an {@code EnumBiMap}
60   *     instance and contains no mappings
61   */
62  public static <K extends Enum<K>, V extends Enum<V>> EnumBiMap<K, V>
63      create(Map<K, V> map) {
64    EnumBiMap<K, V> bimap = create(inferKeyType(map), inferValueType(map));
65    bimap.putAll(map);
66    return bimap;
67  }
68
69  private EnumBiMap(Class<K> keyType, Class<V> valueType) {
70    super(WellBehavedMap.wrap(new EnumMap<K, V>(keyType)),
71        WellBehavedMap.wrap(new EnumMap<V, K>(valueType)));
72    this.keyType = keyType;
73    this.valueType = valueType;
74  }
75
76  static <K extends Enum<K>> Class<K> inferKeyType(Map<K, ?> map) {
77    if (map instanceof EnumBiMap) {
78      return ((EnumBiMap<K, ?>) map).keyType();
79    }
80    if (map instanceof EnumHashBiMap) {
81      return ((EnumHashBiMap<K, ?>) map).keyType();
82    }
83    checkArgument(!map.isEmpty());
84    return map.keySet().iterator().next().getDeclaringClass();
85  }
86
87  private static <V extends Enum<V>> Class<V> inferValueType(Map<?, V> map) {
88    if (map instanceof EnumBiMap) {
89      return ((EnumBiMap<?, V>) map).valueType;
90    }
91    checkArgument(!map.isEmpty());
92    return map.values().iterator().next().getDeclaringClass();
93  }
94
95  /** Returns the associated key type. */
96  public Class<K> keyType() {
97    return keyType;
98  }
99
100  /** Returns the associated value type. */
101  public Class<V> valueType() {
102    return valueType;
103  }
104}
105
106