001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.annotations.J2ktIncompatible;
024import com.google.common.base.MoreObjects;
025import com.google.errorprone.annotations.CanIgnoreReturnValue;
026import com.google.errorprone.annotations.DoNotCall;
027import com.google.errorprone.annotations.concurrent.LazyInit;
028import com.google.j2objc.annotations.RetainedWith;
029import com.google.j2objc.annotations.Weak;
030import java.io.IOException;
031import java.io.InvalidObjectException;
032import java.io.ObjectInputStream;
033import java.io.ObjectOutputStream;
034import java.util.Arrays;
035import java.util.Collection;
036import java.util.Comparator;
037import java.util.Map;
038import java.util.Map.Entry;
039import java.util.function.Function;
040import java.util.stream.Collector;
041import java.util.stream.Stream;
042import javax.annotation.CheckForNull;
043import org.checkerframework.checker.nullness.qual.Nullable;
044
045/**
046 * A {@link SetMultimap} whose contents will never change, with many other important properties
047 * detailed at {@link ImmutableCollection}.
048 *
049 * <p><b>Warning:</b> As in all {@link SetMultimap}s, do not modify either a key <i>or a value</i>
050 * of a {@code ImmutableSetMultimap} in a way that affects its {@link Object#equals} behavior.
051 * Undefined behavior and bugs will result.
052 *
053 * <p>See the Guava User Guide article on <a href=
054 * "https://github.com/google/guava/wiki/ImmutableCollectionsExplained">immutable collections</a>.
055 *
056 * @author Mike Ward
057 * @since 2.0
058 */
059@GwtCompatible(serializable = true, emulated = true)
060@ElementTypesAreNonnullByDefault
061public class ImmutableSetMultimap<K, V> extends ImmutableMultimap<K, V>
062    implements SetMultimap<K, V> {
063  /**
064   * Returns a {@link Collector} that accumulates elements into an {@code ImmutableSetMultimap}
065   * whose keys and values are the result of applying the provided mapping functions to the input
066   * elements.
067   *
068   * <p>For streams with defined encounter order (as defined in the Ordering section of the {@link
069   * java.util.stream} Javadoc), that order is preserved, but entries are <a
070   * href="ImmutableMultimap.html#iteration">grouped by key</a>.
071   *
072   * <p>Example:
073   *
074   * <pre>{@code
075   * static final Multimap<Character, String> FIRST_LETTER_MULTIMAP =
076   *     Stream.of("banana", "apple", "carrot", "asparagus", "cherry")
077   *         .collect(toImmutableSetMultimap(str -> str.charAt(0), str -> str.substring(1)));
078   *
079   * // is equivalent to
080   *
081   * static final Multimap<Character, String> FIRST_LETTER_MULTIMAP =
082   *     new ImmutableSetMultimap.Builder<Character, String>()
083   *         .put('b', "anana")
084   *         .putAll('a', "pple", "sparagus")
085   *         .putAll('c', "arrot", "herry")
086   *         .build();
087   * }</pre>
088   *
089   * @since 21.0
090   */
091  public static <T extends @Nullable Object, K, V>
092      Collector<T, ?, ImmutableSetMultimap<K, V>> toImmutableSetMultimap(
093          Function<? super T, ? extends K> keyFunction,
094          Function<? super T, ? extends V> valueFunction) {
095    return CollectCollectors.toImmutableSetMultimap(keyFunction, valueFunction);
096  }
097
098  /**
099   * Returns a {@code Collector} accumulating entries into an {@code ImmutableSetMultimap}. Each
100   * input element is mapped to a key and a stream of values, each of which are put into the
101   * resulting {@code Multimap}, in the encounter order of the stream and the encounter order of the
102   * streams of values.
103   *
104   * <p>Example:
105   *
106   * <pre>{@code
107   * static final ImmutableSetMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
108   *     Stream.of("banana", "apple", "carrot", "asparagus", "cherry")
109   *         .collect(
110   *             flatteningToImmutableSetMultimap(
111   *                  str -> str.charAt(0),
112   *                  str -> str.substring(1).chars().mapToObj(c -> (char) c));
113   *
114   * // is equivalent to
115   *
116   * static final ImmutableSetMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
117   *     ImmutableSetMultimap.<Character, Character>builder()
118   *         .putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a'))
119   *         .putAll('a', Arrays.asList('p', 'p', 'l', 'e'))
120   *         .putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't'))
121   *         .putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's'))
122   *         .putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y'))
123   *         .build();
124   *
125   * // after deduplication, the resulting multimap is equivalent to
126   *
127   * static final ImmutableSetMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
128   *     ImmutableSetMultimap.<Character, Character>builder()
129   *         .putAll('b', Arrays.asList('a', 'n'))
130   *         .putAll('a', Arrays.asList('p', 'l', 'e', 's', 'a', 'r', 'g', 'u'))
131   *         .putAll('c', Arrays.asList('a', 'r', 'o', 't', 'h', 'e', 'y'))
132   *         .build();
133   * }
134   * }</pre>
135   *
136   * @since 21.0
137   */
138  public static <T extends @Nullable Object, K, V>
139      Collector<T, ?, ImmutableSetMultimap<K, V>> flatteningToImmutableSetMultimap(
140          Function<? super T, ? extends K> keyFunction,
141          Function<? super T, ? extends Stream<? extends V>> valuesFunction) {
142    return CollectCollectors.flatteningToImmutableSetMultimap(keyFunction, valuesFunction);
143  }
144
145  /**
146   * Returns the empty multimap.
147   *
148   * <p><b>Performance note:</b> the instance returned is a singleton.
149   */
150  // Casting is safe because the multimap will never hold any elements.
151  @SuppressWarnings("unchecked")
152  public static <K, V> ImmutableSetMultimap<K, V> of() {
153    return (ImmutableSetMultimap<K, V>) EmptyImmutableSetMultimap.INSTANCE;
154  }
155
156  /** Returns an immutable multimap containing a single entry. */
157  public static <K, V> ImmutableSetMultimap<K, V> of(K k1, V v1) {
158    ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder();
159    builder.put(k1, v1);
160    return builder.build();
161  }
162
163  /**
164   * Returns an immutable multimap containing the given entries, in order. Repeated occurrences of
165   * an entry (according to {@link Object#equals}) after the first are ignored.
166   */
167  public static <K, V> ImmutableSetMultimap<K, V> of(K k1, V v1, K k2, V v2) {
168    ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder();
169    builder.put(k1, v1);
170    builder.put(k2, v2);
171    return builder.build();
172  }
173
174  /**
175   * Returns an immutable multimap containing the given entries, in order. Repeated occurrences of
176   * an entry (according to {@link Object#equals}) after the first are ignored.
177   */
178  public static <K, V> ImmutableSetMultimap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
179    ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder();
180    builder.put(k1, v1);
181    builder.put(k2, v2);
182    builder.put(k3, v3);
183    return builder.build();
184  }
185
186  /**
187   * Returns an immutable multimap containing the given entries, in order. Repeated occurrences of
188   * an entry (according to {@link Object#equals}) after the first are ignored.
189   */
190  public static <K, V> ImmutableSetMultimap<K, V> of(
191      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
192    ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder();
193    builder.put(k1, v1);
194    builder.put(k2, v2);
195    builder.put(k3, v3);
196    builder.put(k4, v4);
197    return builder.build();
198  }
199
200  /**
201   * Returns an immutable multimap containing the given entries, in order. Repeated occurrences of
202   * an entry (according to {@link Object#equals}) after the first are ignored.
203   */
204  public static <K, V> ImmutableSetMultimap<K, V> of(
205      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
206    ImmutableSetMultimap.Builder<K, V> builder = ImmutableSetMultimap.builder();
207    builder.put(k1, v1);
208    builder.put(k2, v2);
209    builder.put(k3, v3);
210    builder.put(k4, v4);
211    builder.put(k5, v5);
212    return builder.build();
213  }
214
215  // looking for of() with > 5 entries? Use the builder instead.
216
217  /** Returns a new {@link Builder}. */
218  public static <K, V> Builder<K, V> builder() {
219    return new Builder<>();
220  }
221
222  /**
223   * A builder for creating immutable {@code SetMultimap} instances, especially {@code public static
224   * final} multimaps ("constant multimaps"). Example:
225   *
226   * <pre>{@code
227   * static final Multimap<String, Integer> STRING_TO_INTEGER_MULTIMAP =
228   *     new ImmutableSetMultimap.Builder<String, Integer>()
229   *         .put("one", 1)
230   *         .putAll("several", 1, 2, 3)
231   *         .putAll("many", 1, 2, 3, 4, 5)
232   *         .build();
233   * }</pre>
234   *
235   * <p>Builder instances can be reused; it is safe to call {@link #build} multiple times to build
236   * multiple multimaps in series. Each multimap contains the key-value mappings in the previously
237   * created multimaps.
238   *
239   * @since 2.0
240   */
241  public static final class Builder<K, V> extends ImmutableMultimap.Builder<K, V> {
242    /**
243     * Creates a new builder. The returned builder is equivalent to the builder generated by {@link
244     * ImmutableSetMultimap#builder}.
245     */
246    public Builder() {
247      super();
248    }
249
250    @Override
251    Collection<V> newMutableValueCollection() {
252      return Platform.preservesInsertionOrderOnAddsSet();
253    }
254
255    /** Adds a key-value mapping to the built multimap if it is not already present. */
256    @CanIgnoreReturnValue
257    @Override
258    public Builder<K, V> put(K key, V value) {
259      super.put(key, value);
260      return this;
261    }
262
263    /**
264     * Adds an entry to the built multimap if it is not already present.
265     *
266     * @since 11.0
267     */
268    @CanIgnoreReturnValue
269    @Override
270    public Builder<K, V> put(Entry<? extends K, ? extends V> entry) {
271      super.put(entry);
272      return this;
273    }
274
275    /**
276     * {@inheritDoc}
277     *
278     * @since 19.0
279     */
280    @CanIgnoreReturnValue
281    @Override
282    public Builder<K, V> putAll(Iterable<? extends Entry<? extends K, ? extends V>> entries) {
283      super.putAll(entries);
284      return this;
285    }
286
287    @CanIgnoreReturnValue
288    @Override
289    public Builder<K, V> putAll(K key, Iterable<? extends V> values) {
290      super.putAll(key, values);
291      return this;
292    }
293
294    @CanIgnoreReturnValue
295    @Override
296    public Builder<K, V> putAll(K key, V... values) {
297      return putAll(key, Arrays.asList(values));
298    }
299
300    @CanIgnoreReturnValue
301    @Override
302    public Builder<K, V> putAll(Multimap<? extends K, ? extends V> multimap) {
303      for (Entry<? extends K, ? extends Collection<? extends V>> entry :
304          multimap.asMap().entrySet()) {
305        putAll(entry.getKey(), entry.getValue());
306      }
307      return this;
308    }
309
310    @CanIgnoreReturnValue
311    @Override
312    Builder<K, V> combine(ImmutableMultimap.Builder<K, V> other) {
313      super.combine(other);
314      return this;
315    }
316
317    /**
318     * {@inheritDoc}
319     *
320     * @since 8.0
321     */
322    @CanIgnoreReturnValue
323    @Override
324    public Builder<K, V> orderKeysBy(Comparator<? super K> keyComparator) {
325      super.orderKeysBy(keyComparator);
326      return this;
327    }
328
329    /**
330     * Specifies the ordering of the generated multimap's values for each key.
331     *
332     * <p>If this method is called, the sets returned by the {@code get()} method of the generated
333     * multimap and its {@link Multimap#asMap()} view are {@link ImmutableSortedSet} instances.
334     * However, serialization does not preserve that property, though it does maintain the key and
335     * value ordering.
336     *
337     * @since 8.0
338     */
339    // TODO: Make serialization behavior consistent.
340    @CanIgnoreReturnValue
341    @Override
342    public Builder<K, V> orderValuesBy(Comparator<? super V> valueComparator) {
343      super.orderValuesBy(valueComparator);
344      return this;
345    }
346
347    /** Returns a newly-created immutable set multimap. */
348    @Override
349    public ImmutableSetMultimap<K, V> build() {
350      Collection<Map.Entry<K, Collection<V>>> mapEntries = builderMap.entrySet();
351      if (keyComparator != null) {
352        mapEntries = Ordering.from(keyComparator).<K>onKeys().immutableSortedCopy(mapEntries);
353      }
354      return fromMapEntries(mapEntries, valueComparator);
355    }
356  }
357
358  /**
359   * Returns an immutable set multimap containing the same mappings as {@code multimap}. The
360   * generated multimap's key and value orderings correspond to the iteration ordering of the {@code
361   * multimap.asMap()} view. Repeated occurrences of an entry in the multimap after the first are
362   * ignored.
363   *
364   * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
365   * safe to do so. The exact circumstances under which a copy will or will not be performed are
366   * undocumented and subject to change.
367   *
368   * @throws NullPointerException if any key or value in {@code multimap} is null
369   */
370  public static <K, V> ImmutableSetMultimap<K, V> copyOf(
371      Multimap<? extends K, ? extends V> multimap) {
372    return copyOf(multimap, null);
373  }
374
375  private static <K, V> ImmutableSetMultimap<K, V> copyOf(
376      Multimap<? extends K, ? extends V> multimap,
377      @CheckForNull Comparator<? super V> valueComparator) {
378    checkNotNull(multimap); // eager for GWT
379    if (multimap.isEmpty() && valueComparator == null) {
380      return of();
381    }
382
383    if (multimap instanceof ImmutableSetMultimap) {
384      @SuppressWarnings("unchecked") // safe since multimap is not writable
385      ImmutableSetMultimap<K, V> kvMultimap = (ImmutableSetMultimap<K, V>) multimap;
386      if (!kvMultimap.isPartialView()) {
387        return kvMultimap;
388      }
389    }
390
391    return fromMapEntries(multimap.asMap().entrySet(), valueComparator);
392  }
393
394  /**
395   * Returns an immutable multimap containing the specified entries. The returned multimap iterates
396   * over keys in the order they were first encountered in the input, and the values for each key
397   * are iterated in the order they were encountered. If two values for the same key are {@linkplain
398   * Object#equals equal}, the first value encountered is used.
399   *
400   * @throws NullPointerException if any key, value, or entry is null
401   * @since 19.0
402   */
403  public static <K, V> ImmutableSetMultimap<K, V> copyOf(
404      Iterable<? extends Entry<? extends K, ? extends V>> entries) {
405    return new Builder<K, V>().putAll(entries).build();
406  }
407
408  /** Creates an ImmutableSetMultimap from an asMap.entrySet. */
409  static <K, V> ImmutableSetMultimap<K, V> fromMapEntries(
410      Collection<? extends Map.Entry<? extends K, ? extends Collection<? extends V>>> mapEntries,
411      @CheckForNull Comparator<? super V> valueComparator) {
412    if (mapEntries.isEmpty()) {
413      return of();
414    }
415    ImmutableMap.Builder<K, ImmutableSet<V>> builder =
416        new ImmutableMap.Builder<>(mapEntries.size());
417    int size = 0;
418
419    for (Entry<? extends K, ? extends Collection<? extends V>> entry : mapEntries) {
420      K key = entry.getKey();
421      Collection<? extends V> values = entry.getValue();
422      ImmutableSet<V> set = valueSet(valueComparator, values);
423      if (!set.isEmpty()) {
424        builder.put(key, set);
425        size += set.size();
426      }
427    }
428
429    return new ImmutableSetMultimap<>(builder.buildOrThrow(), size, valueComparator);
430  }
431
432  /**
433   * Returned by get() when a missing key is provided. Also holds the comparator, if any, used for
434   * values.
435   */
436  private final transient ImmutableSet<V> emptySet;
437
438  ImmutableSetMultimap(
439      ImmutableMap<K, ImmutableSet<V>> map,
440      int size,
441      @CheckForNull Comparator<? super V> valueComparator) {
442    super(map, size);
443    this.emptySet = emptySet(valueComparator);
444  }
445
446  // views
447
448  /**
449   * Returns an immutable set of the values for the given key. If no mappings in the multimap have
450   * the provided key, an empty immutable set is returned. The values are in the same order as the
451   * parameters used to build this multimap.
452   */
453  @Override
454  public ImmutableSet<V> get(K key) {
455    // This cast is safe as its type is known in constructor.
456    ImmutableSet<V> set = (ImmutableSet<V>) map.get(key);
457    return MoreObjects.firstNonNull(set, emptySet);
458  }
459
460  @LazyInit @RetainedWith @CheckForNull private transient ImmutableSetMultimap<V, K> inverse;
461
462  /**
463   * {@inheritDoc}
464   *
465   * <p>Because an inverse of a set multimap cannot contain multiple pairs with the same key and
466   * value, this method returns an {@code ImmutableSetMultimap} rather than the {@code
467   * ImmutableMultimap} specified in the {@code ImmutableMultimap} class.
468   */
469  @Override
470  public ImmutableSetMultimap<V, K> inverse() {
471    ImmutableSetMultimap<V, K> result = inverse;
472    return (result == null) ? (inverse = invert()) : result;
473  }
474
475  private ImmutableSetMultimap<V, K> invert() {
476    Builder<V, K> builder = builder();
477    for (Entry<K, V> entry : entries()) {
478      builder.put(entry.getValue(), entry.getKey());
479    }
480    ImmutableSetMultimap<V, K> invertedMultimap = builder.build();
481    invertedMultimap.inverse = this;
482    return invertedMultimap;
483  }
484
485  /**
486   * Guaranteed to throw an exception and leave the multimap unmodified.
487   *
488   * @throws UnsupportedOperationException always
489   * @deprecated Unsupported operation.
490   */
491  @CanIgnoreReturnValue
492  @Deprecated
493  @Override
494  @DoNotCall("Always throws UnsupportedOperationException")
495  public final ImmutableSet<V> removeAll(@CheckForNull Object key) {
496    throw new UnsupportedOperationException();
497  }
498
499  /**
500   * Guaranteed to throw an exception and leave the multimap unmodified.
501   *
502   * @throws UnsupportedOperationException always
503   * @deprecated Unsupported operation.
504   */
505  @CanIgnoreReturnValue
506  @Deprecated
507  @Override
508  @DoNotCall("Always throws UnsupportedOperationException")
509  public final ImmutableSet<V> replaceValues(K key, Iterable<? extends V> values) {
510    throw new UnsupportedOperationException();
511  }
512
513  @LazyInit @RetainedWith @CheckForNull private transient ImmutableSet<Entry<K, V>> entries;
514
515  /**
516   * Returns an immutable collection of all key-value pairs in the multimap. Its iterator traverses
517   * the values for the first key, the values for the second key, and so on.
518   */
519  @Override
520  public ImmutableSet<Entry<K, V>> entries() {
521    ImmutableSet<Entry<K, V>> result = entries;
522    return result == null ? (entries = new EntrySet<>(this)) : result;
523  }
524
525  private static final class EntrySet<K, V> extends ImmutableSet<Entry<K, V>> {
526    @Weak private final transient ImmutableSetMultimap<K, V> multimap;
527
528    EntrySet(ImmutableSetMultimap<K, V> multimap) {
529      this.multimap = multimap;
530    }
531
532    @Override
533    public boolean contains(@CheckForNull Object object) {
534      if (object instanceof Entry) {
535        Entry<?, ?> entry = (Entry<?, ?>) object;
536        return multimap.containsEntry(entry.getKey(), entry.getValue());
537      }
538      return false;
539    }
540
541    @Override
542    public int size() {
543      return multimap.size();
544    }
545
546    @Override
547    public UnmodifiableIterator<Entry<K, V>> iterator() {
548      return multimap.entryIterator();
549    }
550
551    @Override
552    boolean isPartialView() {
553      return false;
554    }
555  }
556
557  private static <V> ImmutableSet<V> valueSet(
558      @CheckForNull Comparator<? super V> valueComparator, Collection<? extends V> values) {
559    return (valueComparator == null)
560        ? ImmutableSet.copyOf(values)
561        : ImmutableSortedSet.copyOf(valueComparator, values);
562  }
563
564  private static <V> ImmutableSet<V> emptySet(@CheckForNull Comparator<? super V> valueComparator) {
565    return (valueComparator == null)
566        ? ImmutableSet.<V>of()
567        : ImmutableSortedSet.<V>emptySet(valueComparator);
568  }
569
570  private static <V> ImmutableSet.Builder<V> valuesBuilder(
571      @CheckForNull Comparator<? super V> valueComparator) {
572    return (valueComparator == null)
573        ? new ImmutableSet.Builder<V>()
574        : new ImmutableSortedSet.Builder<V>(valueComparator);
575  }
576
577  /**
578   * @serialData number of distinct keys, and then for each distinct key: the key, the number of
579   *     values for that key, and the key's values
580   */
581  @GwtIncompatible // java.io.ObjectOutputStream
582  @J2ktIncompatible
583  private void writeObject(ObjectOutputStream stream) throws IOException {
584    stream.defaultWriteObject();
585    stream.writeObject(valueComparator());
586    Serialization.writeMultimap(this, stream);
587  }
588
589  @CheckForNull
590  Comparator<? super V> valueComparator() {
591    return emptySet instanceof ImmutableSortedSet
592        ? ((ImmutableSortedSet<V>) emptySet).comparator()
593        : null;
594  }
595
596  @GwtIncompatible // java serialization
597  @J2ktIncompatible
598  private static final class SetFieldSettersHolder {
599    static final Serialization.FieldSetter<ImmutableSetMultimap> EMPTY_SET_FIELD_SETTER =
600        Serialization.getFieldSetter(ImmutableSetMultimap.class, "emptySet");
601  }
602
603  @GwtIncompatible // java.io.ObjectInputStream
604  @J2ktIncompatible
605  // Serialization type safety is at the caller's mercy.
606  @SuppressWarnings("unchecked")
607  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
608    stream.defaultReadObject();
609    Comparator<Object> valueComparator = (Comparator<Object>) stream.readObject();
610    int keyCount = stream.readInt();
611    if (keyCount < 0) {
612      throw new InvalidObjectException("Invalid key count " + keyCount);
613    }
614    ImmutableMap.Builder<Object, ImmutableSet<Object>> builder = ImmutableMap.builder();
615    int tmpSize = 0;
616
617    for (int i = 0; i < keyCount; i++) {
618      Object key = stream.readObject();
619      int valueCount = stream.readInt();
620      if (valueCount <= 0) {
621        throw new InvalidObjectException("Invalid value count " + valueCount);
622      }
623
624      ImmutableSet.Builder<Object> valuesBuilder = valuesBuilder(valueComparator);
625      for (int j = 0; j < valueCount; j++) {
626        valuesBuilder.add(stream.readObject());
627      }
628      ImmutableSet<Object> valueSet = valuesBuilder.build();
629      if (valueSet.size() != valueCount) {
630        throw new InvalidObjectException("Duplicate key-value pairs exist for key " + key);
631      }
632      builder.put(key, valueSet);
633      tmpSize += valueCount;
634    }
635
636    ImmutableMap<Object, ImmutableSet<Object>> tmpMap;
637    try {
638      tmpMap = builder.buildOrThrow();
639    } catch (IllegalArgumentException e) {
640      throw (InvalidObjectException) new InvalidObjectException(e.getMessage()).initCause(e);
641    }
642
643    FieldSettersHolder.MAP_FIELD_SETTER.set(this, tmpMap);
644    FieldSettersHolder.SIZE_FIELD_SETTER.set(this, tmpSize);
645    SetFieldSettersHolder.EMPTY_SET_FIELD_SETTER.set(this, emptySet(valueComparator));
646  }
647
648  @GwtIncompatible // not needed in emulated source.
649  @J2ktIncompatible
650  private static final long serialVersionUID = 0;
651}