1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.collections4;
19
20 import java.util.Set;
21
22 /**
23 * Defines a map that allows bidirectional lookup between key and values.
24 * <p>
25 * This extended {@code Map} represents a mapping where a key may lookup a value and a value may lookup a key with equal ease. This interface extends
26 * {@code Map} and so may be used anywhere a map is required. The interface provides an inverse map view, enabling full access to both directions of the
27 * {@code BidiMap}.
28 * </p>
29 * <p>
30 * Implementations should allow a value to be looked up from a key and a key to be looked up from a value with equal performance.
31 * </p>
32 * <p>
33 * This map enforces the restriction that there is a 1:1 relation between keys and values, meaning that multiple keys cannot map to the same value. This is
34 * required so that "inverting" the map results in a map without duplicate keys. See the {@link #put} method description for more information.
35 * </p>
36 *
37 * @param <K> The type of the keys in the map
38 * @param <V> The type of the values in the map
39 * @since 3.0
40 */
41 public interface BidiMap<K, V> extends IterableMap<K, V> {
42
43 /**
44 * Gets the key that is currently mapped to the specified value.
45 * <p>
46 * If the value is not contained in the map, {@code null} is returned.
47 * </p>
48 * <p>
49 * Implementations should seek to make this method perform equally as well as {@code get(Object)}.
50 * </p>
51 *
52 * @param value The value to find the key for.
53 * @return The mapped key, or {@code null} if not found.
54 * @throws ClassCastException (optional) if the map limits the type of the value and the specified value is inappropriate.
55 * @throws NullPointerException (optional) if the map limits the values to non-null and null was specified.
56 */
57 K getKey(Object value);
58
59 /**
60 * Gets a view of this map where the keys and values are reversed.
61 * <p>
62 * Changes to one map will be visible in the other and vice versa. This enables both directions of the map to be accessed as a {@code Map}.
63 * </p>
64 * <p>
65 * Implementations should seek to avoid creating a new object every time this method is called. See {@code AbstractMap.values()} etc. Calling this method on
66 * the inverse map should return the original.
67 * </p>
68 *
69 * @return An inverted bidirectional map
70 */
71 BidiMap<V, K> inverseBidiMap();
72
73 /**
74 * Puts the key-value pair into the map, replacing any previous pair.
75 * <p>
76 * When adding a key-value pair, the value may already exist in the map against a different key. That mapping is removed, to ensure that the value only
77 * occurs once in the inverse map.
78 * </p>
79 *
80 * <pre>
81 * BidiMap map1 = new DualHashBidiMap();
82 * map.put("A", "B"); // contains A mapped to B, as per Map
83 * map.put("A", "C"); // contains A mapped to C, as per Map
84 * BidiMap map2 = new DualHashBidiMap();
85 * map.put("A", "B"); // contains A mapped to B, as per Map
86 * map.put("C", "B"); // contains C mapped to B, key A is removed
87 * </pre>
88 *
89 * @param key The key to store.
90 * @param value The value to store.
91 * @return The previous value mapped to this key.
92 * @throws UnsupportedOperationException if the {@code put} method is not supported.
93 * @throws ClassCastException (optional) if the map limits the type of the value and the specified value is inappropriate.
94 * @throws IllegalArgumentException (optional) if the map limits the values in some way and the value was invalid.
95 * @throws NullPointerException (optional) if the map limits the values to non-null and null was specified.
96 */
97 @Override
98 V put(K key, V value);
99
100 /**
101 * Removes the key-value pair that is currently mapped to the specified value (optional operation).
102 * <p>
103 * If the value is not contained in the map, {@code null} is returned.
104 * </p>
105 * <p>
106 * Implementations should seek to make this method perform equally as well as {@code remove(Object)}.
107 * </p>
108 *
109 * @param value The value to find the key-value pair for.
110 * @return The key that was removed, {@code null} if nothing removed.
111 * @throws ClassCastException (optional) if the map limits the type of the value and the specified value is inappropriate.
112 * @throws NullPointerException (optional) if the map limits the values to non-null and null was specified.
113 * @throws UnsupportedOperationException if this method is not supported by the implementation.
114 */
115 K removeValue(Object value);
116
117 /**
118 * Returns a {@link Set} view of the values contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and
119 * vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own {@code remove} operation), the
120 * results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the
121 * {@code Iterator.remove}, {@code Collection.remove}, {@code removeAll}, {@code retainAll} and {@code clear} operations. It does not support the
122 * {@code add} or {@code addAll} operations.
123 *
124 * @return A set view of the values contained in this map.
125 */
126 @Override
127 Set<V> values();
128 }