1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2008 The Guava Authors 3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License. 6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at 7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software 11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and 14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License. 15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect; 18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkNotNull; 20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible; 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable; 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays; 26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection; 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Collections; 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator; 29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator; 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.LinkedHashMap; 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.List; 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Map.Entry; 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.TreeMap; 34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable; 36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * An immutable {@link Multimap}. Does not permit null keys or values. 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Unlike {@link Multimaps#unmodifiableMultimap(Multimap)}, which is 41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a <i>view</i> of a separate multimap which can still change, an instance of 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code ImmutableMultimap} contains its own data and will <i>never</i> 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * change. {@code ImmutableMultimap} is convenient for 44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code public static final} multimaps ("constant multimaps") and also lets 45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you easily make a "defensive copy" of a multimap provided to your class by 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a caller. 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Note:</b> Although this class is not final, it cannot be subclassed as 49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * it has no public or protected constructors. Thus, instances of this class 50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are guaranteed to be immutable. 51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>In addition to methods defined by {@link Multimap}, an {@link #inverse} 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * method is also supported. 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true) 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert// TODO(user): If BiMultimap graduates from labs, this class should implement it. 60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic abstract class ImmutableMultimap<K, V> 61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson implements Multimap<K, V>, Serializable { 62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** Returns an empty multimap. */ 64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of() { 65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(); 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing a single entry. 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of(K k1, V v1) { 72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1); 73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of(K k1, V v1, K k2, V v2) { 79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1, k2, v2); 80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of( 86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K k1, V v1, K k2, V v2, K k3, V v3) { 87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1, k2, v2, k3, v3); 88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of( 94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { 95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1, k2, v2, k3, v3, k4, v4); 96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of( 102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { 103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); 104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // looking for of() with > 5 entries? Use the builder instead. 107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a new builder. The generated builder is equivalent to the builder 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * created by the {@link Builder} constructor. 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> Builder<K, V> builder() { 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new Builder<K, V>(); 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Multimap for {@link ImmutableMultimap.Builder} that maintains key and 118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * value orderings, allows duplicate values, and performs better than 119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link LinkedListMultimap}. 120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class BuilderMultimap<K, V> extends AbstractMultimap<K, V> { 122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson BuilderMultimap() { 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson super(new LinkedHashMap<K, Collection<V>>()); 124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override Collection<V> createCollection() { 126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Lists.newArrayList(); 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Multimap for {@link ImmutableMultimap.Builder} that sorts key and allows 1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * duplicate values, 1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class SortedKeyBuilderMultimap<K, V> 1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert extends AbstractMultimap<K, V> { 1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert SortedKeyBuilderMultimap( 1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Comparator<? super K> keyComparator, Multimap<K, V> multimap) { 1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert super(new TreeMap<K, Collection<V>>(keyComparator)); 1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert putAll(multimap); 1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override Collection<V> createCollection() { 1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Lists.newArrayList(); 1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final long serialVersionUID = 0; 1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * A builder for creating immutable multimap instances, especially 150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code public static final} multimaps ("constant multimaps"). Example: 151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <pre> {@code 152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * static final Multimap<String, Integer> STRING_TO_INTEGER_MULTIMAP = 154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * new ImmutableMultimap.Builder<String, Integer>() 155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .put("one", 1) 156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .putAll("several", 1, 2, 3) 157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .putAll("many", 1, 2, 3, 4, 5) 158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .build();}</pre> 159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Builder instances can be reused; it is safe to call {@link #build} multiple 1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * times to build multiple multimaps in series. Each multimap contains the 1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * key-value mappings in the previously created multimaps. 1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static class Builder<K, V> { 1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Multimap<K, V> builderMultimap = new BuilderMultimap<K, V>(); 1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Comparator<? super V> valueComparator; 169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Creates a new builder. The returned builder is equivalent to the builder 172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * generated by {@link ImmutableMultimap#builder}. 173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder() {} 175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adds a key-value mapping to the built multimap. 178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder<K, V> put(K key, V value) { 180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builderMultimap.put(checkNotNull(key), checkNotNull(value)); 181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this; 182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Adds an entry to the built multimap. 1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11.0 1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Builder<K, V> put(Entry<? extends K, ? extends V> entry) { 1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builderMultimap.put( 1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(entry.getKey()), checkNotNull(entry.getValue())); 1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this; 1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Stores a collection of values with the same key in the built multimap. 197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if {@code key}, {@code values}, or any 199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * element in {@code values} is null. The builder is left in an invalid 200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * state. 201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder<K, V> putAll(K key, Iterable<? extends V> values) { 203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<V> valueList = builderMultimap.get(checkNotNull(key)); 204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (V value : values) { 205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson valueList.add(checkNotNull(value)); 206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this; 208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Stores an array of values with the same key in the built multimap. 212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if the key or any value is null. The builder 214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * is left in an invalid state. 215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder<K, V> putAll(K key, V... values) { 217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return putAll(key, Arrays.asList(values)); 218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Stores another multimap's entries in the built multimap. The generated 222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * multimap's key and value orderings correspond to the iteration ordering 223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of the {@code multimap.asMap()} view, with new keys and values following 224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * any existing keys and values. 225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if any key or value in {@code multimap} is 227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * null. The builder is left in an invalid state. 228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder<K, V> putAll(Multimap<? extends K, ? extends V> multimap) { 2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Entry<? extends K, ? extends Collection<? extends V>> entry 231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : multimap.asMap().entrySet()) { 232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson putAll(entry.getKey(), entry.getValue()); 233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this; 235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Specifies the ordering of the generated multimap's keys. 2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 8.0 2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Builder<K, V> orderKeysBy(Comparator<? super K> keyComparator) { 2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builderMultimap = new SortedKeyBuilderMultimap<K, V>( 2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(keyComparator), builderMultimap); 2461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this; 2471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Specifies the ordering of the generated multimap's values for each key. 2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 2521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 8.0 2531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 2541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Builder<K, V> orderValuesBy(Comparator<? super V> valueComparator) { 2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.valueComparator = checkNotNull(valueComparator); 2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this; 2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a newly-created immutable multimap. 262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableMultimap<K, V> build() { 2641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (valueComparator != null) { 2651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Collection<V> values : builderMultimap.asMap().values()) { 2661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert List<V> list = (List <V>) values; 2671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Collections.sort(list, valueComparator); 2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return copyOf(builderMultimap); 271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 2751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an immutable multimap containing the same mappings as {@code 2761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * multimap}. The generated multimap's key and value orderings correspond to 2771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the iteration ordering of the {@code multimap.asMap()} view. 278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 2791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Despite the method name, this method attempts to avoid actually copying 2801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the data when it is safe to do so. The exact circumstances under which a 2811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * copy will or will not be performed are undocumented and subject to change. 282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if any key or value in {@code multimap} is 2841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * null 285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> copyOf( 287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Multimap<? extends K, ? extends V> multimap) { 288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (multimap instanceof ImmutableMultimap) { 289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // safe since multimap is not writable 290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultimap<K, V> kvMultimap 291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson = (ImmutableMultimap<K, V>) multimap; 2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (!kvMultimap.isPartialView()) { 2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return kvMultimap; 2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ImmutableListMultimap.copyOf(multimap); 297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final transient ImmutableMap<K, ? extends ImmutableCollection<V>> map; 300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final transient int size; 301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // These constants allow the deserialization code to set final fields. This 303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // holder class makes sure they are not initialized unless an instance is 304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // deserialized. 305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultimap(ImmutableMap<K, ? extends ImmutableCollection<V>> map, 307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int size) { 308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.map = map; 309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.size = size; 310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // mutators (not supported) 313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableCollection<V> removeAll(Object key) { 321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableCollection<V> replaceValues(K key, 331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends V> values) { 332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 339090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public void clear() { 342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable collection of the values for the given key. If no 347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * mappings in the multimap have the provided key, an empty immutable 348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * collection is returned. The values are in the same order as the parameters 349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * used to build this multimap. 350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public abstract ImmutableCollection<V> get(K key); 353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 3551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an immutable multimap which is the inverse of this one. For every 3561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * key-value mapping in the original, the result will have a mapping with 3571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * key and value reversed. 3581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 3591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11 3601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 3611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 3621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public abstract ImmutableMultimap<V, K> inverse(); 3631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 365090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 367090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean put(K key, V value) { 371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 372090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 375090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 378090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean putAll(K key, Iterable<? extends V> values) { 381090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 384090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 385090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 386090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 387090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 388090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean putAll(Multimap<? extends K, ? extends V> multimap) { 391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 395090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 398090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean remove(Object key, Object value) { 401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert boolean isPartialView(){ 4051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return map.isPartialView(); 4061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // accessors 409090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean containsEntry(@Nullable Object key, @Nullable Object value) { 412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<V> values = map.get(key); 413090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return values != null && values.contains(value); 414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 415090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 417090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean containsKey(@Nullable Object key) { 418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.containsKey(key); 419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean containsValue(@Nullable Object value) { 423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (Collection<V> valueCollection : map.values()) { 424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (valueCollection.contains(value)) { 425090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 426090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 427090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean isEmpty() { 433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return size == 0; 434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 435090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 437090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public int size() { 438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return size; 439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 441090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public boolean equals(@Nullable Object object) { 442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (object instanceof Multimap) { 443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Multimap<?, ?> that = (Multimap<?, ?>) object; 444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this.map.equals(that.asMap()); 445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 446090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 447090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 448090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 449090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 450090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.hashCode(); 451090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 452090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 453090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 454090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.toString(); 455090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 456090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 457090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // views 458090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 459090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 460090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable set of the distinct keys in this multimap. These keys 461090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are ordered according to when they first appeared during the construction 462090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of this multimap. 463090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 4641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 465090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableSet<K> keySet() { 466090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.keySet(); 467090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 468090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 469090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 470090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable map that associates each key with its corresponding 471090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * values in the multimap. 472090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 4731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 474090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // a widening cast 475090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableMap<K, Collection<V>> asMap() { 476090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (ImmutableMap) map; 477090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 478090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private transient ImmutableCollection<Entry<K, V>> entries; 480090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 481090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 482090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable collection of all key-value pairs in the multimap. Its 483090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator traverses the values for the first key, the values for the second 484090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * key, and so on. 485090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 4861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 4871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public ImmutableCollection<Entry<K, V>> entries() { 4881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableCollection<Entry<K, V>> result = entries; 489090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (result == null) 490090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? (entries = new EntryCollection<K, V>(this)) : result; 491090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 492090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 493090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class EntryCollection<K, V> 4941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert extends ImmutableCollection<Entry<K, V>> { 495090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final ImmutableMultimap<K, V> multimap; 496090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 497090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson EntryCollection(ImmutableMultimap<K, V> multimap) { 498090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.multimap = multimap; 499090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 500090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public UnmodifiableIterator<Entry<K, V>> iterator() { 5021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterator<? extends Entry<K, ? extends ImmutableCollection<V>>> 503090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson mapIterator = this.multimap.map.entrySet().iterator(); 504090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new UnmodifiableIterator<Entry<K, V>>() { 506090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K key; 507090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<V> valueIterator; 508090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 510090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 511090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (key != null && valueIterator.hasNext()) 512090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson || mapIterator.hasNext(); 513090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 514090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 5161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Entry<K, V> next() { 517090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (key == null || !valueIterator.hasNext()) { 5181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Entry<K, ? extends ImmutableCollection<V>> entry 519090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson = mapIterator.next(); 520090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson key = entry.getKey(); 521090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson valueIterator = entry.getValue().iterator(); 522090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 523090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Maps.immutableEntry(key, valueIterator.next()); 524090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 525090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 526090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 527090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override boolean isPartialView() { 5291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return multimap.isPartialView(); 5301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 533090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public int size() { 534090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return multimap.size(); 535090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 536090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 537090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public boolean contains(Object object) { 5381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (object instanceof Entry) { 5391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Entry<?, ?> entry = (Entry<?, ?>) object; 540090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return multimap.containsEntry(entry.getKey(), entry.getValue()); 541090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 542090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 543090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 544090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 545090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 546090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 547090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 548090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private transient ImmutableMultiset<K> keys; 549090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 550090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 551090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a collection, which may contain duplicates, of all keys. The number 552090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of times a key appears in the returned multiset equals the number of 553090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * mappings the key has in the multimap. Duplicate keys appear consecutively 554090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * in the multiset's iteration order. 555090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 5561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 557090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableMultiset<K> keys() { 558090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultiset<K> result = keys; 559090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (result == null) ? (keys = createKeys()) : result; 560090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 561090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 562090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private ImmutableMultiset<K> createKeys() { 563090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultiset.Builder<K> builder = ImmutableMultiset.builder(); 5641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Entry<K, ? extends ImmutableCollection<V>> entry 565090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : map.entrySet()) { 566090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.addCopies(entry.getKey(), entry.getValue().size()); 567090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 568090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return builder.build(); 569090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 570090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 571090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private transient ImmutableCollection<V> values; 572090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 573090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 574090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable collection of the values in this multimap. Its 575090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator traverses the values for the first key, the values for the second 576090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * key, and so on. 577090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 5781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 579090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableCollection<V> values() { 580090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableCollection<V> result = values; 581090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (result == null) ? (values = new Values<V>(this)) : result; 582090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 583090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 584090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class Values<V> extends ImmutableCollection<V> { 5851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final ImmutableMultimap<?, V> multimap; 586090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Values(ImmutableMultimap<?, V> multimap) { 588090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.multimap = multimap; 589090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 590090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 591090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public UnmodifiableIterator<V> iterator() { 5921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterator<? extends Entry<?, V>> entryIterator 593090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson = multimap.entries().iterator(); 594090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new UnmodifiableIterator<V>() { 5951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 596090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 597090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return entryIterator.hasNext(); 598090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 5991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 600090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public V next() { 601090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return entryIterator.next().getValue(); 602090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 603090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 604090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 605090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 6061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 607090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public int size() { 608090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return multimap.size(); 609090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 610090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 6111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override boolean isPartialView() { 6121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return true; 6131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 615090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 616090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 617090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 618090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 619090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 620