Coverage Summary for Class: EnumBiMap (com.google.common.collect)

Class Class, % Method, % Line, %
EnumBiMap 0% (0/1) 0% (0/11) 0% (0/30)


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