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
017package com.google.common.collect;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.util.Collection;
022import java.util.Map;
023import java.util.Map.Entry;
024import java.util.Set;
025import javax.annotation.Nullable;
026
027/**
028 * A {@code Multimap} that cannot hold duplicate key-value pairs. Adding a
029 * key-value pair that's already in the multimap has no effect. See the {@link
030 * Multimap} documentation for information common to all multimaps.
031 *
032 * <p>The {@link #get}, {@link #removeAll}, and {@link #replaceValues} methods
033 * each return a {@link Set} of values, while {@link #entries} returns a {@code
034 * Set} of map entries. Though the method signature doesn't say so explicitly,
035 * the map returned by {@link #asMap} has {@code Set} values.
036 *
037 * <p>If the values corresponding to a single key should be ordered according to
038 * a {@link java.util.Comparator} (or the natural order), see the
039 * {@link SortedSetMultimap} subinterface.
040 *
041 * <p>Since the value collections are sets, the behavior of a {@code SetMultimap}
042 * is not specified if key <em>or value</em> objects already present in the
043 * multimap change in a manner that affects {@code equals} comparisons.
044 * Use caution if mutable objects are used as keys or values in a
045 * {@code SetMultimap}.
046 *
047 * <p>See the Guava User Guide article on <a href=
048 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multimap">
049 * {@code Multimap}</a>.
050 *
051 * @author Jared Levy
052 * @since 2.0
053 */
054@GwtCompatible
055public interface SetMultimap<K, V> extends Multimap<K, V> {
056  /**
057   * {@inheritDoc}
058   *
059   * <p>Because a {@code SetMultimap} has unique values for a given key, this
060   * method returns a {@link Set}, instead of the {@link java.util.Collection}
061   * specified in the {@link Multimap} interface.
062   */
063  @Override
064  Set<V> get(@Nullable K key);
065
066  /**
067   * {@inheritDoc}
068   *
069   * <p>Because a {@code SetMultimap} has unique values for a given key, this
070   * method returns a {@link Set}, instead of the {@link java.util.Collection}
071   * specified in the {@link Multimap} interface.
072   */
073  @CanIgnoreReturnValue
074  @Override
075  Set<V> removeAll(@Nullable Object key);
076
077  /**
078   * {@inheritDoc}
079   *
080   * <p>Because a {@code SetMultimap} has unique values for a given key, this
081   * method returns a {@link Set}, instead of the {@link java.util.Collection}
082   * specified in the {@link Multimap} interface.
083   *
084   * <p>Any duplicates in {@code values} will be stored in the multimap once.
085   */
086  @CanIgnoreReturnValue
087  @Override
088  Set<V> replaceValues(K key, Iterable<? extends V> values);
089
090  /**
091   * {@inheritDoc}
092   *
093   * <p>Because a {@code SetMultimap} has unique values for a given key, this
094   * method returns a {@link Set}, instead of the {@link java.util.Collection}
095   * specified in the {@link Multimap} interface.
096   */
097  @Override
098  Set<Entry<K, V>> entries();
099
100  /**
101   * {@inheritDoc}
102   *
103   * <p><b>Note:</b> The returned map's values are guaranteed to be of type
104   * {@link Set}. To obtain this map with the more specific generic type
105   * {@code Map<K, Set<V>>}, call {@link Multimaps#asMap(SetMultimap)} instead.
106   */
107  @Override
108  Map<K, Collection<V>> asMap();
109
110  /**
111   * Compares the specified object to this multimap for equality.
112   *
113   * <p>Two {@code SetMultimap} instances are equal if, for each key, they
114   * contain the same values. Equality does not depend on the ordering of keys
115   * or values.
116   *
117   * <p>An empty {@code SetMultimap} is equal to any other empty {@code
118   * Multimap}, including an empty {@code ListMultimap}.
119   */
120  @Override
121  boolean equals(@Nullable Object obj);
122}