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 com.google.common.annotations.GwtCompatible; 20 import com.google.errorprone.annotations.CanIgnoreReturnValue; 21 import java.util.Map; 22 import java.util.Set; 23 import javax.annotation.CheckForNull; 24 import org.checkerframework.checker.nullness.qual.Nullable; 25 26 /** 27 * A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as 28 * that of its keys. This constraint enables bimaps to support an "inverse view", which is another 29 * bimap containing the same entries as this bimap but with reversed keys and values. 30 * 31 * <p>See the Guava User Guide article on <a href= 32 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#bimap">{@code BiMap}</a>. 33 * 34 * @author Kevin Bourrillion 35 * @since 2.0 36 */ 37 @GwtCompatible 38 @ElementTypesAreNonnullByDefault 39 public interface BiMap<K extends @Nullable Object, V extends @Nullable Object> extends Map<K, V> { 40 // Modification Operations 41 42 /** 43 * {@inheritDoc} 44 * 45 * @throws IllegalArgumentException if the given value is already bound to a different key in this 46 * bimap. The bimap will remain unmodified in this event. To avoid this exception, call {@link 47 * #forcePut} instead. 48 */ 49 @CanIgnoreReturnValue 50 @Override 51 @CheckForNull put(@arametricNullness K key, @ParametricNullness V value)52 V put(@ParametricNullness K key, @ParametricNullness V value); 53 54 /** 55 * An alternate form of {@code put} that silently removes any existing entry with the value {@code 56 * value} before proceeding with the {@link #put} operation. If the bimap previously contained the 57 * provided key-value mapping, this method has no effect. 58 * 59 * <p>Note that a successful call to this method could cause the size of the bimap to increase by 60 * one, stay the same, or even decrease by one. 61 * 62 * <p><b>Warning:</b> If an existing entry with this value is removed, the key for that entry is 63 * discarded and not returned. 64 * 65 * @param key the key with which the specified value is to be associated 66 * @param value the value to be associated with the specified key 67 * @return the value that was previously associated with the key, or {@code null} if there was no 68 * previous entry. (If the bimap contains null values, then {@code forcePut}, like {@code 69 * put}, returns {@code null} both if the key is absent and if it is present with a null 70 * value.) 71 */ 72 @CanIgnoreReturnValue 73 @CheckForNull forcePut(@arametricNullness K key, @ParametricNullness V value)74 V forcePut(@ParametricNullness K key, @ParametricNullness V value); 75 76 // Bulk Operations 77 78 /** 79 * {@inheritDoc} 80 * 81 * <p><b>Warning:</b> the results of calling this method may vary depending on the iteration order 82 * of {@code map}. 83 * 84 * @throws IllegalArgumentException if an attempt to {@code put} any entry fails. Note that some 85 * map entries may have been added to the bimap before the exception was thrown. 86 */ 87 @Override putAll(Map<? extends K, ? extends V> map)88 void putAll(Map<? extends K, ? extends V> map); 89 90 // Views 91 92 /** 93 * {@inheritDoc} 94 * 95 * <p>Because a bimap has unique values, this method returns a {@link Set}, instead of the {@link 96 * java.util.Collection} specified in the {@link Map} interface. 97 */ 98 @Override values()99 Set<V> values(); 100 101 /** 102 * Returns the inverse view of this bimap, which maps each of this bimap's values to its 103 * associated key. The two bimaps are backed by the same data; any changes to one will appear in 104 * the other. 105 * 106 * <p><b>Note:</b>There is no guaranteed correspondence between the iteration order of a bimap and 107 * that of its inverse. 108 * 109 * @return the inverse view of this bimap 110 */ inverse()111 BiMap<V, K> inverse(); 112 } 113