001    /*
002     * Copyright (C) 2007 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.collect;
018    
019    import com.google.common.annotations.GwtCompatible;
020    
021    import java.util.Collection;
022    import java.util.Map;
023    import java.util.Set;
024    
025    import javax.annotation.Nullable;
026    
027    /**
028     * A collection similar to a {@code Map}, but which may associate multiple
029     * values with a single key. If you call {@link #put} twice, with the same key
030     * but different values, the multimap contains mappings from the key to both
031     * values.
032     *
033     * <p>The methods {@link #get}, {@link #keySet}, {@link #keys}, {@link #values},
034     * {@link #entries}, and {@link #asMap} return collections that are views of the
035     * multimap. If the multimap is modifiable, updating it can change the contents
036     * of those collections, and updating the collections will change the multimap.
037     * In contrast, {@link #replaceValues} and {@link #removeAll} return collections
038     * that are independent of subsequent multimap changes.
039     *
040     * <p>Depending on the implementation, a multimap may or may not allow duplicate
041     * key-value pairs. In other words, the multimap contents after adding the same
042     * key and value twice varies between implementations. In multimaps allowing
043     * duplicates, the multimap will contain two mappings, and {@code get} will
044     * return a collection that includes the value twice. In multimaps not
045     * supporting duplicates, the multimap will contain a single mapping from the
046     * key to the value, and {@code get} will return a collection that includes the
047     * value once.
048     *
049     * <p>All methods that alter the multimap are optional, and the views returned
050     * by the multimap may or may not be modifiable. When modification isn't
051     * supported, those methods will throw an {@link UnsupportedOperationException}.
052     * 
053     * <p>See the Guava User Guide article on <a href=
054     * "http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap">
055     * {@code Multimap}</a>.
056     *
057     * @author Jared Levy
058     * @param <K> the type of keys maintained by this multimap
059     * @param <V> the type of mapped values
060     * @since 2.0 (imported from Google Collections Library)
061     */
062    @GwtCompatible
063    public interface Multimap<K, V> {
064      // Query Operations
065    
066      /** Returns the number of key-value pairs in the multimap. */
067      int size();
068    
069      /** Returns {@code true} if the multimap contains no key-value pairs. */
070      boolean isEmpty();
071    
072      /**
073       * Returns {@code true} if the multimap contains any values for the specified
074       * key.
075       *
076       * @param key key to search for in multimap
077       */
078      boolean containsKey(@Nullable Object key);
079    
080      /**
081       * Returns {@code true} if the multimap contains the specified value for any
082       * key.
083       *
084       * @param value value to search for in multimap
085       */
086      boolean containsValue(@Nullable Object value);
087    
088      /**
089       * Returns {@code true} if the multimap contains the specified key-value pair.
090       *
091       * @param key key to search for in multimap
092       * @param value value to search for in multimap
093       */
094      boolean containsEntry(@Nullable Object key, @Nullable Object value);
095    
096      // Modification Operations
097    
098      /**
099       * Stores a key-value pair in the multimap.
100       *
101       * <p>Some multimap implementations allow duplicate key-value pairs, in which
102       * case {@code put} always adds a new key-value pair and increases the
103       * multimap size by 1. Other implementations prohibit duplicates, and storing
104       * a key-value pair that's already in the multimap has no effect.
105       *
106       * @param key key to store in the multimap
107       * @param value value to store in the multimap
108       * @return {@code true} if the method increased the size of the multimap, or
109       *     {@code false} if the multimap already contained the key-value pair and
110       *     doesn't allow duplicates
111       */
112      boolean put(@Nullable K key, @Nullable V value);
113    
114      /**
115       * Removes a key-value pair from the multimap.
116       *
117       * @param key key of entry to remove from the multimap
118       * @param value value of entry to remove the multimap
119       * @return {@code true} if the multimap changed
120       */
121      boolean remove(@Nullable Object key, @Nullable Object value);
122    
123      // Bulk Operations
124    
125      /**
126       * Stores a collection of values with the same key.
127       *
128       * @param key key to store in the multimap
129       * @param values values to store in the multimap
130       * @return {@code true} if the multimap changed
131       */
132      boolean putAll(@Nullable K key, Iterable<? extends V> values);
133    
134      /**
135       * Copies all of another multimap's key-value pairs into this multimap. The
136       * order in which the mappings are added is determined by
137       * {@code multimap.entries()}.
138       *
139       * @param multimap mappings to store in this multimap
140       * @return {@code true} if the multimap changed
141       */
142      boolean putAll(Multimap<? extends K, ? extends V> multimap);
143    
144      /**
145       * Stores a collection of values with the same key, replacing any existing
146       * values for that key.
147       *
148       * @param key key to store in the multimap
149       * @param values values to store in the multimap
150       * @return the collection of replaced values, or an empty collection if no
151       *     values were previously associated with the key. The collection
152       *     <i>may</i> be modifiable, but updating it will have no effect on the
153       *     multimap.
154       */
155      Collection<V> replaceValues(@Nullable K key, Iterable<? extends V> values);
156    
157      /**
158       * Removes all values associated with a given key.
159       *
160       * @param key key of entries to remove from the multimap
161       * @return the collection of removed values, or an empty collection if no
162       *     values were associated with the provided key. The collection
163       *     <i>may</i> be modifiable, but updating it will have no effect on the
164       *     multimap.
165       */
166      Collection<V> removeAll(@Nullable Object key);
167    
168      /**
169       * Removes all key-value pairs from the multimap.
170       */
171      void clear();
172    
173      // Views
174    
175      /**
176       * Returns a collection view of all values associated with a key. If no
177       * mappings in the multimap have the provided key, an empty collection is
178       * returned.
179       *
180       * <p>Changes to the returned collection will update the underlying multimap,
181       * and vice versa.
182       *
183       * @param key key to search for in multimap
184       * @return the collection of values that the key maps to
185       */
186      Collection<V> get(@Nullable K key);
187    
188      /**
189       * Returns the set of all keys, each appearing once in the returned set.
190       * Changes to the returned set will update the underlying multimap, and vice
191       * versa.
192       *
193       * @return the collection of distinct keys
194       */
195      Set<K> keySet();
196    
197      /**
198       * Returns a collection, which may contain duplicates, of all keys. The number
199       * of times of key appears in the returned multiset equals the number of
200       * mappings the key has in the multimap. Changes to the returned multiset will
201       * update the underlying multimap, and vice versa.
202       *
203       * @return a multiset with keys corresponding to the distinct keys of the
204       *     multimap and frequencies corresponding to the number of values that
205       *     each key maps to
206       */
207      Multiset<K> keys();
208    
209      /**
210       * Returns a collection of all values in the multimap. Changes to the returned
211       * collection will update the underlying multimap, and vice versa.
212       *
213       * @return collection of values, which may include the same value multiple
214       *     times if it occurs in multiple mappings
215       */
216      Collection<V> values();
217    
218      /**
219       * Returns a collection of all key-value pairs. Changes to the returned
220       * collection will update the underlying multimap, and vice versa. The entries
221       * collection does not support the {@code add} or {@code addAll} operations.
222       *
223       * @return collection of map entries consisting of key-value pairs
224       */
225      Collection<Map.Entry<K, V>> entries();
226    
227      /**
228       * Returns a map view that associates each key with the corresponding values
229       * in the multimap. Changes to the returned map, such as element removal, will
230       * update the underlying multimap. The map does not support {@code setValue()}
231       * on its entries, {@code put}, or {@code putAll}.
232       *
233       * <p>When passed a key that is present in the map, {@code
234       * asMap().get(Object)} has the same behavior as {@link #get}, returning a
235       * live collection. When passed a key that is not present, however, {@code
236       * asMap().get(Object)} returns {@code null} instead of an empty collection.
237       *
238       * @return a map view from a key to its collection of values
239       */
240      Map<K, Collection<V>> asMap();
241    
242      // Comparison and hashing
243    
244      /**
245       * Compares the specified object with this multimap for equality. Two
246       * multimaps are equal when their map views, as returned by {@link #asMap},
247       * are also equal.
248       *
249       * <p>In general, two multimaps with identical key-value mappings may or may
250       * not be equal, depending on the implementation. For example, two
251       * {@link SetMultimap} instances with the same key-value mappings are equal,
252       * but equality of two {@link ListMultimap} instances depends on the ordering
253       * of the values for each key.
254       *
255       * <p>A non-empty {@link SetMultimap} cannot be equal to a non-empty
256       * {@link ListMultimap}, since their {@link #asMap} views contain unequal
257       * collections as values. However, any two empty multimaps are equal, because
258       * they both have empty {@link #asMap} views.
259       */
260      @Override
261      boolean equals(@Nullable Object obj);
262    
263      /**
264       * Returns the hash code for this multimap.
265       *
266       * <p>The hash code of a multimap is defined as the hash code of the map view,
267       * as returned by {@link Multimap#asMap}.
268       */
269      @Override
270      int hashCode();
271    }