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 17 package com.google.common.collect; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.errorprone.annotations.CanIgnoreReturnValue; 24 import java.io.IOException; 25 import java.io.ObjectInputStream; 26 import java.io.ObjectOutputStream; 27 import java.util.EnumMap; 28 import java.util.HashMap; 29 import java.util.Map; 30 import org.checkerframework.checker.nullness.compatqual.NullableDecl; 31 32 /** 33 * A {@code BiMap} backed by an {@code EnumMap} instance for keys-to-values, and a {@code HashMap} 34 * instance for values-to-keys. Null keys are not permitted, but null values are. An {@code 35 * EnumHashBiMap} and its inverse are both serializable. 36 * 37 * <p>See the Guava User Guide article on <a href= 38 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#bimap"> {@code BiMap}</a>. 39 * 40 * @author Mike Bostock 41 * @since 2.0 42 */ 43 @GwtCompatible(emulated = true) 44 public final class EnumHashBiMap<K extends Enum<K>, V> extends AbstractBiMap<K, V> { 45 private transient Class<K> keyType; 46 47 /** 48 * Returns a new, empty {@code EnumHashBiMap} using the specified key type. 49 * 50 * @param keyType the key type 51 */ create(Class<K> keyType)52 public static <K extends Enum<K>, V> EnumHashBiMap<K, V> create(Class<K> keyType) { 53 return new EnumHashBiMap<>(keyType); 54 } 55 56 /** 57 * Constructs a new bimap with the same mappings as the specified map. If the specified map is an 58 * {@code EnumHashBiMap} or an {@link EnumBiMap}, the new bimap has the same key type as the input 59 * bimap. Otherwise, the specified map must contain at least one mapping, in order to determine 60 * the key type. 61 * 62 * @param map the map whose mappings are to be placed in this map 63 * @throws IllegalArgumentException if map is not an {@code EnumBiMap} or an {@code EnumHashBiMap} 64 * instance and contains no mappings 65 */ create(Map<K, ? extends V> map)66 public static <K extends Enum<K>, V> EnumHashBiMap<K, V> create(Map<K, ? extends V> map) { 67 EnumHashBiMap<K, V> bimap = create(EnumBiMap.inferKeyType(map)); 68 bimap.putAll(map); 69 return bimap; 70 } 71 EnumHashBiMap(Class<K> keyType)72 private EnumHashBiMap(Class<K> keyType) { 73 super( 74 new EnumMap<K, V>(keyType), 75 Maps.<V, K>newHashMapWithExpectedSize(keyType.getEnumConstants().length)); 76 this.keyType = keyType; 77 } 78 79 // Overriding these 3 methods to show that values may be null (but not keys) 80 81 @Override checkKey(K key)82 K checkKey(K key) { 83 return checkNotNull(key); 84 } 85 86 @CanIgnoreReturnValue 87 @Override put(K key, @NullableDecl V value)88 public V put(K key, @NullableDecl V value) { 89 return super.put(key, value); 90 } 91 92 @CanIgnoreReturnValue 93 @Override forcePut(K key, @NullableDecl V value)94 public V forcePut(K key, @NullableDecl V value) { 95 return super.forcePut(key, value); 96 } 97 98 /** Returns the associated key type. */ keyType()99 public Class<K> keyType() { 100 return keyType; 101 } 102 103 /** 104 * @serialData the key class, number of entries, first key, first value, second key, second value, 105 * and so on. 106 */ 107 @GwtIncompatible // java.io.ObjectOutputStream writeObject(ObjectOutputStream stream)108 private void writeObject(ObjectOutputStream stream) throws IOException { 109 stream.defaultWriteObject(); 110 stream.writeObject(keyType); 111 Serialization.writeMap(this, stream); 112 } 113 114 @SuppressWarnings("unchecked") // reading field populated by writeObject 115 @GwtIncompatible // java.io.ObjectInputStream readObject(ObjectInputStream stream)116 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 117 stream.defaultReadObject(); 118 keyType = (Class<K>) stream.readObject(); 119 setDelegates( 120 new EnumMap<K, V>(keyType), new HashMap<V, K>(keyType.getEnumConstants().length * 3 / 2)); 121 Serialization.populateMap(this, stream); 122 } 123 124 @GwtIncompatible // only needed in emulated source. 125 private static final long serialVersionUID = 0; 126 } 127