All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.collect.EnumBiMap Maven / Gradle / Ivy

Go to download

Guava is a suite of core and expanded libraries that include utility classes, google's collections, io classes, and much much more.

There is a newer version: 33.3.0-jre
Show newest version
/*
 * Copyright (C) 2007 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.EnumMap;
import java.util.Map;

/**
 * A {@code BiMap} backed by two {@code EnumMap} instances. Null keys and values are not permitted.
 * An {@code EnumBiMap} and its inverse are both serializable.
 *
 * 

See the Guava User Guide article on {@code BiMap}. * * @author Mike Bostock * @since 2.0 */ @GwtCompatible(emulated = true) public final class EnumBiMap, V extends Enum> extends AbstractBiMap { private transient Class keyType; private transient Class valueType; /** * Returns a new, empty {@code EnumBiMap} using the specified key and value types. * * @param keyType the key type * @param valueType the value type */ public static , V extends Enum> EnumBiMap create( Class keyType, Class valueType) { return new EnumBiMap<>(keyType, valueType); } /** * Returns a new bimap with the same mappings as the specified map. If the specified map is an * {@code EnumBiMap}, the new bimap has the same types as the provided map. Otherwise, the * specified map must contain at least one mapping, in order to determine the key and value types. * * @param map the map whose mappings are to be placed in this map * @throws IllegalArgumentException if map is not an {@code EnumBiMap} instance and contains no * mappings */ public static , V extends Enum> EnumBiMap create(Map map) { EnumBiMap bimap = create(inferKeyType(map), inferValueType(map)); bimap.putAll(map); return bimap; } private EnumBiMap(Class keyType, Class valueType) { super( WellBehavedMap.wrap(new EnumMap(keyType)), WellBehavedMap.wrap(new EnumMap(valueType))); this.keyType = keyType; this.valueType = valueType; } static > Class inferKeyType(Map map) { if (map instanceof EnumBiMap) { return ((EnumBiMap) map).keyType(); } if (map instanceof EnumHashBiMap) { return ((EnumHashBiMap) map).keyType(); } checkArgument(!map.isEmpty()); return map.keySet().iterator().next().getDeclaringClass(); } private static > Class inferValueType(Map map) { if (map instanceof EnumBiMap) { return ((EnumBiMap) map).valueType; } checkArgument(!map.isEmpty()); return map.values().iterator().next().getDeclaringClass(); } /** Returns the associated key type. */ public Class keyType() { return keyType; } /** Returns the associated value type. */ public Class valueType() { return valueType; } @Override K checkKey(K key) { return checkNotNull(key); } @Override V checkValue(V value) { return checkNotNull(value); } /** * @serialData the key class, value class, number of entries, first key, first value, second key, * second value, and so on. */ @GwtIncompatible // java.io.ObjectOutputStream private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); stream.writeObject(keyType); stream.writeObject(valueType); Serialization.writeMap(this, stream); } @SuppressWarnings("unchecked") // reading fields populated by writeObject @GwtIncompatible // java.io.ObjectInputStream private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); keyType = (Class) stream.readObject(); valueType = (Class) stream.readObject(); setDelegates( WellBehavedMap.wrap(new EnumMap(keyType)), WellBehavedMap.wrap(new EnumMap(valueType))); Serialization.populateMap(this, stream); } @GwtIncompatible // not needed in emulated source. private static final long serialVersionUID = 0; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy