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.Comparator;
023import java.util.Map;
024import java.util.Set;
025import java.util.SortedMap;
026import java.util.SortedSet;
027import javax.annotation.CheckForNull;
028import org.checkerframework.checker.nullness.qual.Nullable;
029
030/**
031 * A {@code SetMultimap} whose set of values for a given key are kept sorted; that is, they comprise
032 * a {@link SortedSet}. It cannot hold duplicate key-value pairs; adding a key-value pair that's
033 * already in the multimap has no effect. This interface does not specify the ordering of the
034 * multimap's keys. See the {@link Multimap} documentation for information common to all multimaps.
035 *
036 * <p>The {@link #get}, {@link #removeAll}, and {@link #replaceValues} methods each return a {@link
037 * SortedSet} of values, while {@link Multimap#entries()} returns a {@link Set} of map entries.
038 * Though the method signature doesn't say so explicitly, the map returned by {@link #asMap} has
039 * {@code SortedSet} values.
040 *
041 * <p><b>Warning:</b> As in all {@link SetMultimap}s, do not modify either a key <i>or a value</i>
042 * of a {@code SortedSetMultimap} in a way that affects its {@link Object#equals} behavior (or its
043 * position in the order of the values). Undefined behavior and bugs will result.
044 *
045 * <p>See the Guava User Guide article on <a href=
046 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multimap">{@code Multimap}</a>.
047 *
048 * @author Jared Levy
049 * @since 2.0
050 */
051@GwtCompatible
052@ElementTypesAreNonnullByDefault
053public interface SortedSetMultimap<K extends @Nullable Object, V extends @Nullable Object>
054    extends SetMultimap<K, V> {
055  // Following Javadoc copied from Multimap.
056
057  /**
058   * Returns a collection view of all values associated with a key. If no mappings in the multimap
059   * have the provided key, an empty collection is returned.
060   *
061   * <p>Changes to the returned collection will update the underlying multimap, and vice versa.
062   *
063   * <p>Because a {@code SortedSetMultimap} has unique sorted values for a given key, this method
064   * returns a {@link SortedSet}, instead of the {@link java.util.Collection} specified in the
065   * {@link Multimap} interface.
066   */
067  @Override
068  SortedSet<V> get(@ParametricNullness K key);
069
070  /**
071   * Removes all values associated with a given key.
072   *
073   * <p>Because a {@code SortedSetMultimap} has unique sorted values for a given key, this method
074   * returns a {@link SortedSet}, instead of the {@link java.util.Collection} specified in the
075   * {@link Multimap} interface.
076   */
077  @CanIgnoreReturnValue
078  @Override
079  SortedSet<V> removeAll(@CheckForNull Object key);
080
081  /**
082   * Stores a collection of values with the same key, replacing any existing values for that key.
083   *
084   * <p>Because a {@code SortedSetMultimap} has unique sorted values for a given key, this method
085   * returns a {@link SortedSet}, instead of the {@link java.util.Collection} specified in the
086   * {@link Multimap} interface.
087   *
088   * <p>Any duplicates in {@code values} will be stored in the multimap once.
089   */
090  @CanIgnoreReturnValue
091  @Override
092  SortedSet<V> replaceValues(@ParametricNullness K key, Iterable<? extends V> values);
093
094  /**
095   * Returns a map view that associates each key with the corresponding values in the multimap.
096   * Changes to the returned map, such as element removal, will update the underlying multimap. The
097   * map does not support {@code setValue()} on its entries, {@code put}, or {@code putAll}.
098   *
099   * <p>When passed a key that is present in the map, {@code asMap().get(Object)} has the same
100   * behavior as {@link #get}, returning a live collection. When passed a key that is not present,
101   * however, {@code asMap().get(Object)} returns {@code null} instead of an empty collection.
102   *
103   * <p><b>Note:</b> The returned map's values are guaranteed to be of type {@link SortedSet}. To
104   * obtain this map with the more specific generic type {@code Map<K, SortedSet<V>>}, call {@link
105   * Multimaps#asMap(SortedSetMultimap)} instead. <b>However</b>, the returned map <i>itself</i> is
106   * not necessarily a {@link SortedMap}: A {@code SortedSetMultimap} must expose the <i>values</i>
107   * for a given key in sorted order, but it need not expose the <i>keys</i> in sorted order.
108   * Individual {@code SortedSetMultimap} implementations, like those built with {@link
109   * MultimapBuilder#treeKeys()}, may make additional guarantees.
110   */
111  @Override
112  Map<K, Collection<V>> asMap();
113
114  /**
115   * Returns the comparator that orders the multimap values, with {@code null} indicating that
116   * natural ordering is used.
117   */
118  @CheckForNull
119  Comparator<? super V> valueComparator();
120}