001/*
002 * Copyright (C) 2013 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;
020import static com.google.common.collect.CollectPreconditions.checkNonnegative;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.base.Supplier;
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.Comparator;
028import java.util.EnumMap;
029import java.util.EnumSet;
030import java.util.LinkedList;
031import java.util.List;
032import java.util.Map;
033import java.util.Set;
034import java.util.SortedSet;
035import java.util.TreeMap;
036import java.util.TreeSet;
037import org.jspecify.annotations.Nullable;
038
039/**
040 * An immutable builder for {@link Multimap} instances, letting you independently select the desired
041 * behaviors (for example, ordering) of the backing map and value-collections. Example:
042 *
043 * <pre>{@code
044 * ListMultimap<UserId, ErrorResponse> errorsByUser =
045 *     MultimapBuilder.linkedHashKeys().arrayListValues().build();
046 * SortedSetMultimap<String, Method> methodsForName =
047 *     MultimapBuilder.treeKeys().treeSetValues(this::compareMethods).build();
048 * }</pre>
049 *
050 * <p>{@code MultimapBuilder} instances are immutable. Invoking a configuration method has no effect
051 * on the receiving instance; you must store and use the new builder instance it returns instead.
052 *
053 * <p>The generated multimaps are serializable if the key and value types are serializable, unless
054 * stated otherwise in one of the configuration methods.
055 *
056 * @author Louis Wasserman
057 * @param <K0> An upper bound on the key type of the generated multimap.
058 * @param <V0> An upper bound on the value type of the generated multimap.
059 * @since 16.0
060 */
061@GwtCompatible
062public abstract class MultimapBuilder<K0 extends @Nullable Object, V0 extends @Nullable Object> {
063  /*
064   * Leaving K and V as upper bounds rather than the actual key and value types allows type
065   * parameters to be left implicit more often. CacheBuilder uses the same technique.
066   */
067
068  private MultimapBuilder() {}
069
070  private static final int DEFAULT_EXPECTED_KEYS = 8;
071
072  /** Uses a hash table to map keys to value collections. */
073  public static MultimapBuilderWithKeys<@Nullable Object> hashKeys() {
074    return hashKeys(DEFAULT_EXPECTED_KEYS);
075  }
076
077  /**
078   * Uses a hash table to map keys to value collections, initialized to expect the specified number
079   * of keys.
080   *
081   * @throws IllegalArgumentException if {@code expectedKeys < 0}
082   */
083  public static MultimapBuilderWithKeys<@Nullable Object> hashKeys(int expectedKeys) {
084    checkNonnegative(expectedKeys, "expectedKeys");
085    return new MultimapBuilderWithKeys<@Nullable Object>() {
086      @Override
087      <K extends @Nullable Object, V extends @Nullable Object> Map<K, Collection<V>> createMap() {
088        return Platform.newHashMapWithExpectedSize(expectedKeys);
089      }
090    };
091  }
092
093  /**
094   * Uses a hash table to map keys to value collections.
095   *
096   * <p>The collections returned by {@link Multimap#keySet()}, {@link Multimap#keys()}, and {@link
097   * Multimap#asMap()} will iterate through the keys in the order that they were first added to the
098   * multimap, save that if all values associated with a key are removed and then the key is added
099   * back into the multimap, that key will come last in the key iteration order.
100   */
101  public static MultimapBuilderWithKeys<@Nullable Object> linkedHashKeys() {
102    return linkedHashKeys(DEFAULT_EXPECTED_KEYS);
103  }
104
105  /**
106   * Uses an hash table to map keys to value collections, initialized to expect the specified number
107   * of keys.
108   *
109   * <p>The collections returned by {@link Multimap#keySet()}, {@link Multimap#keys()}, and {@link
110   * Multimap#asMap()} will iterate through the keys in the order that they were first added to the
111   * multimap, save that if all values associated with a key are removed and then the key is added
112   * back into the multimap, that key will come last in the key iteration order.
113   */
114  public static MultimapBuilderWithKeys<@Nullable Object> linkedHashKeys(int expectedKeys) {
115    checkNonnegative(expectedKeys, "expectedKeys");
116    return new MultimapBuilderWithKeys<@Nullable Object>() {
117      @Override
118      <K extends @Nullable Object, V extends @Nullable Object> Map<K, Collection<V>> createMap() {
119        return Platform.newLinkedHashMapWithExpectedSize(expectedKeys);
120      }
121    };
122  }
123
124  /**
125   * Uses a naturally-ordered {@link TreeMap} to map keys to value collections.
126   *
127   * <p>The collections returned by {@link Multimap#keySet()}, {@link Multimap#keys()}, and {@link
128   * Multimap#asMap()} will iterate through the keys in sorted order.
129   *
130   * <p>For all multimaps generated by the resulting builder, the {@link Multimap#keySet()} can be
131   * safely cast to a {@link java.util.SortedSet}, and the {@link Multimap#asMap()} can safely be
132   * cast to a {@link java.util.SortedMap}.
133   */
134  @SuppressWarnings("rawtypes")
135  public static MultimapBuilderWithKeys<Comparable> treeKeys() {
136    return treeKeys(Ordering.natural());
137  }
138
139  /**
140   * Uses a {@link TreeMap} sorted by the specified comparator to map keys to value collections.
141   *
142   * <p>The collections returned by {@link Multimap#keySet()}, {@link Multimap#keys()}, and {@link
143   * Multimap#asMap()} will iterate through the keys in sorted order.
144   *
145   * <p>For all multimaps generated by the resulting builder, the {@link Multimap#keySet()} can be
146   * safely cast to a {@link java.util.SortedSet}, and the {@link Multimap#asMap()} can safely be
147   * cast to a {@link java.util.SortedMap}.
148   *
149   * <p>Multimaps generated by the resulting builder will not be serializable if {@code comparator}
150   * is not serializable.
151   */
152  public static <K0 extends @Nullable Object> MultimapBuilderWithKeys<K0> treeKeys(
153      Comparator<K0> comparator) {
154    checkNotNull(comparator);
155    return new MultimapBuilderWithKeys<K0>() {
156      @Override
157      <K extends K0, V extends @Nullable Object> Map<K, Collection<V>> createMap() {
158        return new TreeMap<>(comparator);
159      }
160    };
161  }
162
163  /**
164   * Uses an {@link EnumMap} to map keys to value collections.
165   *
166   * @since 16.0
167   */
168  public static <K0 extends Enum<K0>> MultimapBuilderWithKeys<K0> enumKeys(Class<K0> keyClass) {
169    checkNotNull(keyClass);
170    return new MultimapBuilderWithKeys<K0>() {
171      @SuppressWarnings("unchecked")
172      @Override
173      <K extends K0, V extends @Nullable Object> Map<K, Collection<V>> createMap() {
174        // K must actually be K0, since enums are effectively final
175        // (their subclasses are inaccessible)
176        return (Map<K, Collection<V>>) new EnumMap<K0, Collection<V>>(keyClass);
177      }
178    };
179  }
180
181  private static final class ArrayListSupplier<V extends @Nullable Object>
182      implements Supplier<List<V>>, Serializable {
183    private final int expectedValuesPerKey;
184
185    ArrayListSupplier(int expectedValuesPerKey) {
186      this.expectedValuesPerKey = checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
187    }
188
189    @Override
190    public List<V> get() {
191      return new ArrayList<>(expectedValuesPerKey);
192    }
193  }
194
195  private enum LinkedListSupplier implements Supplier<List<?>> {
196    INSTANCE;
197
198    public static <V extends @Nullable Object> Supplier<List<V>> instance() {
199      // Each call generates a fresh LinkedList, which can serve as a List<V> for any V.
200      @SuppressWarnings({"rawtypes", "unchecked"})
201      Supplier<List<V>> result = (Supplier) INSTANCE;
202      return result;
203    }
204
205    @Override
206    // We recommend against linkedListValues but need to keep it for compatibility.
207    @SuppressWarnings("JdkObsolete")
208    public List<?> get() {
209      return new LinkedList<>();
210    }
211  }
212
213  private static final class HashSetSupplier<V extends @Nullable Object>
214      implements Supplier<Set<V>>, Serializable {
215    private final int expectedValuesPerKey;
216
217    HashSetSupplier(int expectedValuesPerKey) {
218      this.expectedValuesPerKey = checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
219    }
220
221    @Override
222    public Set<V> get() {
223      return Platform.newHashSetWithExpectedSize(expectedValuesPerKey);
224    }
225  }
226
227  private static final class LinkedHashSetSupplier<V extends @Nullable Object>
228      implements Supplier<Set<V>>, Serializable {
229    private final int expectedValuesPerKey;
230
231    LinkedHashSetSupplier(int expectedValuesPerKey) {
232      this.expectedValuesPerKey = checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
233    }
234
235    @Override
236    public Set<V> get() {
237      return Platform.newLinkedHashSetWithExpectedSize(expectedValuesPerKey);
238    }
239  }
240
241  private static final class TreeSetSupplier<V extends @Nullable Object>
242      implements Supplier<SortedSet<V>>, Serializable {
243    private final Comparator<? super V> comparator;
244
245    TreeSetSupplier(Comparator<? super V> comparator) {
246      this.comparator = checkNotNull(comparator);
247    }
248
249    @Override
250    public SortedSet<V> get() {
251      return new TreeSet<>(comparator);
252    }
253  }
254
255  private static final class EnumSetSupplier<V extends Enum<V>>
256      implements Supplier<Set<V>>, Serializable {
257    private final Class<V> clazz;
258
259    EnumSetSupplier(Class<V> clazz) {
260      this.clazz = checkNotNull(clazz);
261    }
262
263    @Override
264    public Set<V> get() {
265      return EnumSet.noneOf(clazz);
266    }
267  }
268
269  /**
270   * An intermediate stage in a {@link MultimapBuilder} in which the key-value collection map
271   * implementation has been specified, but the value collection implementation has not.
272   *
273   * @param <K0> The upper bound on the key type of the generated multimap.
274   * @since 16.0
275   */
276  public abstract static class MultimapBuilderWithKeys<K0 extends @Nullable Object> {
277
278    private static final int DEFAULT_EXPECTED_VALUES_PER_KEY = 2;
279
280    MultimapBuilderWithKeys() {}
281
282    abstract <K extends K0, V extends @Nullable Object> Map<K, Collection<V>> createMap();
283
284    /** Uses an {@link ArrayList} to store value collections. */
285    public ListMultimapBuilder<K0, @Nullable Object> arrayListValues() {
286      return arrayListValues(DEFAULT_EXPECTED_VALUES_PER_KEY);
287    }
288
289    /**
290     * Uses an {@link ArrayList} to store value collections, initialized to expect the specified
291     * number of values per key.
292     *
293     * @throws IllegalArgumentException if {@code expectedValuesPerKey < 0}
294     */
295    public ListMultimapBuilder<K0, @Nullable Object> arrayListValues(int expectedValuesPerKey) {
296      checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
297      return new ListMultimapBuilder<K0, @Nullable Object>() {
298        @Override
299        public <K extends K0, V extends @Nullable Object> ListMultimap<K, V> build() {
300          return Multimaps.newListMultimap(
301              MultimapBuilderWithKeys.this.<K, V>createMap(),
302              new ArrayListSupplier<V>(expectedValuesPerKey));
303        }
304      };
305    }
306
307    /**
308     * Uses a {@link LinkedList} to store value collections.
309     *
310     * <p><b>Performance note:</b> {@link ArrayList} and {@link java.util.ArrayDeque} consistently
311     * outperform {@code LinkedList} except in certain rare and specific situations. Unless you have
312     * spent a lot of time benchmarking your specific needs, use one of those instead. (However, we
313     * do not currently offer a {@link Multimap} implementation based on {@link
314     * java.util.ArrayDeque}.)
315     */
316    public ListMultimapBuilder<K0, @Nullable Object> linkedListValues() {
317      return new ListMultimapBuilder<K0, @Nullable Object>() {
318        @Override
319        public <K extends K0, V extends @Nullable Object> ListMultimap<K, V> build() {
320          return Multimaps.newListMultimap(
321              MultimapBuilderWithKeys.this.<K, V>createMap(), LinkedListSupplier.<V>instance());
322        }
323      };
324    }
325
326    /** Uses a hash-based {@code Set} to store value collections. */
327    public SetMultimapBuilder<K0, @Nullable Object> hashSetValues() {
328      return hashSetValues(DEFAULT_EXPECTED_VALUES_PER_KEY);
329    }
330
331    /**
332     * Uses a hash-based {@code Set} to store value collections, initialized to expect the specified
333     * number of values per key.
334     *
335     * @throws IllegalArgumentException if {@code expectedValuesPerKey < 0}
336     */
337    public SetMultimapBuilder<K0, @Nullable Object> hashSetValues(int expectedValuesPerKey) {
338      checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
339      return new SetMultimapBuilder<K0, @Nullable Object>() {
340        @Override
341        public <K extends K0, V extends @Nullable Object> SetMultimap<K, V> build() {
342          return Multimaps.newSetMultimap(
343              MultimapBuilderWithKeys.this.<K, V>createMap(),
344              new HashSetSupplier<V>(expectedValuesPerKey));
345        }
346      };
347    }
348
349    /** Uses an insertion-ordered hash-based {@code Set} to store value collections. */
350    public SetMultimapBuilder<K0, @Nullable Object> linkedHashSetValues() {
351      return linkedHashSetValues(DEFAULT_EXPECTED_VALUES_PER_KEY);
352    }
353
354    /**
355     * Uses an insertion-ordered hash-based {@code Set} to store value collections, initialized to
356     * expect the specified number of values per key.
357     *
358     * @throws IllegalArgumentException if {@code expectedValuesPerKey < 0}
359     */
360    public SetMultimapBuilder<K0, @Nullable Object> linkedHashSetValues(int expectedValuesPerKey) {
361      checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
362      return new SetMultimapBuilder<K0, @Nullable Object>() {
363        @Override
364        public <K extends K0, V extends @Nullable Object> SetMultimap<K, V> build() {
365          return Multimaps.newSetMultimap(
366              MultimapBuilderWithKeys.this.<K, V>createMap(),
367              new LinkedHashSetSupplier<V>(expectedValuesPerKey));
368        }
369      };
370    }
371
372    /** Uses a naturally-ordered {@link TreeSet} to store value collections. */
373    @SuppressWarnings("rawtypes")
374    public SortedSetMultimapBuilder<K0, Comparable> treeSetValues() {
375      return treeSetValues(Ordering.natural());
376    }
377
378    /**
379     * Uses a {@link TreeSet} ordered by the specified comparator to store value collections.
380     *
381     * <p>Multimaps generated by the resulting builder will not be serializable if {@code
382     * comparator} is not serializable.
383     */
384    public <V0 extends @Nullable Object> SortedSetMultimapBuilder<K0, V0> treeSetValues(
385        Comparator<V0> comparator) {
386      checkNotNull(comparator, "comparator");
387      return new SortedSetMultimapBuilder<K0, V0>() {
388        @Override
389        public <K extends K0, V extends V0> SortedSetMultimap<K, V> build() {
390          return Multimaps.newSortedSetMultimap(
391              MultimapBuilderWithKeys.this.<K, V>createMap(), new TreeSetSupplier<V>(comparator));
392        }
393      };
394    }
395
396    /** Uses an {@link EnumSet} to store value collections. */
397    public <V0 extends Enum<V0>> SetMultimapBuilder<K0, V0> enumSetValues(Class<V0> valueClass) {
398      checkNotNull(valueClass, "valueClass");
399      return new SetMultimapBuilder<K0, V0>() {
400        @Override
401        public <K extends K0, V extends V0> SetMultimap<K, V> build() {
402          // V must actually be V0, since enums are effectively final
403          // (their subclasses are inaccessible)
404          @SuppressWarnings({"unchecked", "rawtypes"})
405          Supplier<Set<V>> factory = (Supplier) new EnumSetSupplier<V0>(valueClass);
406          return Multimaps.newSetMultimap(MultimapBuilderWithKeys.this.<K, V>createMap(), factory);
407        }
408      };
409    }
410  }
411
412  /** Returns a new, empty {@code Multimap} with the specified implementation. */
413  public abstract <K extends K0, V extends V0> Multimap<K, V> build();
414
415  /**
416   * Returns a {@code Multimap} with the specified implementation, initialized with the entries of
417   * {@code multimap}.
418   */
419  public <K extends K0, V extends V0> Multimap<K, V> build(
420      Multimap<? extends K, ? extends V> multimap) {
421    Multimap<K, V> result = build();
422    result.putAll(multimap);
423    return result;
424  }
425
426  /**
427   * A specialization of {@link MultimapBuilder} that generates {@link ListMultimap} instances.
428   *
429   * @since 16.0
430   */
431  public abstract static class ListMultimapBuilder<
432          K0 extends @Nullable Object, V0 extends @Nullable Object>
433      extends MultimapBuilder<K0, V0> {
434    ListMultimapBuilder() {}
435
436    @Override
437    public abstract <K extends K0, V extends V0> ListMultimap<K, V> build();
438
439    @Override
440    public <K extends K0, V extends V0> ListMultimap<K, V> build(
441        Multimap<? extends K, ? extends V> multimap) {
442      return (ListMultimap<K, V>) super.<K, V>build(multimap);
443    }
444  }
445
446  /**
447   * A specialization of {@link MultimapBuilder} that generates {@link SetMultimap} instances.
448   *
449   * @since 16.0
450   */
451  public abstract static class SetMultimapBuilder<
452          K0 extends @Nullable Object, V0 extends @Nullable Object>
453      extends MultimapBuilder<K0, V0> {
454    SetMultimapBuilder() {}
455
456    @Override
457    public abstract <K extends K0, V extends V0> SetMultimap<K, V> build();
458
459    @Override
460    public <K extends K0, V extends V0> SetMultimap<K, V> build(
461        Multimap<? extends K, ? extends V> multimap) {
462      return (SetMultimap<K, V>) super.<K, V>build(multimap);
463    }
464  }
465
466  /**
467   * A specialization of {@link MultimapBuilder} that generates {@link SortedSetMultimap} instances.
468   *
469   * @since 16.0
470   */
471  public abstract static class SortedSetMultimapBuilder<
472          K0 extends @Nullable Object, V0 extends @Nullable Object>
473      extends SetMultimapBuilder<K0, V0> {
474    SortedSetMultimapBuilder() {}
475
476    @Override
477    public abstract <K extends K0, V extends V0> SortedSetMultimap<K, V> build();
478
479    @Override
480    public <K extends K0, V extends V0> SortedSetMultimap<K, V> build(
481        Multimap<? extends K, ? extends V> multimap) {
482      return (SortedSetMultimap<K, V>) super.<K, V>build(multimap);
483    }
484  }
485}