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