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