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
017package com.google.common.collect;
018
019import static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.annotations.GwtIncompatible;
024import java.io.IOException;
025import java.io.ObjectInputStream;
026import java.io.ObjectOutputStream;
027import java.util.EnumMap;
028import java.util.Map;
029
030/**
031 * A {@code BiMap} backed by two {@code EnumMap} instances. Null keys and values are not permitted.
032 * An {@code EnumBiMap} and its inverse are both serializable.
033 *
034 * <p>See the Guava User Guide article on <a href=
035 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#bimap"> {@code BiMap}</a>.
036 *
037 * @author Mike Bostock
038 * @since 2.0
039 */
040@GwtCompatible(emulated = true)
041public final class EnumBiMap<K extends Enum<K>, V extends Enum<V>> extends AbstractBiMap<K, V> {
042  private transient Class<K> keyType;
043  private transient Class<V> valueType;
044
045  /**
046   * Returns a new, empty {@code EnumBiMap} using the specified key and value types.
047   *
048   * @param keyType the key type
049   * @param valueType the value type
050   */
051  public static <K extends Enum<K>, V extends Enum<V>> EnumBiMap<K, V> create(
052      Class<K> keyType, Class<V> valueType) {
053    return new EnumBiMap<>(keyType, valueType);
054  }
055
056  /**
057   * Returns a new bimap with the same mappings as the specified map. If the specified map is an
058   * {@code EnumBiMap}, the new bimap has the same types as the provided map. Otherwise, the
059   * specified map must contain at least one mapping, in order to determine the key and value types.
060   *
061   * @param map the map whose mappings are to be placed in this map
062   * @throws IllegalArgumentException if map is not an {@code EnumBiMap} instance and contains no
063   *     mappings
064   */
065  public static <K extends Enum<K>, V extends Enum<V>> EnumBiMap<K, V> create(Map<K, V> map) {
066    EnumBiMap<K, V> bimap = create(inferKeyType(map), inferValueType(map));
067    bimap.putAll(map);
068    return bimap;
069  }
070
071  private EnumBiMap(Class<K> keyType, Class<V> valueType) {
072    super(new EnumMap<K, V>(keyType), new EnumMap<V, K>(valueType));
073    this.keyType = keyType;
074    this.valueType = valueType;
075  }
076
077  static <K extends Enum<K>> Class<K> inferKeyType(Map<K, ?> map) {
078    if (map instanceof EnumBiMap) {
079      return ((EnumBiMap<K, ?>) map).keyType();
080    }
081    if (map instanceof EnumHashBiMap) {
082      return ((EnumHashBiMap<K, ?>) map).keyType();
083    }
084    checkArgument(!map.isEmpty());
085    return map.keySet().iterator().next().getDeclaringClass();
086  }
087
088  private static <V extends Enum<V>> Class<V> inferValueType(Map<?, V> map) {
089    if (map instanceof EnumBiMap) {
090      return ((EnumBiMap<?, V>) map).valueType;
091    }
092    checkArgument(!map.isEmpty());
093    return map.values().iterator().next().getDeclaringClass();
094  }
095
096  /** Returns the associated key type. */
097  public Class<K> keyType() {
098    return keyType;
099  }
100
101  /** Returns the associated value type. */
102  public Class<V> valueType() {
103    return valueType;
104  }
105
106  @Override
107  K checkKey(K key) {
108    return checkNotNull(key);
109  }
110
111  @Override
112  V checkValue(V value) {
113    return checkNotNull(value);
114  }
115
116  /**
117   * @serialData the key class, value class, number of entries, first key, first value, second key,
118   *     second value, and so on.
119   */
120  @GwtIncompatible // java.io.ObjectOutputStream
121  private void writeObject(ObjectOutputStream stream) throws IOException {
122    stream.defaultWriteObject();
123    stream.writeObject(keyType);
124    stream.writeObject(valueType);
125    Serialization.writeMap(this, stream);
126  }
127
128  @SuppressWarnings("unchecked") // reading fields populated by writeObject
129  @GwtIncompatible // java.io.ObjectInputStream
130  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
131    stream.defaultReadObject();
132    keyType = (Class<K>) stream.readObject();
133    valueType = (Class<V>) stream.readObject();
134    setDelegates(new EnumMap<K, V>(keyType), new EnumMap<V, K>(valueType));
135    Serialization.populateMap(this, stream);
136  }
137
138  @GwtIncompatible // not needed in emulated source.
139  private static final long serialVersionUID = 0;
140}