    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 *      https://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 */
    017
    018package org.apache.commons.collections4;
    019
    020import java.util.Set;
    021
    022/**
    023 * Defines a map that allows bidirectional lookup between key and values.
    024 * <p>
    025 * 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
    026 * {@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
    027 * {@code BidiMap}.
    028 * </p>
    029 * <p>
    030 * 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.
    031 * </p>
    032 * <p>
    033 * 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
    034 * required so that "inverting" the map results in a map without duplicate keys. See the {@link #put} method description for more information.
    035 * </p>
    036 *
    037 * @param <K> The type of the keys in the map
    038 * @param <V> The type of the values in the map
    039 * @since 3.0
    040 */
    041public interface BidiMap<K, V> extends IterableMap<K, V> {
    042
    043    /**
    044     * Gets the key that is currently mapped to the specified value.
    045     * <p>
    046     * If the value is not contained in the map, {@code null} is returned.
    047     * </p>
    048     * <p>
    049     * Implementations should seek to make this method perform equally as well as {@code get(Object)}.
    050     * </p>
    051     *
    052     * @param value The value to find the key for.
    053     * @return The mapped key, or {@code null} if not found.
    054     * @throws ClassCastException   (optional) if the map limits the type of the value and the specified value is inappropriate.
    055     * @throws NullPointerException (optional) if the map limits the values to non-null and null was specified.
    056     */
    057    K getKey(Object value);
    058
    059    /**
    060     * Gets a view of this map where the keys and values are reversed.
    061     * <p>
    062     * 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}.
    063     * </p>
    064     * <p>
    065     * Implementations should seek to avoid creating a new object every time this method is called. See {@code AbstractMap.values()} etc. Calling this method on
    066     * the inverse map should return the original.
    067     * </p>
    068     *
    069     * @return An inverted bidirectional map
    070     */
    071    BidiMap<V, K> inverseBidiMap();
    072
    073    /**
    074     * Puts the key-value pair into the map, replacing any previous pair.
    075     * <p>
    076     * 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
    077     * occurs once in the inverse map.
    078     * </p>
    079     *
    080     * <pre>
    081     * BidiMap map1 = new DualHashBidiMap();
    082     * map.put("A", "B"); // contains A mapped to B, as per Map
    083     * map.put("A", "C"); // contains A mapped to C, as per Map
    084     * BidiMap map2 = new DualHashBidiMap();
    085     * map.put("A", "B"); // contains A mapped to B, as per Map
    086     * map.put("C", "B"); // contains C mapped to B, key A is removed
    087     * </pre>
    088     *
    089     * @param key   The key to store.
    090     * @param value The value to store.
    091     * @return The previous value mapped to this key.
    092     * @throws UnsupportedOperationException if the {@code put} method is not supported.
    093     * @throws ClassCastException            (optional) if the map limits the type of the value and the specified value is inappropriate.
    094     * @throws IllegalArgumentException      (optional) if the map limits the values in some way and the value was invalid.
    095     * @throws NullPointerException          (optional) if the map limits the values to non-null and null was specified.
    096     */
    097    @Override
    098    V put(K key, V value);
    099
    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}