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