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,
116        "the total number of elements must fit in an int");
117    final int paramCount = 6;
118    Object[] elements = new Object[paramCount + others.length];
119    elements[0] = e1;
120    elements[1] = e2;
121    elements[2] = e3;
122    elements[3] = e4;
123    elements[4] = e5;
124    elements[5] = e6;
125    System.arraycopy(others, 0, elements, paramCount, others.length);
126    return construct(elements.length, elements);
127  }
128
129  /**
130   * Constructs an {@code ImmutableSet} from the first {@code n} elements of the specified array. If
131   * {@code k} is the size of the returned {@code ImmutableSet}, then the unique elements of {@code
132   * elements} will be in the first {@code k} positions, and {@code elements[i] == null} for {@code
133   * k <= i < n}.
134   *
135   * <p>After this method returns, {@code elements} will contain no duplicates, but {@code elements}
136   * may be the real array backing the returned set, so do not modify it further.
137   *
138   * <p>{@code elements} may contain only values of type {@code E}.
139   *
140   * @throws NullPointerException if any of the first {@code n} elements of {@code elements} is null
141   */
142  private static <E> ImmutableSet<E> construct(int n, Object... elements) {
143    switch (n) {
144      case 0:
145        return of();
146      case 1:
147        @SuppressWarnings("unchecked") // safe; elements contains only E's
148        E elem = (E) elements[0];
149        return of(elem);
150      default:
151        // continue below to handle the general case
152    }
153    int tableSize = chooseTableSize(n);
154    Object[] table = new Object[tableSize];
155    int mask = tableSize - 1;
156    int hashCode = 0;
157    int uniques = 0;
158    for (int i = 0; i < n; i++) {
159      Object element = checkElementNotNull(elements[i], i);
160      int hash = element.hashCode();
161      for (int j = Hashing.smear(hash); ; j++) {
162        int index = j & mask;
163        Object value = table[index];
164        if (value == null) {
165          // Came to an empty slot. Put the element here.
166          elements[uniques++] = element;
167          table[index] = element;
168          hashCode += hash;
169          break;
170        } else if (value.equals(element)) {
171          break;
172        }
173      }
174    }
175    Arrays.fill(elements, uniques, n, null);
176    if (uniques == 1) {
177      // There is only one element or elements are all duplicates
178      @SuppressWarnings("unchecked") // we are careful to only pass in E
179      E element = (E) elements[0];
180      return new SingletonImmutableSet<E>(element, hashCode);
181    } else if (chooseTableSize(uniques) < tableSize / 2) {
182      // Resize the table when the array includes too many duplicates.
183      return construct(uniques, elements);
184    } else {
185      Object[] uniqueElements =
186          shouldTrim(uniques, elements.length) ? Arrays.copyOf(elements, uniques) : elements;
187      return new RegularImmutableSet<E>(uniqueElements, hashCode, table, mask, uniques);
188    }
189  }
190
191  private static boolean shouldTrim(int actualUnique, int expectedUnique) {
192    return actualUnique < (expectedUnique >> 1) + (expectedUnique >> 2);
193  }
194
195  // We use power-of-2 tables, and this is the highest int that's a power of 2
196  static final int MAX_TABLE_SIZE = Ints.MAX_POWER_OF_TWO;
197
198  // Represents how tightly we can pack things, as a maximum.
199  private static final double DESIRED_LOAD_FACTOR = 0.7;
200
201  // If the set has this many elements, it will "max out" the table size
202  private static final int CUTOFF = (int) (MAX_TABLE_SIZE * DESIRED_LOAD_FACTOR);
203
204  /**
205   * Returns an array size suitable for the backing array of a hash table that uses open addressing
206   * with linear probing in its implementation. The returned size is the smallest power of two that
207   * can hold setSize elements with the desired load factor. Always returns at least setSize + 2.
208   */
209  @VisibleForTesting
210  static int chooseTableSize(int setSize) {
211    setSize = Math.max(setSize, 2);
212    // Correct the size for open addressing to match desired load factor.
213    if (setSize < CUTOFF) {
214      // Round up to the next highest power of 2.
215      int tableSize = Integer.highestOneBit(setSize - 1) << 1;
216      while (tableSize * DESIRED_LOAD_FACTOR < setSize) {
217        tableSize <<= 1;
218      }
219      return tableSize;
220    }
221
222    // The table can't be completely full or we'll get infinite reprobes
223    checkArgument(setSize < MAX_TABLE_SIZE, "collection too large");
224    return MAX_TABLE_SIZE;
225  }
226
227  /**
228   * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
229   * each appears first in the source collection.
230   *
231   * <p><b>Performance note:</b> This method will sometimes recognize that the actual copy operation
232   * is unnecessary; for example, {@code copyOf(copyOf(anArrayList))} will copy the data only once.
233   * This reduces the expense of habitually making defensive copies at API boundaries. However, the
234   * precise conditions for skipping the copy operation are undefined.
235   *
236   * @throws NullPointerException if any of {@code elements} is null
237   * @since 7.0 (source-compatible since 2.0)
238   */
239  public static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements) {
240    /*
241     * TODO(lowasser): consider checking for ImmutableAsList here
242     * TODO(lowasser): consider checking for Multiset here
243     */
244    // Don't refer to ImmutableSortedSet by name so it won't pull in all that code
245    if (elements instanceof ImmutableSet && !(elements instanceof SortedSet)) {
246      @SuppressWarnings("unchecked") // all supported methods are covariant
247      ImmutableSet<E> set = (ImmutableSet<E>) elements;
248      if (!set.isPartialView()) {
249        return set;
250      }
251    }
252    Object[] array = elements.toArray();
253    return construct(array.length, array);
254  }
255
256  /**
257   * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
258   * each appears first in the source iterable. This method iterates over {@code elements} only
259   * once.
260   *
261   * <p><b>Performance note:</b> This method will sometimes recognize that the actual copy operation
262   * is unnecessary; for example, {@code copyOf(copyOf(anArrayList))} should copy the data only
263   * once. This reduces the expense of habitually making defensive copies at API boundaries.
264   * However, the precise conditions for skipping the copy operation are undefined.
265   *
266   * @throws NullPointerException if any of {@code elements} is null
267   */
268  public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements) {
269    return (elements instanceof Collection)
270        ? copyOf((Collection<? extends E>) elements)
271        : copyOf(elements.iterator());
272  }
273
274  /**
275   * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
276   * each appears first in the source iterator.
277   *
278   * @throws NullPointerException if any of {@code elements} is null
279   */
280  public static <E> ImmutableSet<E> copyOf(Iterator<? extends E> elements) {
281    // We special-case for 0 or 1 elements, but anything further is madness.
282    if (!elements.hasNext()) {
283      return of();
284    }
285    E first = elements.next();
286    if (!elements.hasNext()) {
287      return of(first);
288    } else {
289      return new ImmutableSet.Builder<E>().add(first).addAll(elements).build();
290    }
291  }
292
293  /**
294   * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
295   * each appears first in the source array.
296   *
297   * @throws NullPointerException if any of {@code elements} is null
298   * @since 3.0
299   */
300  public static <E> ImmutableSet<E> copyOf(E[] elements) {
301    switch (elements.length) {
302      case 0:
303        return of();
304      case 1:
305        return of(elements[0]);
306      default:
307        return construct(elements.length, elements.clone());
308    }
309  }
310
311  ImmutableSet() {}
312
313  /** Returns {@code true} if the {@code hashCode()} method runs quickly. */
314  boolean isHashCodeFast() {
315    return false;
316  }
317
318  @Override
319  public boolean equals(@NullableDecl Object object) {
320    if (object == this) {
321      return true;
322    } else 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    @CanIgnoreReturnValue
472    @Override
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    /**
542     * Returns a newly-created {@code ImmutableSet} based on the contents of the {@code Builder}.
543     */
544    @SuppressWarnings("unchecked")
545    @Override
546    public ImmutableSet<E> build() {
547      switch (size) {
548        case 0:
549          return of();
550        case 1:
551          return (ImmutableSet<E>) of(contents[0]);
552        default:
553          ImmutableSet<E> result;
554          if (hashTable != null && chooseTableSize(size) == hashTable.length) {
555            Object[] uniqueElements =
556                shouldTrim(size, contents.length) ? Arrays.copyOf(contents, size) : contents;
557            result =
558                new RegularImmutableSet<E>(
559                    uniqueElements, hashCode, hashTable, hashTable.length - 1, size);
560          } else {
561            result = construct(size, contents);
562            // construct has the side effect of deduping contents, so we update size
563            // accordingly.
564            size = result.size();
565          }
566          forceCopy = true;
567          hashTable = null;
568          return result;
569      }
570    }
571  }
572}