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