001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4;
018
019import java.util.Set;
020
021/**
022 * Defines a map that allows bidirectional lookup between key and values.
023 * <p>
024 * This extended {@code Map} represents a mapping where a key may
025 * lookup a value and a value may lookup a key with equal ease.
026 * This interface extends {@code Map} and so may be used anywhere a map
027 * is required. The interface provides an inverse map view, enabling
028 * full access to both directions of the {@code BidiMap}.
029 * </p>
030 * <p>
031 * Implementations should allow a value to be looked up from a key and
032 * a key to be looked up from a value with equal performance.
033 * </p>
034 * <p>
035 * This map enforces the restriction that there is a 1:1 relation between
036 * keys and values, meaning that multiple keys cannot map to the same value.
037 * This is required so that "inverting" the map results in a map without
038 * duplicate keys. See the {@link #put} method description for more information.
039 * </p>
040 *
041 * @param <K> the type of the keys in the map
042 * @param <V> the type of the values in the map
043 * @since 3.0
044 */
045public interface BidiMap<K, V> extends IterableMap<K, V> {
046
047    /**
048     * Gets the key that is currently mapped to the specified value.
049     * <p>
050     * If the value is not contained in the map, {@code null} is returned.
051     * </p>
052     * <p>
053     * Implementations should seek to make this method perform equally as well
054     * as {@code get(Object)}.
055     * </p>
056     *
057     * @param value  the value to find the key for
058     * @return the mapped key, or {@code null} if not found
059     * @throws ClassCastException (optional) if the map limits the type of the
060     *  value and the specified value is inappropriate
061     * @throws NullPointerException (optional) if the map limits the values to
062     *  non-null and null was specified
063     */
064    K getKey(Object value);
065
066    /**
067     * Gets a view of this map where the keys and values are reversed.
068     * <p>
069     * Changes to one map will be visible in the other and vice versa.
070     * This enables both directions of the map to be accessed as a {@code Map}.
071     * </p>
072     * <p>
073     * Implementations should seek to avoid creating a new object every time this
074     * method is called. See {@code AbstractMap.values()} etc. Calling this
075     * method on the inverse map should return the original.
076     * </p>
077     *
078     * @return an inverted bidirectional map
079     */
080    BidiMap<V, K> inverseBidiMap();
081
082    /**
083     * Puts the key-value pair into the map, replacing any previous pair.
084     * <p>
085     * When adding a key-value pair, the value may already exist in the map
086     * against a different key. That mapping is removed, to ensure that the
087     * value only occurs once in the inverse map.
088     * </p>
089     * <pre>
090     *  BidiMap map1 = new DualHashBidiMap();
091     *  map.put("A","B");  // contains A mapped to B, as per Map
092     *  map.put("A","C");  // contains A mapped to C, as per Map
093     *
094     *  BidiMap map2 = new DualHashBidiMap();
095     *  map.put("A","B");  // contains A mapped to B, as per Map
096     *  map.put("C","B");  // contains C mapped to B, key A is removed
097     * </pre>
098     *
099     * @param key  the key to store
100     * @param value  the value to store
101     * @return the previous value mapped to this key
102     * @throws UnsupportedOperationException if the {@code put} method is not supported
103     * @throws ClassCastException (optional) if the map limits the type of the
104     *  value and the specified value is inappropriate
105     * @throws IllegalArgumentException (optional) if the map limits the values
106     *  in some way and the value was invalid
107     * @throws NullPointerException (optional) if the map limits the values to
108     *  non-null and null was specified
109     */
110    @Override
111    V put(K key, V value);
112
113    /**
114     * Removes the key-value pair that is currently mapped to the specified
115     * value (optional operation).
116     * <p>
117     * If the value is not contained in the map, {@code null} is returned.
118     * </p>
119     * <p>
120     * Implementations should seek to make this method perform equally as well
121     * as {@code remove(Object)}.
122     * </p>
123     *
124     * @param value  the value to find the key-value pair for
125     * @return the key that was removed, {@code null} if nothing removed
126     * @throws ClassCastException (optional) if the map limits the type of the
127     *  value and the specified value is inappropriate
128     * @throws NullPointerException (optional) if the map limits the values to
129     *  non-null and null was specified
130     * @throws UnsupportedOperationException if this method is not supported
131     *  by the implementation
132     */
133    K removeValue(Object value);
134
135    /**
136     * Returns a {@link Set} view of the values contained in this map.
137     * The set is backed by the map, so changes to the map are reflected
138     * in the set, and vice-versa.  If the map is modified while an iteration
139     * over the set is in progress (except through the iterator's own
140     * {@code remove} operation), the results of the iteration are undefined.
141     * The set supports element removal, which removes the corresponding
142     * mapping from the map, via the {@code Iterator.remove},
143     * {@code Collection.remove}, {@code removeAll},
144     * {@code retainAll} and {@code clear} operations.  It does not
145     * support the {@code add} or {@code addAll} operations.
146     *
147     * @return a set view of the values contained in this map
148     */
149    @Override
150    Set<V> values();
151}