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