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