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
055public interface SetMultimap<K extends @Nullable Object, V extends @Nullable Object>
056    extends Multimap<K, V> {
057  /**
058   * {@inheritDoc}
059   *
060   * <p>Because a {@code SetMultimap} has unique values for a given key, this method returns a
061   * {@link Set}, instead of the {@link java.util.Collection} specified in the {@link Multimap}
062   * interface.
063   */
064  @Override
065  Set<V> get(@ParametricNullness K key);
066
067  /**
068   * {@inheritDoc}
069   *
070   * <p>Because a {@code SetMultimap} has unique values for a given key, this method returns a
071   * {@link Set}, instead of the {@link java.util.Collection} specified in the {@link Multimap}
072   * interface.
073   */
074  @CanIgnoreReturnValue
075  @Override
076  Set<V> removeAll(@CheckForNull Object key);
077
078  /**
079   * {@inheritDoc}
080   *
081   * <p>Because a {@code SetMultimap} has unique values for a given key, this method returns a
082   * {@link Set}, instead of the {@link java.util.Collection} specified in the {@link Multimap}
083   * interface.
084   *
085   * <p>Any duplicates in {@code values} will be stored in the multimap once.
086   */
087  @CanIgnoreReturnValue
088  @Override
089  Set<V> replaceValues(@ParametricNullness K key, Iterable<? extends V> values);
090
091  /**
092   * {@inheritDoc}
093   *
094   * <p>Because a {@code SetMultimap} has unique values for a given key, this method returns a
095   * {@link Set}, instead of the {@link java.util.Collection} specified in the {@link Multimap}
096   * interface.
097   */
098  @Override
099  Set<Entry<K, V>> entries();
100
101  /**
102   * {@inheritDoc}
103   *
104   * <p><b>Note:</b> The returned map's values are guaranteed to be of type {@link Set}. To obtain
105   * this map with the more specific generic type {@code Map<K, Set<V>>}, call {@link
106   * Multimaps#asMap(SetMultimap)} instead.
107   */
108  @Override
109  Map<K, Collection<V>> asMap();
110
111  /**
112   * Compares the specified object to this multimap for equality.
113   *
114   * <p>Two {@code SetMultimap} instances are equal if, for each key, they contain the same values.
115   * Equality does not depend on the ordering of keys or values.
116   *
117   * <p>An empty {@code SetMultimap} is equal to any other empty {@code Multimap}, including an
118   * empty {@code ListMultimap}.
119   */
120  @Override
121  boolean equals(@CheckForNull Object obj);
122}