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 static com.google.common.base.Preconditions.checkNotNull; 020import static java.util.Objects.requireNonNull; 021 022import com.google.common.annotations.GwtCompatible; 023import com.google.common.annotations.GwtIncompatible; 024import com.google.common.annotations.J2ktIncompatible; 025import java.io.IOException; 026import java.io.ObjectInputStream; 027import java.io.ObjectOutputStream; 028import java.util.Collection; 029import java.util.Comparator; 030import java.util.Map; 031import java.util.NavigableMap; 032import java.util.NavigableSet; 033import java.util.SortedSet; 034import java.util.TreeMap; 035import java.util.TreeSet; 036import org.checkerframework.checker.nullness.qual.Nullable; 037 038/** 039 * Implementation of {@code Multimap} whose keys and values are ordered by their natural ordering or 040 * by supplied comparators. In all cases, this implementation uses {@link Comparable#compareTo} or 041 * {@link Comparator#compare} instead of {@link Object#equals} to determine equivalence of 042 * instances. 043 * 044 * <p><b>Warning:</b> The comparators or comparables used must be <i>consistent with equals</i> as 045 * explained by the {@link Comparable} class specification. Otherwise, the resulting multiset will 046 * violate the general contract of {@link SetMultimap}, which is specified in terms of {@link 047 * Object#equals}. 048 * 049 * <p>The collections returned by {@code keySet} and {@code asMap} iterate through the keys 050 * according to the key comparator ordering or the natural ordering of the keys. Similarly, {@code 051 * get}, {@code removeAll}, and {@code replaceValues} return collections that iterate through the 052 * values according to the value comparator ordering or the natural ordering of the values. The 053 * collections generated by {@code entries}, {@code keys}, and {@code values} iterate across the 054 * keys according to the above key ordering, and for each key they iterate across the values 055 * according to the value ordering. 056 * 057 * <p>The multimap does not store duplicate key-value pairs. Adding a new key-value pair equal to an 058 * existing key-value pair has no effect. 059 * 060 * <p>Null keys and values are permitted (provided, of course, that the respective comparators 061 * support them). All optional multimap methods are supported, and all returned views are 062 * modifiable. 063 * 064 * <p>This class is not threadsafe when any concurrent operations update the multimap. Concurrent 065 * read operations will work correctly. To allow concurrent update operations, wrap your multimap 066 * with a call to {@link Multimaps#synchronizedSortedSetMultimap}. 067 * 068 * <p>See the Guava User Guide article on <a href= 069 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multimap">{@code Multimap}</a>. 070 * 071 * @author Jared Levy 072 * @author Louis Wasserman 073 * @since 2.0 074 */ 075@GwtCompatible(serializable = true, emulated = true) 076@ElementTypesAreNonnullByDefault 077public class TreeMultimap<K extends @Nullable Object, V extends @Nullable Object> 078 extends AbstractSortedKeySortedSetMultimap<K, V> { 079 private transient Comparator<? super K> keyComparator; 080 private transient Comparator<? super V> valueComparator; 081 082 /** 083 * Creates an empty {@code TreeMultimap} ordered by the natural ordering of its keys and values. 084 */ 085 @SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989 086 public static <K extends Comparable, V extends Comparable> TreeMultimap<K, V> create() { 087 return new TreeMultimap<>(Ordering.natural(), Ordering.natural()); 088 } 089 090 /** 091 * Creates an empty {@code TreeMultimap} instance using explicit comparators. Neither comparator 092 * may be null; use {@link Ordering#natural()} to specify natural order. 093 * 094 * @param keyComparator the comparator that determines the key ordering 095 * @param valueComparator the comparator that determines the value ordering 096 */ 097 public static <K extends @Nullable Object, V extends @Nullable Object> TreeMultimap<K, V> create( 098 Comparator<? super K> keyComparator, Comparator<? super V> valueComparator) { 099 return new TreeMultimap<>(checkNotNull(keyComparator), checkNotNull(valueComparator)); 100 } 101 102 /** 103 * Constructs a {@code TreeMultimap}, ordered by the natural ordering of its keys and values, with 104 * the same mappings as the specified multimap. 105 * 106 * @param multimap the multimap whose contents are copied to this multimap 107 */ 108 @SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989 109 public static <K extends Comparable, V extends Comparable> TreeMultimap<K, V> create( 110 Multimap<? extends K, ? extends V> multimap) { 111 return new TreeMultimap<>(Ordering.natural(), Ordering.natural(), multimap); 112 } 113 114 TreeMultimap(Comparator<? super K> keyComparator, Comparator<? super V> valueComparator) { 115 super(new TreeMap<K, Collection<V>>(keyComparator)); 116 this.keyComparator = keyComparator; 117 this.valueComparator = valueComparator; 118 } 119 120 private TreeMultimap( 121 Comparator<? super K> keyComparator, 122 Comparator<? super V> valueComparator, 123 Multimap<? extends K, ? extends V> multimap) { 124 this(keyComparator, valueComparator); 125 putAll(multimap); 126 } 127 128 @Override 129 Map<K, Collection<V>> createAsMap() { 130 return createMaybeNavigableAsMap(); 131 } 132 133 /** 134 * {@inheritDoc} 135 * 136 * <p>Creates an empty {@code TreeSet} for a collection of values for one key. 137 * 138 * @return a new {@code TreeSet} containing a collection of values for one key 139 */ 140 @Override 141 SortedSet<V> createCollection() { 142 return new TreeSet<>(valueComparator); 143 } 144 145 @Override 146 Collection<V> createCollection(@ParametricNullness K key) { 147 if (key == null) { 148 int unused = keyComparator().compare(key, key); 149 } 150 return super.createCollection(key); 151 } 152 153 /** 154 * Returns the comparator that orders the multimap keys. 155 * 156 * @deprecated Use {@code ((NavigableSet<K>) multimap.keySet()).comparator()} instead. 157 */ 158 @Deprecated 159 public Comparator<? super K> keyComparator() { 160 return keyComparator; 161 } 162 163 @Override 164 public Comparator<? super V> valueComparator() { 165 return valueComparator; 166 } 167 168 /** @since 14.0 (present with return type {@code SortedSet} since 2.0) */ 169 @Override 170 @GwtIncompatible // NavigableSet 171 public NavigableSet<V> get(@ParametricNullness K key) { 172 return (NavigableSet<V>) super.get(key); 173 } 174 175 /** 176 * {@inheritDoc} 177 * 178 * <p>Because a {@code TreeMultimap} has unique sorted keys, this method returns a {@link 179 * NavigableSet}, instead of the {@link java.util.Set} specified in the {@link Multimap} 180 * interface. 181 * 182 * @since 14.0 (present with return type {@code SortedSet} since 2.0) 183 */ 184 @Override 185 public NavigableSet<K> keySet() { 186 return (NavigableSet<K>) super.keySet(); 187 } 188 189 /** 190 * {@inheritDoc} 191 * 192 * <p>Because a {@code TreeMultimap} has unique sorted keys, this method returns a {@link 193 * NavigableMap}, instead of the {@link java.util.Map} specified in the {@link Multimap} 194 * interface. 195 * 196 * @since 14.0 (present with return type {@code SortedMap} since 2.0) 197 */ 198 @Override 199 public NavigableMap<K, Collection<V>> asMap() { 200 return (NavigableMap<K, Collection<V>>) super.asMap(); 201 } 202 203 /** 204 * @serialData key comparator, value comparator, number of distinct keys, and then for each 205 * distinct key: the key, number of values for that key, and key values 206 */ 207 @GwtIncompatible // java.io.ObjectOutputStream 208 @J2ktIncompatible 209 private void writeObject(ObjectOutputStream stream) throws IOException { 210 stream.defaultWriteObject(); 211 stream.writeObject(keyComparator()); 212 stream.writeObject(valueComparator()); 213 Serialization.writeMultimap(this, stream); 214 } 215 216 @GwtIncompatible // java.io.ObjectInputStream 217 @J2ktIncompatible 218 @SuppressWarnings("unchecked") // reading data stored by writeObject 219 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 220 stream.defaultReadObject(); 221 keyComparator = requireNonNull((Comparator<? super K>) stream.readObject()); 222 valueComparator = requireNonNull((Comparator<? super V>) stream.readObject()); 223 setMap(new TreeMap<K, Collection<V>>(keyComparator)); 224 Serialization.populateMultimap(this, stream); 225 } 226 227 @GwtIncompatible // not needed in emulated source 228 @J2ktIncompatible 229 private static final long serialVersionUID = 0; 230}