Coverage Summary for Class: AbstractSortedSetMultimap (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| AbstractSortedSetMultimap | 100% (1/1) | 11.1% (1/9) | 14.3% (2/14) |
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 17 package com.google.common.collect; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.errorprone.annotations.CanIgnoreReturnValue; 21 import java.util.Collection; 22 import java.util.Collections; 23 import java.util.Map; 24 import java.util.NavigableSet; 25 import java.util.SortedSet; 26 import org.checkerframework.checker.nullness.qual.Nullable; 27 28 /** 29 * Basic implementation of the {@link SortedSetMultimap} interface. It's a wrapper around {@link 30 * AbstractMapBasedMultimap} that converts the returned collections into sorted sets. The {@link 31 * #createCollection} method must return a {@code SortedSet}. 32 * 33 * @author Jared Levy 34 */ 35 @GwtCompatible 36 abstract class AbstractSortedSetMultimap<K, V> extends AbstractSetMultimap<K, V> 37 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 values 42 */ 43 protected AbstractSortedSetMultimap(Map<K, Collection<V>> map) { 44 super(map); 45 } 46 47 @Override 48 abstract SortedSet<V> createCollection(); 49 50 @Override 51 SortedSet<V> createUnmodifiableEmptyCollection() { 52 return unmodifiableCollectionSubclass(createCollection()); 53 } 54 55 @Override 56 <E> SortedSet<E> unmodifiableCollectionSubclass(Collection<E> collection) { 57 if (collection instanceof NavigableSet) { 58 return Sets.unmodifiableNavigableSet((NavigableSet<E>) collection); 59 } else { 60 return Collections.unmodifiableSortedSet((SortedSet<E>) collection); 61 } 62 } 63 64 @Override 65 Collection<V> wrapCollection(K key, Collection<V> collection) { 66 if (collection instanceof NavigableSet) { 67 return new WrappedNavigableSet(key, (NavigableSet<V>) collection, null); 68 } else { 69 return new WrappedSortedSet(key, (SortedSet<V>) collection, null); 70 } 71 } 72 73 // Following Javadoc copied from Multimap and SortedSetMultimap. 74 75 /** 76 * Returns a collection view of all values associated with a key. If no mappings in the multimap 77 * have the provided key, an empty collection is returned. 78 * 79 * <p>Changes to the returned collection will update the underlying multimap, and vice versa. 80 * 81 * <p>Because a {@code SortedSetMultimap} has unique sorted values for a given key, this method 82 * returns a {@link SortedSet}, instead of the {@link Collection} specified in the {@link 83 * Multimap} interface. 84 */ 85 @Override 86 public SortedSet<V> get(@Nullable K key) { 87 return (SortedSet<V>) super.get(key); 88 } 89 90 /** 91 * Removes all values associated with a given key. The returned collection is immutable. 92 * 93 * <p>Because a {@code SortedSetMultimap} has unique sorted values for a given key, this method 94 * returns a {@link SortedSet}, instead of the {@link Collection} specified in the {@link 95 * Multimap} interface. 96 */ 97 @CanIgnoreReturnValue 98 @Override 99 public SortedSet<V> removeAll(@Nullable Object key) { 100 return (SortedSet<V>) super.removeAll(key); 101 } 102 103 /** 104 * Stores a collection of values with the same key, replacing any existing values for that key. 105 * The returned collection is immutable. 106 * 107 * <p>Because a {@code SortedSetMultimap} has unique sorted values for a given key, this method 108 * returns a {@link SortedSet}, instead of the {@link Collection} specified in the {@link 109 * Multimap} interface. 110 * 111 * <p>Any duplicates in {@code values} will be stored in the multimap once. 112 */ 113 @CanIgnoreReturnValue 114 @Override 115 public SortedSet<V> replaceValues(@Nullable K key, Iterable<? extends V> values) { 116 return (SortedSet<V>) super.replaceValues(key, values); 117 } 118 119 /** 120 * Returns a map view that associates each key with the corresponding values in the multimap. 121 * Changes to the returned map, such as element removal, will update the underlying multimap. The 122 * map does not support {@code setValue} on its entries, {@code put}, or {@code putAll}. 123 * 124 * <p>When passed a key that is present in the map, {@code asMap().get(Object)} has the same 125 * behavior as {@link #get}, returning a live collection. When passed a key that is not present, 126 * however, {@code asMap().get(Object)} returns {@code null} instead of an empty collection. 127 * 128 * <p>Though the method signature doesn't say so explicitly, the returned map has {@link 129 * SortedSet} values. 130 */ 131 @Override 132 public Map<K, Collection<V>> asMap() { 133 return super.asMap(); 134 } 135 136 /** 137 * {@inheritDoc} 138 * 139 * <p>Consequently, the values do not follow their natural ordering or the ordering of the value 140 * comparator. 141 */ 142 @Override 143 public Collection<V> values() { 144 return super.values(); 145 } 146 147 private static final long serialVersionUID = 430848587173315748L; 148 }