AbstractSortedSetMultimap.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
1/* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.common.collect; 18 19import com.google.common.annotations.GwtCompatible; 20 21import java.util.Collection; 22import java.util.Map; 23import java.util.SortedSet; 24 25import javax.annotation.Nullable; 26 27/** 28 * Basic implementation of the {@link SortedSetMultimap} interface. It's a 29 * wrapper around {@link AbstractMultimap} that converts the returned 30 * collections into sorted sets. The {@link #createCollection} method 31 * must return a {@code SortedSet}. 32 * 33 * @author Jared Levy 34 */ 35@GwtCompatible 36abstract class AbstractSortedSetMultimap<K, V> 37 extends AbstractSetMultimap<K, V> implements SortedSetMultimap<K, V> { 38 /** 39 * Creates a new multimap that uses the provided map. 40 * 41 * @param map place to store the mapping from each key to its corresponding 42 * values 43 */ 44 protected AbstractSortedSetMultimap(Map<K, Collection<V>> map) { 45 super(map); 46 } 47 48 @Override abstract SortedSet<V> createCollection(); 49 50 // Following Javadoc copied from Multimap and SortedSetMultimap. 51 52 /** 53 * Returns a collection view of all values associated with a key. If no 54 * mappings in the multimap have the provided key, an empty collection is 55 * returned. 56 * 57 * <p>Changes to the returned collection will update the underlying multimap, 58 * and vice versa. 59 * 60 * <p>Because a {@code SortedSetMultimap} has unique sorted values for a given 61 * key, this method returns a {@link SortedSet}, instead of the 62 * {@link Collection} specified in the {@link Multimap} interface. 63 */ 64 @Override public SortedSet<V> get(@Nullable K key) { 65 return (SortedSet<V>) super.get(key); 66 } 67 68 /** 69 * Removes all values associated with a given key. The returned collection is 70 * immutable. 71 * 72 * <p>Because a {@code SortedSetMultimap} has unique sorted values for a given 73 * key, this method returns a {@link SortedSet}, instead of the 74 * {@link Collection} specified in the {@link Multimap} interface. 75 */ 76 @Override public SortedSet<V> removeAll(@Nullable Object key) { 77 return (SortedSet<V>) super.removeAll(key); 78 } 79 80 /** 81 * Stores a collection of values with the same key, replacing any existing 82 * values for that key. The returned collection is immutable. 83 * 84 * <p>Because a {@code SortedSetMultimap} has unique sorted values for a given 85 * key, this method returns a {@link SortedSet}, instead of the 86 * {@link Collection} specified in the {@link Multimap} interface. 87 * 88 * <p>Any duplicates in {@code values} will be stored in the multimap once. 89 */ 90 @Override public SortedSet<V> replaceValues( 91 K key, Iterable<? extends V> values) { 92 return (SortedSet<V>) super.replaceValues(key, values); 93 } 94 95 /** 96 * Returns a map view that associates each key with the corresponding values 97 * in the multimap. Changes to the returned map, such as element removal, will 98 * update the underlying multimap. The map does not support {@code setValue} 99 * on its entries, {@code put}, or {@code putAll}. 100 * 101 * <p>When passed a key that is present in the map, {@code 102 * asMap().get(Object)} has the same behavior as {@link #get}, returning a 103 * live collection. When passed a key that is not present, however, {@code 104 * asMap().get(Object)} returns {@code null} instead of an empty collection. 105 * 106 * <p>Though the method signature doesn't say so explicitly, the returned map 107 * has {@link SortedSet} values. 108 */ 109 @Override public Map<K, Collection<V>> asMap() { 110 return super.asMap(); 111 } 112 113 /** 114 * {@inheritDoc} 115 * 116 * Consequently, the values do not follow their natural ordering or the 117 * ordering of the value comparator. 118 */ 119 @Override public Collection<V> values() { 120 return super.values(); 121 } 122 123 private static final long serialVersionUID = 430848587173315748L; 124} 125