1/*
2 * Copyright (C) 2007 Google Inc.
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
17package com.google.common.collect;
18
19import com.google.common.annotations.GwtCompatible;
20
21import java.util.Map;
22import java.util.Set;
23
24import javax.annotation.Nullable;
25
26/**
27 * A bimap (or "bidirectional map") is a map that preserves the uniqueness of
28 * its values as well as that of its keys. This constraint enables bimaps to
29 * support an "inverse view", which is another bimap containing the same entries
30 * as this bimap but with reversed keys and values.
31 *
32 * @author Kevin Bourrillion
33 * @since 2010.01.04 <b>stable</b> (imported from Google Collections Library)
34 */
35@GwtCompatible
36public interface BiMap<K, V> extends Map<K, V> {
37  // Modification Operations
38
39  /**
40   * {@inheritDoc}
41   *
42   * @throws IllegalArgumentException if the given value is already bound to a
43   *     different key in this bimap. The bimap will remain unmodified in this
44   *     event. To avoid this exception, call {@link #forcePut} instead.
45   */
46  V put(@Nullable K key, @Nullable V value);
47
48  /**
49   * An alternate form of {@code put} that silently removes any existing entry
50   * with the value {@code value} before proceeding with the {@link #put}
51   * operation. If the bimap previously contained the provided key-value
52   * mapping, this method has no effect.
53   *
54   * <p>Note that a successful call to this method could cause the size of the
55   * bimap to increase by one, stay the same, or even decrease by one.
56   *
57   * <p><b>Warning</b>: If an existing entry with this value is removed, the key
58   * for that entry is discarded and not returned.
59   *
60   * @param key the key with which the specified value is to be associated
61   * @param value the value to be associated with the specified key
62   * @return the value which was previously associated with the key, which may
63   *     be {@code null}, or {@code null} if there was no previous entry
64   */
65  V forcePut(@Nullable K key, @Nullable V value);
66
67  // Bulk Operations
68
69  /**
70   * {@inheritDoc}
71   *
72   * <p><b>Warning:</b> the results of calling this method may vary depending on
73   * the iteration order of {@code map}.
74   *
75   * @throws IllegalArgumentException if an attempt to {@code put} any
76   *     entry fails. Note that some map entries may have been added to the
77   *     bimap before the exception was thrown.
78   */
79  void putAll(Map<? extends K, ? extends V> map);
80
81  // Views
82
83  /**
84   * {@inheritDoc}
85   *
86   * <p>Because a bimap has unique values, this method returns a {@link Set},
87   * instead of the {@link java.util.Collection} specified in the {@link Map}
88   * interface.
89   */
90  Set<V> values();
91
92  /**
93   * Returns the inverse view of this bimap, which maps each of this bimap's
94   * values to its associated key. The two bimaps are backed by the same data;
95   * any changes to one will appear in the other.
96   *
97   * <p><b>Note:</b>There is no guaranteed correspondence between the iteration
98   * order of a bimap and that of its inverse.
99   *
100   * @return the inverse view of this bimap
101   */
102  BiMap<V, K> inverse();
103}
104