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