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