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