1 /* 2 * Copyright 2003-2004 The Apache Software Foundation 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 package org.apache.commons.collections; 17 18 /** 19 * Defines a map that allows bidirectional lookup between key and values. 20 * <p> 21 * This extended <code>Map</code> represents a mapping where a key may 22 * lookup a value and a value may lookup a key with equal ease. 23 * This interface extends <code>Map</code> and so may be used anywhere a map 24 * is required. The interface provides an inverse map view, enabling 25 * full access to both directions of the <code>BidiMap</code>. 26 * <p> 27 * Implementations should allow a value to be looked up from a key and 28 * a key to be looked up from a value with equal performance. 29 * <p> 30 * This map enforces the restriction that there is a 1:1 relation between 31 * keys and values, meaning that multiple keys cannot map to the same value. 32 * This is required so that "inverting" the map results in a map without 33 * duplicate keys. See the {@link #put} method description for more information. 34 * 35 * @since Commons Collections 3.0 36 * @version $Revision: 1.16 $ $Date: 2004/05/10 20:37:19 $ 37 * 38 * @author Stephen Colebourne 39 */ 40 public interface BidiMap extends IterableMap { 41 42 /** 43 * Obtains a <code>MapIterator</code> over the map. 44 * <p> 45 * A map iterator is an efficient way of iterating over maps. 46 * It does not require that the map is stored using Map Entry objects 47 * which can increase performance. 48 * <pre> 49 * BidiMap map = new DualHashBidiMap(); 50 * MapIterator it = map.mapIterator(); 51 * while (it.hasNext()) { 52 * Object key = it.next(); 53 * Object value = it.getValue(); 54 * it.setValue("newValue"); 55 * } 56 * </pre> 57 * 58 * @return a map iterator 59 */ 60 MapIterator mapIterator(); 61 62 /** 63 * Puts the key-value pair into the map, replacing any previous pair. 64 * <p> 65 * When adding a key-value pair, the value may already exist in the map 66 * against a different key. That mapping is removed, to ensure that the 67 * value only occurs once in the inverse map. 68 * <pre> 69 * BidiMap map1 = new DualHashBidiMap(); 70 * map.put("A","B"); // contains A mapped to B, as per Map 71 * map.put("A","C"); // contains A mapped to C, as per Map 72 * 73 * BidiMap map2 = new DualHashBidiMap(); 74 * map.put("A","B"); // contains A mapped to B, as per Map 75 * map.put("C","B"); // contains C mapped to B, key A is removed 76 * </pre> 77 * 78 * @param key the key to store 79 * @param value the value to store 80 * @return the previous value mapped to this key 81 * 82 * @throws UnsupportedOperationException if the <code>put</code> method is not supported 83 * @throws ClassCastException (optional) if the map limits the type of the 84 * value and the specified value is inappropriate 85 * @throws IllegalArgumentException (optional) if the map limits the values 86 * in some way and the value was invalid 87 * @throws NullPointerException (optional) if the map limits the values to 88 * non-null and null was specified 89 */ 90 Object put(Object key, Object value); 91 92 /** 93 * Gets the key that is currently mapped to the specified value. 94 * <p> 95 * If the value is not contained in the map, <code>null</code> is returned. 96 * <p> 97 * Implementations should seek to make this method perform equally as well 98 * as <code>get(Object)</code>. 99 * 100 * @param value the value to find the key for 101 * @return the mapped key, or <code>null</code> if not found 102 * 103 * @throws ClassCastException (optional) if the map limits the type of the 104 * value and the specified value is inappropriate 105 * @throws NullPointerException (optional) if the map limits the values to 106 * non-null and null was specified 107 */ 108 Object getKey(Object value); 109 110 /** 111 * Removes the key-value pair that is currently mapped to the specified 112 * value (optional operation). 113 * <p> 114 * If the value is not contained in the map, <code>null</code> is returned. 115 * <p> 116 * Implementations should seek to make this method perform equally as well 117 * as <code>remove(Object)</code>. 118 * 119 * @param value the value to find the key-value pair for 120 * @return the key that was removed, <code>null</code> if nothing removed 121 * 122 * @throws ClassCastException (optional) if the map limits the type of the 123 * value and the specified value is inappropriate 124 * @throws NullPointerException (optional) if the map limits the values to 125 * non-null and null was specified 126 * @throws UnsupportedOperationException if this method is not supported 127 * by the implementation 128 */ 129 Object removeValue(Object value); 130 131 /** 132 * Gets a view of this map where the keys and values are reversed. 133 * <p> 134 * Changes to one map will be visible in the other and vice versa. 135 * This enables both directions of the map to be accessed as a <code>Map</code>. 136 * <p> 137 * Implementations should seek to avoid creating a new object every time this 138 * method is called. See <code>AbstractMap.values()</code> etc. Calling this 139 * method on the inverse map should return the original. 140 * 141 * @return an inverted bidirectional map 142 */ 143 BidiMap inverseBidiMap(); 144 145 } 146