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)
041@ElementTypesAreNonnullByDefault
042public final class EnumBiMap<K extends Enum<K>, V extends Enum<V>> extends AbstractBiMap<K, V> {
043  private transient Class<K> keyType;
044  private transient Class<V> valueType;
045
046  /**
047   * Returns a new, empty {@code EnumBiMap} using the specified key and value types.
048   *
049   * @param keyType the key type
050   * @param valueType the value type
051   */
052  public static <K extends Enum<K>, V extends Enum<V>> EnumBiMap<K, V> create(
053      Class<K> keyType, Class<V> valueType) {
054    return new EnumBiMap<>(keyType, valueType);
055  }
056
057  /**
058   * Returns a new bimap with the same mappings as the specified map. If the specified map is an
059   * {@code EnumBiMap}, the new bimap has the same types as the provided map. Otherwise, the
060   * specified map must contain at least one mapping, in order to determine the key and value types.
061   *
062   * @param map the map whose mappings are to be placed in this map
063   * @throws IllegalArgumentException if map is not an {@code EnumBiMap} instance and contains no
064   *     mappings
065   */
066  public static <K extends Enum<K>, V extends Enum<V>> EnumBiMap<K, V> create(Map<K, V> map) {
067    EnumBiMap<K, V> bimap = create(inferKeyType(map), inferValueType(map));
068    bimap.putAll(map);
069    return bimap;
070  }
071
072  private EnumBiMap(Class<K> keyType, Class<V> valueType) {
073    super(new EnumMap<K, V>(keyType), new EnumMap<V, K>(valueType));
074    this.keyType = keyType;
075    this.valueType = valueType;
076  }
077
078  static <K extends Enum<K>> Class<K> inferKeyType(Map<K, ?> map) {
079    if (map instanceof EnumBiMap) {
080      return ((EnumBiMap<K, ?>) map).keyType();
081    }
082    if (map instanceof EnumHashBiMap) {
083      return ((EnumHashBiMap<K, ?>) map).keyType();
084    }
085    checkArgument(!map.isEmpty());
086    return map.keySet().iterator().next().getDeclaringClass();
087  }
088
089  private static <V extends Enum<V>> Class<V> inferValueType(Map<?, V> map) {
090    if (map instanceof EnumBiMap) {
091      return ((EnumBiMap<?, V>) map).valueType;
092    }
093    checkArgument(!map.isEmpty());
094    return map.values().iterator().next().getDeclaringClass();
095  }
096
097  /** Returns the associated key type. */
098  public Class<K> keyType() {
099    return keyType;
100  }
101
102  /** Returns the associated value type. */
103  public Class<V> valueType() {
104    return valueType;
105  }
106
107  @Override
108  K checkKey(K key) {
109    return checkNotNull(key);
110  }
111
112  @Override
113  V checkValue(V value) {
114    return checkNotNull(value);
115  }
116
117  /**
118   * @serialData the key class, value class, number of entries, first key, first value, second key,
119   *     second value, and so on.
120   */
121  @GwtIncompatible // java.io.ObjectOutputStream
122  private void writeObject(ObjectOutputStream stream) throws IOException {
123    stream.defaultWriteObject();
124    stream.writeObject(keyType);
125    stream.writeObject(valueType);
126    Serialization.writeMap(this, stream);
127  }
128
129  @SuppressWarnings("unchecked") // reading fields populated by writeObject
130  @GwtIncompatible // java.io.ObjectInputStream
131  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
132    stream.defaultReadObject();
133    keyType = (Class<K>) stream.readObject();
134    valueType = (Class<V>) stream.readObject();
135    setDelegates(new EnumMap<K, V>(keyType), new EnumMap<V, K>(valueType));
136    Serialization.populateMap(this, stream);
137  }
138
139  @GwtIncompatible // not needed in emulated source.
140  private static final long serialVersionUID = 0;
141}