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; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.util.Map; 022import java.util.Set; 023import javax.annotation.Nullable; 024 025/** 026 * A bimap (or "bidirectional map") is a map that preserves the uniqueness of 027 * its values as well as that of its keys. This constraint enables bimaps to 028 * support an "inverse view", which is another bimap containing the same entries 029 * as this bimap but with reversed keys and values. 030 * 031 * <p>See the Guava User Guide article on <a href= 032 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#bimap"> 033 * {@code BiMap}</a>. 034 * 035 * @author Kevin Bourrillion 036 * @since 2.0 037 */ 038@GwtCompatible 039public interface BiMap<K, V> extends Map<K, V> { 040 // Modification Operations 041 042 /** 043 * {@inheritDoc} 044 * 045 * @throws IllegalArgumentException if the given value is already bound to a 046 * different key in this bimap. The bimap will remain unmodified in this 047 * event. To avoid this exception, call {@link #forcePut} instead. 048 */ 049 @CanIgnoreReturnValue 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 @CanIgnoreReturnValue 072 @Nullable 073 V forcePut(@Nullable K key, @Nullable V value); 074 075 // Bulk Operations 076 077 /** 078 * {@inheritDoc} 079 * 080 * <p><b>Warning:</b> the results of calling this method may vary depending on 081 * the iteration order of {@code map}. 082 * 083 * @throws IllegalArgumentException if an attempt to {@code put} any 084 * entry fails. Note that some map entries may have been added to the 085 * bimap before the exception was thrown. 086 */ 087 @Override 088 void putAll(Map<? extends K, ? extends V> map); 089 090 // Views 091 092 /** 093 * {@inheritDoc} 094 * 095 * <p>Because a bimap has unique values, this method returns a {@link Set}, 096 * instead of the {@link java.util.Collection} specified in the {@link Map} 097 * interface. 098 */ 099 @Override 100 Set<V> values(); 101 102 /** 103 * Returns the inverse view of this bimap, which maps each of this bimap's 104 * values to its associated key. The two bimaps are backed by the same data; 105 * any changes to one will appear in the other. 106 * 107 * <p><b>Note:</b>There is no guaranteed correspondence between the iteration 108 * order of a bimap and that of its inverse. 109 * 110 * @return the inverse view of this bimap 111 */ 112 BiMap<V, K> inverse(); 113}