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