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