1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 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.util.Arrays; 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection; 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator; 27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.LinkedHashMap; 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Map.Entry; 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.TreeMap; 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable; 32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** 34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * An immutable {@link SetMultimap} with reliable user-specified key and value 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iteration order. Does not permit null keys or values. 36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Unlike {@link Multimaps#unmodifiableSetMultimap(SetMultimap)}, which is 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a <i>view</i> of a separate multimap which can still change, an instance of 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code ImmutableSetMultimap} contains its own data and will <i>never</i> 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * change. {@code ImmutableSetMultimap} is convenient for 41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code public static final} multimaps ("constant multimaps") and also lets 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you easily make a "defensive copy" of a multimap provided to your class by 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a caller. 44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Note:</b> Although this class is not final, it cannot be subclassed as 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * it has no public or protected constructors. Thus, instances of this class 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are guaranteed to be immutable. 48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Mike Ward 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(serializable = true, emulated = true) 53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic class ImmutableSetMultimap<K, V> 54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson extends ImmutableMultimap<K, V> 55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson implements SetMultimap<K, V> { 56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** Returns the empty multimap. */ 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Casting is safe because the multimap will never hold any elements. 59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableSetMultimap<K, V> of() { 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (ImmutableSetMultimap<K, V>) EmptyImmutableSetMultimap.INSTANCE; 62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing a single entry. 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableSetMultimap<K, V> of(K k1, V v1) { 68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder(); 69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k1, v1); 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return builder.build(); 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Repeated occurrences of an entry (according to {@link Object#equals}) after 76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the first are ignored. 77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableSetMultimap<K, V> of(K k1, V v1, K k2, V v2) { 79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder(); 80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k1, v1); 81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k2, v2); 82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return builder.build(); 83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Repeated occurrences of an entry (according to {@link Object#equals}) after 88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the first are ignored. 89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableSetMultimap<K, V> of( 91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K k1, V v1, K k2, V v2, K k3, V v3) { 92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder(); 93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k1, v1); 94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k2, v2); 95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k3, v3); 96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return builder.build(); 97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Repeated occurrences of an entry (according to {@link Object#equals}) after 102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the first are ignored. 103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableSetMultimap<K, V> of( 105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { 106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder(); 107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k1, v1); 108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k2, v2); 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k3, v3); 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k4, v4); 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return builder.build(); 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable multimap containing the given entries, in order. 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Repeated occurrences of an entry (according to {@link Object#equals}) after 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the first are ignored. 118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableSetMultimap<K, V> of( 120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { 121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder(); 122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k1, v1); 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k2, v2); 124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k3, v3); 125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k4, v4); 126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(k5, v5); 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return builder.build(); 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // looking for of() with > 5 entries? Use the builder instead. 131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a new {@link Builder}. 134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> Builder<K, V> builder() { 136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new Builder<K, V>(); 137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Multimap for {@link ImmutableSetMultimap.Builder} that maintains key 141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * and value orderings and performs better than {@link LinkedHashMultimap}. 142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class BuilderMultimap<K, V> extends AbstractMultimap<K, V> { 144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson BuilderMultimap() { 145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson super(new LinkedHashMap<K, Collection<V>>()); 146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override Collection<V> createCollection() { 148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Sets.newLinkedHashSet(); 149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Multimap for {@link ImmutableSetMultimap.Builder} that sorts keys and 1551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * maintains value orderings. 1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class SortedKeyBuilderMultimap<K, V> 1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert extends AbstractMultimap<K, V> { 1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert SortedKeyBuilderMultimap( 1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Comparator<? super K> keyComparator, Multimap<K, V> multimap) { 1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert super(new TreeMap<K, Collection<V>>(keyComparator)); 1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert putAll(multimap); 1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override Collection<V> createCollection() { 1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Sets.newLinkedHashSet(); 1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final long serialVersionUID = 0; 1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * A builder for creating immutable {@code SetMultimap} instances, especially 172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code public static final} multimaps ("constant multimaps"). Example: 173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <pre> {@code 174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * static final Multimap<String, Integer> STRING_TO_INTEGER_MULTIMAP = 176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * new ImmutableSetMultimap.Builder<String, Integer>() 177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .put("one", 1) 178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .putAll("several", 1, 2, 3) 179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .putAll("many", 1, 2, 3, 4, 5) 180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * .build();}</pre> 181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Builder instances can be reused; it is safe to call {@link #build} multiple 1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * times to build multiple multimaps in series. Each multimap contains the 1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * key-value mappings in the previously created multimaps. 1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static final class Builder<K, V> 189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson extends ImmutableMultimap.Builder<K, V> { 190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Creates a new builder. The returned builder is equivalent to the builder 192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * generated by {@link ImmutableSetMultimap#builder}. 193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Builder() { 1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builderMultimap = new BuilderMultimap<K, V>(); 1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adds a key-value mapping to the built multimap if it is not already 200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * present. 201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public Builder<K, V> put(K key, V value) { 203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builderMultimap.put(checkNotNull(key), checkNotNull(value)); 204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this; 205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Adds an entry to the built multimap if it is not already present. 209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11.0 211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public Builder<K, V> put(Entry<? extends K, ? extends V> entry) { 2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builderMultimap.put( 2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(entry.getKey()), checkNotNull(entry.getValue())); 2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this; 2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public Builder<K, V> putAll(K key, Iterable<? extends V> values) { 219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<V> collection = builderMultimap.get(checkNotNull(key)); 220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (V value : values) { 221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson collection.add(checkNotNull(value)); 222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this; 224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public Builder<K, V> putAll(K key, V... values) { 227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return putAll(key, Arrays.asList(values)); 228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public Builder<K, V> putAll( 231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Multimap<? extends K, ? extends V> multimap) { 2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Entry<? extends K, ? extends Collection<? extends V>> entry 233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : multimap.asMap().entrySet()) { 234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson putAll(entry.getKey(), entry.getValue()); 235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return this; 237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@inheritDoc} 2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 8.0 2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta @Override 2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Builder<K, V> orderKeysBy(Comparator<? super K> keyComparator) { 2461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builderMultimap = new SortedKeyBuilderMultimap<K, V>( 2471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(keyComparator), builderMultimap); 2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this; 2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 2521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Specifies the ordering of the generated multimap's values for each key. 2531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 2541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>If this method is called, the sets returned by the {@code get()} 2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * method of the generated multimap and its {@link Multimap#asMap()} view 2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * are {@link ImmutableSortedSet} instances. However, serialization does not 2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * preserve that property, though it does maintain the key and value 2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * ordering. 2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 8.0 2611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 2621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO: Make serialization behavior consistent. 2631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta @Override 2641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Builder<K, V> orderValuesBy(Comparator<? super V> valueComparator) { 2651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert super.orderValuesBy(valueComparator); 2661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this; 2671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a newly-created immutable set multimap. 271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public ImmutableSetMultimap<K, V> build() { 2731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return copyOf(builderMultimap, valueComparator); 274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable set multimap containing the same mappings as 279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code multimap}. The generated multimap's key and value orderings 280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * correspond to the iteration ordering of the {@code multimap.asMap()} view. 281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Repeated occurrences of an entry in the multimap after the first are 282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * ignored. 283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 2841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Despite the method name, this method attempts to avoid actually copying 2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the data when it is safe to do so. The exact circumstances under which a 2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * copy will or will not be performed are undocumented and subject to change. 287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if any key or value in {@code multimap} is 289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * null 290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> ImmutableSetMultimap<K, V> copyOf( 292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Multimap<? extends K, ? extends V> multimap) { 2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return copyOf(multimap, null); 2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <K, V> ImmutableSetMultimap<K, V> copyOf( 2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Multimap<? extends K, ? extends V> multimap, 2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Comparator<? super V> valueComparator) { 2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(multimap); // eager for GWT 3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (multimap.isEmpty() && valueComparator == null) { 301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return of(); 302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (multimap instanceof ImmutableSetMultimap) { 305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // safe since multimap is not writable 306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableSetMultimap<K, V> kvMultimap 307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson = (ImmutableSetMultimap<K, V>) multimap; 3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (!kvMultimap.isPartialView()) { 3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return kvMultimap; 3101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableMap.Builder<K, ImmutableSet<V>> builder = ImmutableMap.builder(); 314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int size = 0; 315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 3161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Entry<? extends K, ? extends Collection<? extends V>> entry 317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : multimap.asMap().entrySet()) { 318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K key = entry.getKey(); 319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<? extends V> values = entry.getValue(); 3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableSet<V> set = (valueComparator == null) 3211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ? ImmutableSet.copyOf(values) 3221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert : ImmutableSortedSet.copyOf(valueComparator, values); 323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!set.isEmpty()) { 324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.put(key, set); 325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson size += set.size(); 326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new ImmutableSetMultimap<K, V>( 3301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builder.build(), size, valueComparator); 331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 3331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Returned by get() when values are sorted and a missing key is provided. 3341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final transient ImmutableSortedSet<V> emptySet; 3351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableSetMultimap(ImmutableMap<K, ImmutableSet<V>> map, int size, 3371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Nullable Comparator<? super V> valueComparator) { 338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson super(map, size); 3391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.emptySet = (valueComparator == null) 3401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ? null : ImmutableSortedSet.<V>emptySet(valueComparator); 341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // views 344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable set of the values for the given key. If no mappings 347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * in the multimap have the provided key, an empty immutable set is returned. 348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * The values are in the same order as the parameters used to build this 349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * multimap. 350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public ImmutableSet<V> get(@Nullable K key) { 352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // This cast is safe as its type is known in constructor. 353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ImmutableSet<V> set = (ImmutableSet<V>) map.get(key); 3541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (set != null) { 3551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return set; 3561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } else if (emptySet != null) { 3571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return emptySet; 3581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } else { 3591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ImmutableSet.<V>of(); 3601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private transient ImmutableSetMultimap<V, K> inverse; 3641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 3661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@inheritDoc} 3671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 3681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Because an inverse of a set multimap cannot contain multiple pairs with the same key and 3691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * value, this method returns an {@code ImmutableSetMultimap} rather than the 3701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code ImmutableMultimap} specified in the {@code ImmutableMultimap} class. 3711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 3721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11 3731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 3741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 3751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public ImmutableSetMultimap<V, K> inverse() { 3761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableSetMultimap<V, K> result = inverse; 3771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (result == null) ? (inverse = invert()) : result; 3781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private ImmutableSetMultimap<V, K> invert() { 3811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Builder<V, K> builder = builder(); 3821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Entry<K, V> entry : entries()) { 3831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builder.put(entry.getValue(), entry.getKey()); 3841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableSetMultimap<V, K> invertedMultimap = builder.build(); 3861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert invertedMultimap.inverse = this; 3871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return invertedMultimap; 388090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 389090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 395090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public ImmutableSet<V> removeAll(Object key) { 396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 398090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 399090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Guaranteed to throw an exception and leave the multimap unmodified. 401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException always 403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public ImmutableSet<V> replaceValues( 405090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson K key, Iterable<? extends V> values) { 406090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new UnsupportedOperationException(); 407090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private transient ImmutableSet<Entry<K, V>> entries; 410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an immutable collection of all key-value pairs in the multimap. 413090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Its iterator traverses the values for the first key, the values for the 414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * second key, and so on. 415090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 4161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(kevinb): Fix this so that two copies of the entries are not created. 4171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public ImmutableSet<Entry<K, V>> entries() { 4181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableSet<Entry<K, V>> result = entries; 419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (result == null) 420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? (entries = ImmutableSet.copyOf(super.entries())) 421090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : result; 422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 4241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 425