001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import com.google.common.annotations.GwtCompatible; 020 021import java.util.Map; 022import java.util.Set; 023 024import javax.annotation.Nullable; 025 026/** 027 * A bimap (or "bidirectional map") is a map that preserves the uniqueness of 028 * its values as well as that of its keys. This constraint enables bimaps to 029 * support an "inverse view", which is another bimap containing the same entries 030 * as this bimap but with reversed keys and values. 031 * 032 * <p>See the Guava User Guide article on <a href= 033 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#bimap"> 034 * {@code BiMap}</a>. 035 * 036 * @author Kevin Bourrillion 037 * @since 2.0 038 */ 039@GwtCompatible 040public interface BiMap<K, V> extends Map<K, V> { 041 // Modification Operations 042 043 /** 044 * {@inheritDoc} 045 * 046 * @throws IllegalArgumentException if the given value is already bound to a 047 * different key in this bimap. The bimap will remain unmodified in this 048 * event. To avoid this exception, call {@link #forcePut} instead. 049 */ 050 @Override 051 @Nullable 052 V put(@Nullable K key, @Nullable V value); 053 054 /** 055 * An alternate form of {@code put} that silently removes any existing entry 056 * with the value {@code value} before proceeding with the {@link #put} 057 * operation. If the bimap previously contained the provided key-value 058 * mapping, this method has no effect. 059 * 060 * <p>Note that a successful call to this method could cause the size of the 061 * bimap to increase by one, stay the same, or even decrease by one. 062 * 063 * <p><b>Warning:</b> If an existing entry with this value is removed, the key 064 * for that entry is discarded and not returned. 065 * 066 * @param key the key with which the specified value is to be associated 067 * @param value the value to be associated with the specified key 068 * @return the value which was previously associated with the key, which may 069 * be {@code null}, or {@code null} if there was no previous entry 070 */ 071 @Nullable 072 V forcePut(@Nullable K key, @Nullable V value); 073 074 // Bulk Operations 075 076 /** 077 * {@inheritDoc} 078 * 079 * <p><b>Warning:</b> the results of calling this method may vary depending on 080 * the iteration order of {@code map}. 081 * 082 * @throws IllegalArgumentException if an attempt to {@code put} any 083 * entry fails. Note that some map entries may have been added to the 084 * bimap before the exception was thrown. 085 */ 086 @Override 087 void putAll(Map<? extends K, ? extends V> map); 088 089 // Views 090 091 /** 092 * {@inheritDoc} 093 * 094 * <p>Because a bimap has unique values, this method returns a {@link Set}, 095 * instead of the {@link java.util.Collection} specified in the {@link Map} 096 * interface. 097 */ 098 @Override 099 Set<V> values(); 100 101 /** 102 * Returns the inverse view of this bimap, which maps each of this bimap's 103 * values to its associated key. The two bimaps are backed by the same data; 104 * any changes to one will appear in the other. 105 * 106 * <p><b>Note:</b>There is no guaranteed correspondence between the iteration 107 * order of a bimap and that of its inverse. 108 * 109 * @return the inverse view of this bimap 110 */ 111 BiMap<V, K> inverse(); 112}