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