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 Bringertimport com.google.common.annotations.GwtIncompatible; 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable; 26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays; 27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection; 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Collections; 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator; 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator; 31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.LinkedHashMap; 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.List; 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Map.Entry; 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.TreeMap; 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable; 37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * An immutable {@link Multimap}. Does not permit null keys or values. 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Unlike {@link Multimaps#unmodifiableMultimap(Multimap)}, which is 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a <i>view</i> of a separate multimap which can still change, an instance of 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code ImmutableMultimap} contains its own data and will <i>never</i> 44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * change. {@code ImmutableMultimap} is convenient for 45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code public static final} multimaps ("constant multimaps") and also lets 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you easily make a "defensive copy" of a multimap provided to your class by 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a caller. 48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Note:</b> Although this class is not final, it cannot be subclassed as 50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * it has no public or protected constructors. Thus, instances of this class 51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are guaranteed to be immutable. 52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>In addition to methods defined by {@link Multimap}, an {@link #inverse} 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * method is also supported. 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true) 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert// TODO(user): If BiMultimap graduates from labs, this class should implement it. 61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic abstract class ImmutableMultimap<K, V> 62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson implements Multimap<K, V>, Serializable { 63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** Returns an empty multimap. */ 65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of() { 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(); 67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing a single entry. 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of(K k1, V v1) { 73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1); 74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of(K k1, V v1, K k2, V v2) { 80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1, k2, v2); 81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of( 87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K k1, V v1, K k2, V v2, K k3, V v3) { 88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1, k2, v2, k3, v3); 89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of( 95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { 96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1, k2, v2, k3, v3, k4, v4); 97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> of( 103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { 104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ImmutableListMultimap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); 105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // looking for of() with > 5 entries? Use the builder instead. 108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a new builder. The generated builder is equivalent to the builder 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * created by the {@link Builder} constructor. 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> Builder<K, V> builder() { 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new Builder<K, V>(); 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Multimap for {@link ImmutableMultimap.Builder} that maintains key and 119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * value orderings, allows duplicate values, and performs better than 120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link LinkedListMultimap}. 121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class BuilderMultimap<K, V> extends AbstractMultimap<K, V> { 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson BuilderMultimap() { 124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson super(new LinkedHashMap<K, Collection<V>>()); 125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override Collection<V> createCollection() { 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Lists.newArrayList(); 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Multimap for {@link ImmutableMultimap.Builder} that sorts key and allows 1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * duplicate values, 1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class SortedKeyBuilderMultimap<K, V> 1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert extends AbstractMultimap<K, V> { 1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert SortedKeyBuilderMultimap( 1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Comparator<? super K> keyComparator, Multimap<K, V> multimap) { 1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert super(new TreeMap<K, Collection<V>>(keyComparator)); 1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert putAll(multimap); 1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override Collection<V> createCollection() { 1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Lists.newArrayList(); 1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final long serialVersionUID = 0; 1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * A builder for creating immutable multimap instances, especially 151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code public static final} multimaps ("constant multimaps"). Example: 152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <pre> {@code 153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * static final Multimap<String, Integer> STRING_TO_INTEGER_MULTIMAP = 155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * new ImmutableMultimap.Builder<String, Integer>() 156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .put("one", 1) 157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .putAll("several", 1, 2, 3) 158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .putAll("many", 1, 2, 3, 4, 5) 159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .build();}</pre> 160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Builder instances can be reused; it is safe to call {@link #build} multiple 1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * times to build multiple multimaps in series. Each multimap contains the 1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * key-value mappings in the previously created multimaps. 1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static class Builder<K, V> { 1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Multimap<K, V> builderMultimap = new BuilderMultimap<K, V>(); 1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Comparator<? super V> valueComparator; 170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Creates a new builder. The returned builder is equivalent to the builder 173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * generated by {@link ImmutableMultimap#builder}. 174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder() {} 176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adds a key-value mapping to the built multimap. 179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder<K, V> put(K key, V value) { 181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builderMultimap.put(checkNotNull(key), checkNotNull(value)); 182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this; 183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Adds an entry to the built multimap. 1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11.0 1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Builder<K, V> put(Entry<? extends K, ? extends V> entry) { 1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builderMultimap.put( 1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(entry.getKey()), checkNotNull(entry.getValue())); 1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this; 1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Stores a collection of values with the same key in the built multimap. 198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if {@code key}, {@code values}, or any 200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * element in {@code values} is null. The builder is left in an invalid 201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * state. 202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder<K, V> putAll(K key, Iterable<? extends V> values) { 204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<V> valueList = builderMultimap.get(checkNotNull(key)); 205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (V value : values) { 206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson valueList.add(checkNotNull(value)); 207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this; 209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Stores an array of values with the same key in the built multimap. 213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if the key or any value is null. The builder 215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * is left in an invalid state. 216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder<K, V> putAll(K key, V... values) { 218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return putAll(key, Arrays.asList(values)); 219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Stores another multimap's entries in the built multimap. The generated 223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * multimap's key and value orderings correspond to the iteration ordering 224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of the {@code multimap.asMap()} view, with new keys and values following 225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * any existing keys and values. 226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if any key or value in {@code multimap} is 228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * null. The builder is left in an invalid state. 229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Builder<K, V> putAll(Multimap<? extends K, ? extends V> multimap) { 2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Entry<? extends K, ? extends Collection<? extends V>> entry 232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : multimap.asMap().entrySet()) { 233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson putAll(entry.getKey(), entry.getValue()); 234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this; 236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Specifies the ordering of the generated multimap's keys. 2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 8.0 2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Builder<K, V> orderKeysBy(Comparator<? super K> keyComparator) { 2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builderMultimap = new SortedKeyBuilderMultimap<K, V>( 2461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(keyComparator), builderMultimap); 2471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this; 2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Specifies the ordering of the generated multimap's values for each key. 2521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 2531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 8.0 2541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Builder<K, V> orderValuesBy(Comparator<? super V> valueComparator) { 2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.valueComparator = checkNotNull(valueComparator); 2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this; 2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a newly-created immutable multimap. 263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableMultimap<K, V> build() { 2651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (valueComparator != null) { 2661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Collection<V> values : builderMultimap.asMap().values()) { 2671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert List<V> list = (List <V>) values; 2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Collections.sort(list, valueComparator); 2691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return copyOf(builderMultimap); 272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 2761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an immutable multimap containing the same mappings as {@code 2771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * multimap}. The generated multimap's key and value orderings correspond to 2781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the iteration ordering of the {@code multimap.asMap()} view. 279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 2801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Despite the method name, this method attempts to avoid actually copying 2811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the data when it is safe to do so. The exact circumstances under which a 2821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * copy will or will not be performed are undocumented and subject to change. 283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if any key or value in {@code multimap} is 2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * null 286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableMultimap<K, V> copyOf( 288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Multimap<? extends K, ? extends V> multimap) { 289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (multimap instanceof ImmutableMultimap) { 290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // safe since multimap is not writable 291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultimap<K, V> kvMultimap 292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson = (ImmutableMultimap<K, V>) multimap; 2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (!kvMultimap.isPartialView()) { 2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return kvMultimap; 2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ImmutableListMultimap.copyOf(multimap); 298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final transient ImmutableMap<K, ? extends ImmutableCollection<V>> map; 301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final transient int size; 302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // These constants allow the deserialization code to set final fields. This 304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // holder class makes sure they are not initialized unless an instance is 305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // deserialized. 3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java serialization is not supported") 307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static class FieldSettersHolder { 308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static final Serialization.FieldSetter<ImmutableMultimap> 309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson MAP_FIELD_SETTER = Serialization.getFieldSetter( 310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultimap.class, "map"); 311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static final Serialization.FieldSetter<ImmutableMultimap> 312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson SIZE_FIELD_SETTER = Serialization.getFieldSetter( 313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultimap.class, "size"); 314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultimap(ImmutableMap<K, ? extends ImmutableCollection<V>> map, 317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int size) { 318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.map = map; 319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.size = size; 320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // mutators (not supported) 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> removeAll(Object key) { 331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableCollection<V> replaceValues(K key, 341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends V> values) { 342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public void clear() { 352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable collection of the values for the given key. If no 357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * mappings in the multimap have the provided key, an empty immutable 358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * collection is returned. The values are in the same order as the parameters 359090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * used to build this multimap. 360090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public abstract ImmutableCollection<V> get(K key); 363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 3651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an immutable multimap which is the inverse of this one. For every 3661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * key-value mapping in the original, the result will have a mapping with 3671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * key and value reversed. 3681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 3691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11 3701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 3711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 3721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public abstract ImmutableMultimap<V, K> inverse(); 3731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 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 put(K key, V value) { 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(K key, Iterable<? extends V> values) { 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 putAll(Multimap<? extends K, ? extends V> multimap) { 401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 405090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 406090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 407090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 4091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean remove(Object key, Object value) { 411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 413090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert boolean isPartialView(){ 4151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return map.isPartialView(); 4161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // accessors 419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 421090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean containsEntry(@Nullable Object key, @Nullable Object value) { 422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<V> values = map.get(key); 423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return values != null && values.contains(value); 424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 425090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 427090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean containsKey(@Nullable Object key) { 428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.containsKey(key); 429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean containsValue(@Nullable Object value) { 433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (Collection<V> valueCollection : map.values()) { 434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (valueCollection.contains(value)) { 435090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 436090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 437090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean isEmpty() { 443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return size == 0; 444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 447090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public int size() { 448090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return size; 449090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 450090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 451090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public boolean equals(@Nullable Object object) { 452090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (object instanceof Multimap) { 453090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Multimap<?, ?> that = (Multimap<?, ?>) object; 454090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this.map.equals(that.asMap()); 455090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 456090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 457090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 458090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 459090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 460090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.hashCode(); 461090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 462090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 463090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 464090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.toString(); 465090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 466090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 467090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // views 468090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 469090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 470090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable set of the distinct keys in this multimap. These keys 471090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are ordered according to when they first appeared during the construction 472090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of this multimap. 473090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 4741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 475090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableSet<K> keySet() { 476090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.keySet(); 477090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 478090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 479090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 480090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable map that associates each key with its corresponding 481090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * values in the multimap. 482090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 4831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 484090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // a widening cast 485090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableMap<K, Collection<V>> asMap() { 486090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (ImmutableMap) map; 487090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 488090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private transient ImmutableCollection<Entry<K, V>> entries; 490090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 491090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 492090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable collection of all key-value pairs in the multimap. Its 493090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator traverses the values for the first key, the values for the second 494090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * key, and so on. 495090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 4961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 4971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public ImmutableCollection<Entry<K, V>> entries() { 4981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableCollection<Entry<K, V>> result = entries; 499090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (result == null) 500090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? (entries = new EntryCollection<K, V>(this)) : result; 501090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 502090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 503090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class EntryCollection<K, V> 5041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert extends ImmutableCollection<Entry<K, V>> { 505090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final ImmutableMultimap<K, V> multimap; 506090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 507090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson EntryCollection(ImmutableMultimap<K, V> multimap) { 508090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.multimap = multimap; 509090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 510090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public UnmodifiableIterator<Entry<K, V>> iterator() { 5121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterator<? extends Entry<K, ? extends ImmutableCollection<V>>> 513090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson mapIterator = this.multimap.map.entrySet().iterator(); 514090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new UnmodifiableIterator<Entry<K, V>>() { 516090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K key; 517090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<V> valueIterator; 518090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 520090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 521090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (key != null && valueIterator.hasNext()) 522090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson || mapIterator.hasNext(); 523090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 524090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 5261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Entry<K, V> next() { 527090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (key == null || !valueIterator.hasNext()) { 5281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Entry<K, ? extends ImmutableCollection<V>> entry 529090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson = mapIterator.next(); 530090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson key = entry.getKey(); 531090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson valueIterator = entry.getValue().iterator(); 532090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 533090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Maps.immutableEntry(key, valueIterator.next()); 534090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 535090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 536090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 537090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override boolean isPartialView() { 5391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return multimap.isPartialView(); 5401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 543090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public int size() { 544090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return multimap.size(); 545090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 546090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 547090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public boolean contains(Object object) { 5481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (object instanceof Entry) { 5491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Entry<?, ?> entry = (Entry<?, ?>) object; 550090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return multimap.containsEntry(entry.getKey(), entry.getValue()); 551090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 552090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 553090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 554090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 555090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 556090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 557090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 558090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private transient ImmutableMultiset<K> keys; 559090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 560090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 561090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a collection, which may contain duplicates, of all keys. The number 562090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of times a key appears in the returned multiset equals the number of 563090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * mappings the key has in the multimap. Duplicate keys appear consecutively 564090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * in the multiset's iteration order. 565090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 5661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 567090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableMultiset<K> keys() { 568090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultiset<K> result = keys; 569090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (result == null) ? (keys = createKeys()) : result; 570090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 571090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 572090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private ImmutableMultiset<K> createKeys() { 573090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMultiset.Builder<K> builder = ImmutableMultiset.builder(); 5741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Entry<K, ? extends ImmutableCollection<V>> entry 575090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : map.entrySet()) { 576090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.addCopies(entry.getKey(), entry.getValue().size()); 577090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 578090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return builder.build(); 579090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 580090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 581090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private transient ImmutableCollection<V> values; 582090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 583090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 584090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable collection of the values in this multimap. Its 585090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator traverses the values for the first key, the values for the second 586090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * key, and so on. 587090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 5881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 589090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ImmutableCollection<V> values() { 590090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableCollection<V> result = values; 591090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (result == null) ? (values = new Values<V>(this)) : result; 592090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 593090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 594090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class Values<V> extends ImmutableCollection<V> { 5951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final ImmutableMultimap<?, V> multimap; 596090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Values(ImmutableMultimap<?, V> multimap) { 598090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.multimap = multimap; 599090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 600090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 601090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public UnmodifiableIterator<V> iterator() { 6021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterator<? extends Entry<?, V>> entryIterator 603090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson = multimap.entries().iterator(); 604090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new UnmodifiableIterator<V>() { 6051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 606090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 607090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return entryIterator.hasNext(); 608090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 6091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 610090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public V next() { 611090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return entryIterator.next().getValue(); 612090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 613090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 614090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 615090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 6161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 617090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public int size() { 618090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return multimap.size(); 619090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 620090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 6211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override boolean isPartialView() { 6221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return true; 6231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 625090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 626090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 627090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 628090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 629090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 630