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