001/*
002 * Copyright (C) 2007 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.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021import static com.google.common.collect.CollectPreconditions.checkNonnegative;
022import static com.google.common.collect.ObjectArrays.checkElementNotNull;
023
024import com.google.common.annotations.Beta;
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.annotations.VisibleForTesting;
027import com.google.common.primitives.Ints;
028import com.google.errorprone.annotations.CanIgnoreReturnValue;
029import com.google.errorprone.annotations.concurrent.LazyInit;
030import com.google.j2objc.annotations.RetainedWith;
031import java.io.Serializable;
032import java.util.Arrays;
033import java.util.Collection;
034import java.util.Collections;
035import java.util.EnumSet;
036import java.util.Iterator;
037import java.util.Set;
038import java.util.SortedSet;
039import java.util.Spliterator;
040import java.util.function.Consumer;
041import java.util.stream.Collector;
042import org.checkerframework.checker.nullness.compatqual.NullableDecl;
043
044/**
045 * A {@link Set} whose contents will never change, with many other important properties detailed at
046 * {@link ImmutableCollection}.
047 *
048 * @since 2.0
049 */
050@GwtCompatible(serializable = true, emulated = true)
051@SuppressWarnings("serial") // we're overriding default serialization
052public abstract class ImmutableSet<E> extends ImmutableCollection<E> implements Set<E> {
053  static final int SPLITERATOR_CHARACTERISTICS =
054      ImmutableCollection.SPLITERATOR_CHARACTERISTICS | Spliterator.DISTINCT;
055
056  /**
057   * Returns a {@code Collector} that accumulates the input elements into a new {@code
058   * ImmutableSet}. Elements appear in the resulting set in the encounter order of the stream; if
059   * the stream contains duplicates (according to {@link Object#equals(Object)}), only the first
060   * duplicate in encounter order will appear in the result.
061   *
062   * @since 21.0
063   */
064  @Beta
065  public static <E> Collector<E, ?, ImmutableSet<E>> toImmutableSet() {
066    return CollectCollectors.toImmutableSet();
067  }
068
069  /**
070   * Returns the empty immutable set. Preferred over {@link Collections#emptySet} for code
071   * consistency, and because the return type conveys the immutability guarantee.
072   */
073  @SuppressWarnings({"unchecked"}) // fully variant implementation (never actually produces any Es)
074  public static <E> ImmutableSet<E> of() {
075    return (ImmutableSet<E>) RegularImmutableSet.EMPTY;
076  }
077
078  /**
079   * Returns an immutable set containing {@code element}. Preferred over {@link
080   * Collections#singleton} for code consistency, {@code null} rejection, and because the return
081   * type conveys the immutability guarantee.
082   */
083  public static <E> ImmutableSet<E> of(E element) {
084    return new SingletonImmutableSet<E>(element);
085  }
086
087  /**
088   * Returns an immutable set containing the given elements, minus duplicates, in the order each was
089   * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
090   * the first are ignored.
091   */
092  public static <E> ImmutableSet<E> of(E e1, E e2) {
093    return construct(2, e1, e2);
094  }
095
096  /**
097   * Returns an immutable set containing the given elements, minus duplicates, in the order each was
098   * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
099   * the first are ignored.
100   */
101  public static <E> ImmutableSet<E> of(E e1, E e2, E e3) {
102    return construct(3, e1, e2, e3);
103  }
104
105  /**
106   * Returns an immutable set containing the given elements, minus duplicates, in the order each was
107   * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
108   * the first are ignored.
109   */
110  public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4) {
111    return construct(4, e1, e2, e3, e4);
112  }
113
114  /**
115   * Returns an immutable set containing the given elements, minus duplicates, in the order each was
116   * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
117   * the first are ignored.
118   */
119  public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5) {
120    return construct(5, e1, e2, e3, e4, e5);
121  }
122
123  /**
124   * Returns an immutable set containing the given elements, minus duplicates, in the order each was
125   * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
126   * the first are ignored.
127   *
128   * @since 3.0 (source-compatible since 2.0)
129   */
130  @SafeVarargs // For Eclipse. For internal javac we have disabled this pointless type of warning.
131  public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... others) {
132    final int paramCount = 6;
133    Object[] elements = new Object[paramCount + others.length];
134    elements[0] = e1;
135    elements[1] = e2;
136    elements[2] = e3;
137    elements[3] = e4;
138    elements[4] = e5;
139    elements[5] = e6;
140    System.arraycopy(others, 0, elements, paramCount, others.length);
141    return construct(elements.length, elements);
142  }
143
144  /**
145   * Constructs an {@code ImmutableSet} from the first {@code n} elements of the specified array. If
146   * {@code k} is the size of the returned {@code ImmutableSet}, then the unique elements of {@code
147   * elements} will be in the first {@code k} positions, and {@code elements[i] == null} for {@code
148   * k <= i < n}.
149   *
150   * <p>This may modify {@code elements}. Additionally, if {@code n == elements.length} and {@code
151   * elements} contains no duplicates, {@code elements} may be used without copying in the returned
152   * {@code ImmutableSet}, in which case it may no longer be modified.
153   *
154   * <p>{@code elements} may contain only values of type {@code E}.
155   *
156   * @throws NullPointerException if any of the first {@code n} elements of {@code elements} is null
157   */
158  private static <E> ImmutableSet<E> construct(int n, Object... elements) {
159    switch (n) {
160      case 0:
161        return of();
162      case 1:
163        @SuppressWarnings("unchecked") // safe; elements contains only E's
164        E elem = (E) elements[0];
165        return of(elem);
166      default:
167        // continue below to handle the general case
168    }
169    int tableSize = chooseTableSize(n);
170    Object[] table = new Object[tableSize];
171    int mask = tableSize - 1;
172    int hashCode = 0;
173    int uniques = 0;
174    for (int i = 0; i < n; i++) {
175      Object element = checkElementNotNull(elements[i], i);
176      int hash = element.hashCode();
177      for (int j = Hashing.smear(hash); ; j++) {
178        int index = j & mask;
179        Object value = table[index];
180        if (value == null) {
181          // Came to an empty slot. Put the element here.
182          elements[uniques++] = element;
183          table[index] = element;
184          hashCode += hash;
185          break;
186        } else if (value.equals(element)) {
187          break;
188        }
189      }
190    }
191    Arrays.fill(elements, uniques, n, null);
192    if (uniques == 1) {
193      // There is only one element or elements are all duplicates
194      @SuppressWarnings("unchecked") // we are careful to only pass in E
195      E element = (E) elements[0];
196      return new SingletonImmutableSet<E>(element, hashCode);
197    } else if (tableSize != chooseTableSize(uniques)) {
198      // Resize the table when the array includes too many duplicates.
199      // when this happens, we have already made a copy
200      return construct(uniques, elements);
201    } else {
202      Object[] uniqueElements =
203          (uniques < elements.length) ? Arrays.copyOf(elements, uniques) : elements;
204      return new RegularImmutableSet<E>(uniqueElements, hashCode, table, mask);
205    }
206  }
207
208  // We use power-of-2 tables, and this is the highest int that's a power of 2
209  static final int MAX_TABLE_SIZE = Ints.MAX_POWER_OF_TWO;
210
211  // Represents how tightly we can pack things, as a maximum.
212  private static final double DESIRED_LOAD_FACTOR = 0.7;
213
214  // If the set has this many elements, it will "max out" the table size
215  private static final int CUTOFF = (int) (MAX_TABLE_SIZE * DESIRED_LOAD_FACTOR);
216
217  /**
218   * Returns an array size suitable for the backing array of a hash table that uses open addressing
219   * with linear probing in its implementation. The returned size is the smallest power of two that
220   * can hold setSize elements with the desired load factor. Always returns at least setSize + 2.
221   */
222  @VisibleForTesting
223  static int chooseTableSize(int setSize) {
224    setSize = Math.max(setSize, 2);
225    // Correct the size for open addressing to match desired load factor.
226    if (setSize < CUTOFF) {
227      // Round up to the next highest power of 2.
228      int tableSize = Integer.highestOneBit(setSize - 1) << 1;
229      while (tableSize * DESIRED_LOAD_FACTOR < setSize) {
230        tableSize <<= 1;
231      }
232      return tableSize;
233    }
234
235    // The table can't be completely full or we'll get infinite reprobes
236    checkArgument(setSize < MAX_TABLE_SIZE, "collection too large");
237    return MAX_TABLE_SIZE;
238  }
239
240  /**
241   * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
242   * each appears first in the source collection.
243   *
244   * <p><b>Performance note:</b> This method will sometimes recognize that the actual copy operation
245   * is unnecessary; for example, {@code copyOf(copyOf(anArrayList))} will copy the data only once.
246   * This reduces the expense of habitually making defensive copies at API boundaries. However, the
247   * precise conditions for skipping the copy operation are undefined.
248   *
249   * @throws NullPointerException if any of {@code elements} is null
250   * @since 7.0 (source-compatible since 2.0)
251   */
252  public static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements) {
253    /*
254     * TODO(lowasser): consider checking for ImmutableAsList here
255     * TODO(lowasser): consider checking for Multiset here
256     */
257    // Don't refer to ImmutableSortedSet by name so it won't pull in all that code
258    if (elements instanceof ImmutableSet && !(elements instanceof SortedSet)) {
259      @SuppressWarnings("unchecked") // all supported methods are covariant
260      ImmutableSet<E> set = (ImmutableSet<E>) elements;
261      if (!set.isPartialView()) {
262        return set;
263      }
264    } else if (elements instanceof EnumSet) {
265      return copyOfEnumSet((EnumSet) elements);
266    }
267    Object[] array = elements.toArray();
268    return construct(array.length, array);
269  }
270
271  /**
272   * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
273   * each appears first in the source iterable. This method iterates over {@code elements} only
274   * once.
275   *
276   * <p><b>Performance note:</b> This method will sometimes recognize that the actual copy operation
277   * is unnecessary; for example, {@code copyOf(copyOf(anArrayList))} should copy the data only
278   * once. This reduces the expense of habitually making defensive copies at API boundaries.
279   * However, the precise conditions for skipping the copy operation are undefined.
280   *
281   * @throws NullPointerException if any of {@code elements} is null
282   */
283  public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements) {
284    return (elements instanceof Collection)
285        ? copyOf((Collection<? extends E>) elements)
286        : copyOf(elements.iterator());
287  }
288
289  /**
290   * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
291   * each appears first in the source iterator.
292   *
293   * @throws NullPointerException if any of {@code elements} is null
294   */
295  public static <E> ImmutableSet<E> copyOf(Iterator<? extends E> elements) {
296    // We special-case for 0 or 1 elements, but anything further is madness.
297    if (!elements.hasNext()) {
298      return of();
299    }
300    E first = elements.next();
301    if (!elements.hasNext()) {
302      return of(first);
303    } else {
304      return new ImmutableSet.Builder<E>().add(first).addAll(elements).build();
305    }
306  }
307
308  /**
309   * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
310   * each appears first in the source array.
311   *
312   * @throws NullPointerException if any of {@code elements} is null
313   * @since 3.0
314   */
315  public static <E> ImmutableSet<E> copyOf(E[] elements) {
316    switch (elements.length) {
317      case 0:
318        return of();
319      case 1:
320        return of(elements[0]);
321      default:
322        return construct(elements.length, elements.clone());
323    }
324  }
325
326  @SuppressWarnings("rawtypes") // necessary to compile against Java 8
327  private static ImmutableSet copyOfEnumSet(EnumSet enumSet) {
328    return ImmutableEnumSet.asImmutable(EnumSet.copyOf(enumSet));
329  }
330
331  ImmutableSet() {}
332
333  /** Returns {@code true} if the {@code hashCode()} method runs quickly. */
334  boolean isHashCodeFast() {
335    return false;
336  }
337
338  @Override
339  public boolean equals(@NullableDecl Object object) {
340    if (object == this) {
341      return true;
342    } else if (object instanceof ImmutableSet
343        && isHashCodeFast()
344        && ((ImmutableSet<?>) object).isHashCodeFast()
345        && hashCode() != object.hashCode()) {
346      return false;
347    }
348    return Sets.equalsImpl(this, object);
349  }
350
351  @Override
352  public int hashCode() {
353    return Sets.hashCodeImpl(this);
354  }
355
356  // This declaration is needed to make Set.iterator() and
357  // ImmutableCollection.iterator() consistent.
358  @Override
359  public abstract UnmodifiableIterator<E> iterator();
360
361  @LazyInit @NullableDecl @RetainedWith private transient ImmutableList<E> asList;
362
363  @Override
364  public ImmutableList<E> asList() {
365    ImmutableList<E> result = asList;
366    return (result == null) ? asList = createAsList() : result;
367  }
368
369  ImmutableList<E> createAsList() {
370    return new RegularImmutableAsList<E>(this, toArray());
371  }
372
373  abstract static class Indexed<E> extends ImmutableSet<E> {
374    abstract E get(int index);
375
376    @Override
377    public UnmodifiableIterator<E> iterator() {
378      return asList().iterator();
379    }
380
381    @Override
382    public Spliterator<E> spliterator() {
383      return CollectSpliterators.indexed(size(), SPLITERATOR_CHARACTERISTICS, this::get);
384    }
385
386    @Override
387    public void forEach(Consumer<? super E> consumer) {
388      checkNotNull(consumer);
389      int n = size();
390      for (int i = 0; i < n; i++) {
391        consumer.accept(get(i));
392      }
393    }
394
395    @Override
396    ImmutableList<E> createAsList() {
397      return new ImmutableAsList<E>() {
398        @Override
399        public E get(int index) {
400          return Indexed.this.get(index);
401        }
402
403        @Override
404        Indexed<E> delegateCollection() {
405          return Indexed.this;
406        }
407      };
408    }
409  }
410
411  /*
412   * This class is used to serialize all ImmutableSet instances, except for
413   * ImmutableEnumSet/ImmutableSortedSet, regardless of implementation type. It
414   * captures their "logical contents" and they are reconstructed using public
415   * static factories. This is necessary to ensure that the existence of a
416   * particular implementation type is an implementation detail.
417   */
418  private static class SerializedForm implements Serializable {
419    final Object[] elements;
420
421    SerializedForm(Object[] elements) {
422      this.elements = elements;
423    }
424
425    Object readResolve() {
426      return copyOf(elements);
427    }
428
429    private static final long serialVersionUID = 0;
430  }
431
432  @Override
433  Object writeReplace() {
434    return new SerializedForm(toArray());
435  }
436
437  /**
438   * Returns a new builder. The generated builder is equivalent to the builder created by the {@link
439   * Builder} constructor.
440   */
441  public static <E> Builder<E> builder() {
442    return new Builder<E>();
443  }
444
445  /**
446   * Returns a new builder, expecting the specified number of distinct elements to be added.
447   *
448   * <p>If {@code expectedSize} is exactly the number of distinct elements added to the builder
449   * before {@link Builder#build} is called, the builder is likely to perform better than an unsized
450   * {@link #builder()} would have.
451   *
452   * <p>It is not specified if any performance benefits apply if {@code expectedSize} is close to,
453   * but not exactly, the number of distinct elements added to the builder.
454   *
455   * @since 23.1
456   */
457  @Beta
458  public static <E> Builder<E> builderWithExpectedSize(int expectedSize) {
459    checkNonnegative(expectedSize, "expectedSize");
460    return new Builder<E>(expectedSize);
461  }
462
463  /**
464   * A builder for creating {@code ImmutableSet} instances. Example:
465   *
466   * <pre>{@code
467   * static final ImmutableSet<Color> GOOGLE_COLORS =
468   *     ImmutableSet.<Color>builder()
469   *         .addAll(WEBSAFE_COLORS)
470   *         .add(new Color(0, 191, 255))
471   *         .build();
472   * }</pre>
473   *
474   * <p>Elements appear in the resulting set in the same order they were first added to the builder.
475   *
476   * <p>Building does not change the state of the builder, so it is still possible to add more
477   * elements and to build again.
478   *
479   * @since 2.0
480   */
481  public static class Builder<E> extends ImmutableCollection.ArrayBasedBuilder<E> {
482    @NullableDecl @VisibleForTesting Object[] hashTable;
483    private int hashCode;
484
485    /**
486     * Creates a new builder. The returned builder is equivalent to the builder generated by {@link
487     * ImmutableSet#builder}.
488     */
489    public Builder() {
490      super(DEFAULT_INITIAL_CAPACITY);
491    }
492
493    Builder(int capacity) {
494      super(capacity);
495      this.hashTable = new Object[chooseTableSize(capacity)];
496    }
497
498    /**
499     * Adds {@code element} to the {@code ImmutableSet}. If the {@code ImmutableSet} already
500     * contains {@code element}, then {@code add} has no effect (only the previously added element
501     * is retained).
502     *
503     * @param element the element to add
504     * @return this {@code Builder} object
505     * @throws NullPointerException if {@code element} is null
506     */
507    @CanIgnoreReturnValue
508    @Override
509    public Builder<E> add(E element) {
510      checkNotNull(element);
511      if (hashTable != null && chooseTableSize(size) <= hashTable.length) {
512        addDeduping(element);
513        return this;
514      } else {
515        hashTable = null;
516        super.add(element);
517        return this;
518      }
519    }
520
521    private void addDeduping(E element) {
522      int mask = hashTable.length - 1;
523      int hash = element.hashCode();
524      for (int i = Hashing.smear(hash); ; i++) {
525        i &= mask;
526        Object previous = hashTable[i];
527        if (previous == null) {
528          hashTable[i] = element;
529          hashCode += hash;
530          super.add(element);
531          return;
532        } else if (previous.equals(element)) {
533          return;
534        }
535      }
536    }
537
538    /**
539     * Adds each element of {@code elements} to the {@code ImmutableSet}, ignoring duplicate
540     * elements (only the first duplicate element is added).
541     *
542     * @param elements the elements to add
543     * @return this {@code Builder} object
544     * @throws NullPointerException if {@code elements} is null or contains a null element
545     */
546    @CanIgnoreReturnValue
547    @Override
548    public Builder<E> add(E... elements) {
549      if (hashTable != null) {
550        for (E e : elements) {
551          add(e);
552        }
553      } else {
554        super.add(elements);
555      }
556      return this;
557    }
558
559    /**
560     * Adds each element of {@code elements} to the {@code ImmutableSet}, ignoring duplicate
561     * elements (only the first duplicate element is added).
562     *
563     * @param elements the {@code Iterable} to add to the {@code ImmutableSet}
564     * @return this {@code Builder} object
565     * @throws NullPointerException if {@code elements} is null or contains a null element
566     */
567    @CanIgnoreReturnValue
568    @Override
569    public Builder<E> addAll(Iterable<? extends E> elements) {
570      checkNotNull(elements);
571      if (hashTable != null) {
572        for (E e : elements) {
573          add(e);
574        }
575      } else {
576        super.addAll(elements);
577      }
578      return this;
579    }
580
581    /**
582     * Adds each element of {@code elements} to the {@code ImmutableSet}, ignoring duplicate
583     * elements (only the first duplicate element is added).
584     *
585     * @param elements the elements to add to the {@code ImmutableSet}
586     * @return this {@code Builder} object
587     * @throws NullPointerException if {@code elements} is null or contains a null element
588     */
589    @CanIgnoreReturnValue
590    @Override
591    public Builder<E> addAll(Iterator<? extends E> elements) {
592      checkNotNull(elements);
593      while (elements.hasNext()) {
594        add(elements.next());
595      }
596      return this;
597    }
598
599    @SuppressWarnings("unchecked")
600    @CanIgnoreReturnValue
601    @Override
602    Builder<E> combine(ArrayBasedBuilder<E> builder) {
603      if (hashTable != null && builder instanceof Builder) {
604        for (int i = 0; i < builder.size; i++) {
605          addDeduping((E) builder.contents[i]);
606        }
607      } else {
608        super.combine(builder);
609      }
610      return this;
611    }
612
613    /**
614     * Returns a newly-created {@code ImmutableSet} based on the contents of the {@code Builder}.
615     */
616    @SuppressWarnings("unchecked")
617    @Override
618    public ImmutableSet<E> build() {
619      switch (size) {
620        case 0:
621          return of();
622        case 1:
623          return (ImmutableSet<E>) of(contents[0]);
624        default:
625          ImmutableSet<E> result;
626          if (hashTable != null && size == contents.length) {
627            result =
628                new RegularImmutableSet<E>(contents, hashCode, hashTable, hashTable.length - 1);
629          } else {
630            result = construct(size, contents);
631            // construct has the side effect of deduping contents, so we update size
632            // accordingly.
633            size = result.size();
634          }
635          forceCopy = true;
636          hashTable = null;
637          return result;
638      }
639    }
640  }
641}