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.checkElementIndex; 021import static com.google.common.base.Preconditions.checkNotNull; 022import static com.google.common.base.Preconditions.checkPositionIndex; 023import static com.google.common.base.Preconditions.checkPositionIndexes; 024import static com.google.common.collect.CollectPreconditions.checkNonnegative; 025import static com.google.common.collect.ObjectArrays.checkElementsNotNull; 026import static com.google.common.collect.RegularImmutableList.EMPTY; 027 028import com.google.common.annotations.Beta; 029import com.google.common.annotations.GwtCompatible; 030import com.google.errorprone.annotations.CanIgnoreReturnValue; 031import java.io.InvalidObjectException; 032import java.io.ObjectInputStream; 033import java.io.Serializable; 034import java.util.Arrays; 035import java.util.Collection; 036import java.util.Collections; 037import java.util.Comparator; 038import java.util.Iterator; 039import java.util.List; 040import java.util.RandomAccess; 041import org.checkerframework.checker.nullness.compatqual.NullableDecl; 042 043/** 044 * A {@link List} whose contents will never change, with many other important properties detailed at 045 * {@link ImmutableCollection}. 046 * 047 * <p>See the Guava User Guide article on <a href= 048 * "https://github.com/google/guava/wiki/ImmutableCollectionsExplained"> immutable collections</a>. 049 * 050 * @see ImmutableMap 051 * @see ImmutableSet 052 * @author Kevin Bourrillion 053 * @since 2.0 054 */ 055@GwtCompatible(serializable = true, emulated = true) 056@SuppressWarnings("serial") // we're overriding default serialization 057public abstract class ImmutableList<E> extends ImmutableCollection<E> 058 implements List<E>, RandomAccess { 059 /** 060 * Returns the empty immutable list. This list behaves and performs comparably to {@link 061 * Collections#emptyList}, and is preferable mainly for consistency and maintainability of your 062 * code. 063 */ 064 // Casting to any type is safe because the list will never hold any elements. 065 @SuppressWarnings("unchecked") 066 public static <E> ImmutableList<E> of() { 067 return (ImmutableList<E>) EMPTY; 068 } 069 070 /** 071 * Returns an immutable list containing a single element. This list behaves and performs 072 * comparably to {@link Collections#singleton}, but will not accept a null element. It is 073 * preferable mainly for consistency and maintainability of your code. 074 * 075 * @throws NullPointerException if {@code element} is null 076 */ 077 public static <E> ImmutableList<E> of(E element) { 078 return construct(element); 079 } 080 081 /** 082 * Returns an immutable list containing the given elements, in order. 083 * 084 * @throws NullPointerException if any element is null 085 */ 086 public static <E> ImmutableList<E> of(E e1, E e2) { 087 return construct(e1, e2); 088 } 089 090 /** 091 * Returns an immutable list containing the given elements, in order. 092 * 093 * @throws NullPointerException if any element is null 094 */ 095 public static <E> ImmutableList<E> of(E e1, E e2, E e3) { 096 return construct(e1, e2, e3); 097 } 098 099 /** 100 * Returns an immutable list containing the given elements, in order. 101 * 102 * @throws NullPointerException if any element is null 103 */ 104 public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4) { 105 return construct(e1, e2, e3, e4); 106 } 107 108 /** 109 * Returns an immutable list containing the given elements, in order. 110 * 111 * @throws NullPointerException if any element is null 112 */ 113 public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5) { 114 return construct(e1, e2, e3, e4, e5); 115 } 116 117 /** 118 * Returns an immutable list containing the given elements, in order. 119 * 120 * @throws NullPointerException if any element is null 121 */ 122 public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6) { 123 return construct(e1, e2, e3, e4, e5, e6); 124 } 125 126 /** 127 * Returns an immutable list containing the given elements, in order. 128 * 129 * @throws NullPointerException if any element is null 130 */ 131 public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) { 132 return construct(e1, e2, e3, e4, e5, e6, e7); 133 } 134 135 /** 136 * Returns an immutable list containing the given elements, in order. 137 * 138 * @throws NullPointerException if any element is null 139 */ 140 public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) { 141 return construct(e1, e2, e3, e4, e5, e6, e7, e8); 142 } 143 144 /** 145 * Returns an immutable list containing the given elements, in order. 146 * 147 * @throws NullPointerException if any element is null 148 */ 149 public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) { 150 return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9); 151 } 152 153 /** 154 * Returns an immutable list containing the given elements, in order. 155 * 156 * @throws NullPointerException if any element is null 157 */ 158 public static <E> ImmutableList<E> of( 159 E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) { 160 return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10); 161 } 162 163 /** 164 * Returns an immutable list containing the given elements, in order. 165 * 166 * @throws NullPointerException if any element is null 167 */ 168 public static <E> ImmutableList<E> of( 169 E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11) { 170 return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11); 171 } 172 173 // These go up to eleven. After that, you just get the varargs form, and 174 // whatever warnings might come along with it. :( 175 176 /** 177 * Returns an immutable list containing the given elements, in order. 178 * 179 * <p>The array {@code others} must not be longer than {@code Integer.MAX_VALUE - 12}. 180 * 181 * @throws NullPointerException if any element is null 182 * @since 3.0 (source-compatible since 2.0) 183 */ 184 @SafeVarargs // For Eclipse. For internal javac we have disabled this pointless type of warning. 185 public static <E> ImmutableList<E> of( 186 E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11, E e12, E... others) { 187 checkArgument( 188 others.length <= Integer.MAX_VALUE - 12, 189 "the total number of elements must fit in an int"); 190 Object[] array = new Object[12 + others.length]; 191 array[0] = e1; 192 array[1] = e2; 193 array[2] = e3; 194 array[3] = e4; 195 array[4] = e5; 196 array[5] = e6; 197 array[6] = e7; 198 array[7] = e8; 199 array[8] = e9; 200 array[9] = e10; 201 array[10] = e11; 202 array[11] = e12; 203 System.arraycopy(others, 0, array, 12, others.length); 204 return construct(array); 205 } 206 207 /** 208 * Returns an immutable list containing the given elements, in order. If {@code elements} is a 209 * {@link Collection}, this method behaves exactly as {@link #copyOf(Collection)}; otherwise, it 210 * behaves exactly as {@code copyOf(elements.iterator()}. 211 * 212 * @throws NullPointerException if any of {@code elements} is null 213 */ 214 public static <E> ImmutableList<E> copyOf(Iterable<? extends E> elements) { 215 checkNotNull(elements); // TODO(kevinb): is this here only for GWT? 216 return (elements instanceof Collection) 217 ? copyOf((Collection<? extends E>) elements) 218 : copyOf(elements.iterator()); 219 } 220 221 /** 222 * Returns an immutable list containing the given elements, in order. 223 * 224 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is 225 * safe to do so. The exact circumstances under which a copy will or will not be performed are 226 * undocumented and subject to change. 227 * 228 * <p>Note that if {@code list} is a {@code List<String>}, then {@code ImmutableList.copyOf(list)} 229 * returns an {@code ImmutableList<String>} containing each of the strings in {@code list}, while 230 * ImmutableList.of(list)} returns an {@code ImmutableList<List<String>>} containing one element 231 * (the given list itself). 232 * 233 * <p>This method is safe to use even when {@code elements} is a synchronized or concurrent 234 * collection that is currently being modified by another thread. 235 * 236 * @throws NullPointerException if any of {@code elements} is null 237 */ 238 public static <E> ImmutableList<E> copyOf(Collection<? extends E> elements) { 239 if (elements instanceof ImmutableCollection) { 240 @SuppressWarnings("unchecked") // all supported methods are covariant 241 ImmutableList<E> list = ((ImmutableCollection<E>) elements).asList(); 242 return list.isPartialView() ? ImmutableList.<E>asImmutableList(list.toArray()) : list; 243 } 244 return construct(elements.toArray()); 245 } 246 247 /** 248 * Returns an immutable list containing the given elements, in order. 249 * 250 * @throws NullPointerException if any of {@code elements} is null 251 */ 252 public static <E> ImmutableList<E> copyOf(Iterator<? extends E> elements) { 253 // We special-case for 0 or 1 elements, but going further is madness. 254 if (!elements.hasNext()) { 255 return of(); 256 } 257 E first = elements.next(); 258 if (!elements.hasNext()) { 259 return of(first); 260 } else { 261 return new ImmutableList.Builder<E>().add(first).addAll(elements).build(); 262 } 263 } 264 265 /** 266 * Returns an immutable list containing the given elements, in order. 267 * 268 * @throws NullPointerException if any of {@code elements} is null 269 * @since 3.0 270 */ 271 public static <E> ImmutableList<E> copyOf(E[] elements) { 272 return (elements.length == 0) 273 ? ImmutableList.<E>of() 274 : ImmutableList.<E>construct(elements.clone()); 275 } 276 277 /** 278 * Returns an immutable list containing the given elements, sorted according to their natural 279 * order. The sorting algorithm used is stable, so elements that compare as equal will stay in the 280 * order in which they appear in the input. 281 * 282 * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code 283 * ImmutableSortedSet.copyOf(elements)}; if you want a {@code List} you can use its {@code 284 * asList()} view. 285 * 286 * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted 287 * {@code ImmutableList}, use {@code stream.sorted().collect(toImmutableList())}. 288 * 289 * @throws NullPointerException if any element in the input is null 290 * @since 21.0 291 */ 292 public static <E extends Comparable<? super E>> ImmutableList<E> sortedCopyOf( 293 Iterable<? extends E> elements) { 294 Comparable<?>[] array = Iterables.toArray(elements, new Comparable<?>[0]); 295 checkElementsNotNull((Object[]) array); 296 Arrays.sort(array); 297 return asImmutableList(array); 298 } 299 300 /** 301 * Returns an immutable list containing the given elements, in sorted order relative to the 302 * specified comparator. The sorting algorithm used is stable, so elements that compare as equal 303 * will stay in the order in which they appear in the input. 304 * 305 * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code 306 * ImmutableSortedSet.copyOf(comparator, elements)}; if you want a {@code List} you can use its 307 * {@code asList()} view. 308 * 309 * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted 310 * {@code ImmutableList}, use {@code stream.sorted(comparator).collect(toImmutableList())}. 311 * 312 * @throws NullPointerException if any element in the input is null 313 * @since 21.0 314 */ 315 public static <E> ImmutableList<E> sortedCopyOf( 316 Comparator<? super E> comparator, Iterable<? extends E> elements) { 317 checkNotNull(comparator); 318 @SuppressWarnings("unchecked") // all supported methods are covariant 319 E[] array = (E[]) Iterables.toArray(elements); 320 checkElementsNotNull(array); 321 Arrays.sort(array, comparator); 322 return asImmutableList(array); 323 } 324 325 /** Views the array as an immutable list. Checks for nulls; does not copy. */ 326 private static <E> ImmutableList<E> construct(Object... elements) { 327 return asImmutableList(checkElementsNotNull(elements)); 328 } 329 330 /** 331 * Views the array as an immutable list. Does not check for nulls; does not copy. 332 * 333 * <p>The array must be internally created. 334 */ 335 static <E> ImmutableList<E> asImmutableList(Object[] elements) { 336 return asImmutableList(elements, elements.length); 337 } 338 339 /** Views the array as an immutable list. Does not check for nulls. */ 340 static <E> ImmutableList<E> asImmutableList(Object[] elements, int length) { 341 if (length == 0) { 342 return of(); 343 } 344 return new RegularImmutableList<E>(elements, length); 345 } 346 347 ImmutableList() {} 348 349 // This declaration is needed to make List.iterator() and 350 // ImmutableCollection.iterator() consistent. 351 @Override 352 public UnmodifiableIterator<E> iterator() { 353 return listIterator(); 354 } 355 356 @Override 357 public UnmodifiableListIterator<E> listIterator() { 358 return listIterator(0); 359 } 360 361 @SuppressWarnings("unchecked") 362 @Override 363 public UnmodifiableListIterator<E> listIterator(int index) { 364 checkPositionIndex(index, size()); 365 if (isEmpty()) { 366 return (UnmodifiableListIterator<E>) EMPTY_ITR; 367 } else { 368 return new Itr<E>(this, index); 369 } 370 } 371 372 /** A singleton implementation of iterator() for the empty ImmutableList. */ 373 private static final UnmodifiableListIterator<Object> EMPTY_ITR = 374 new Itr<Object>(RegularImmutableList.EMPTY, 0); 375 376 static class Itr<E> extends AbstractIndexedListIterator<E> { 377 private final ImmutableList<E> list; 378 379 Itr(ImmutableList<E> list, int index) { 380 super(list.size(), index); 381 this.list = list; 382 } 383 384 @Override 385 protected E get(int index) { 386 return list.get(index); 387 } 388 } 389 390 @Override 391 public int indexOf(@NullableDecl Object object) { 392 return (object == null) ? -1 : Lists.indexOfImpl(this, object); 393 } 394 395 @Override 396 public int lastIndexOf(@NullableDecl Object object) { 397 return (object == null) ? -1 : Lists.lastIndexOfImpl(this, object); 398 } 399 400 @Override 401 public boolean contains(@NullableDecl Object object) { 402 return indexOf(object) >= 0; 403 } 404 405 // constrain the return type to ImmutableList<E> 406 407 /** 408 * Returns an immutable list of the elements between the specified {@code fromIndex}, inclusive, 409 * and {@code toIndex}, exclusive. (If {@code fromIndex} and {@code toIndex} are equal, the empty 410 * immutable list is returned.) 411 */ 412 @Override 413 public ImmutableList<E> subList(int fromIndex, int toIndex) { 414 checkPositionIndexes(fromIndex, toIndex, size()); 415 int length = toIndex - fromIndex; 416 if (length == size()) { 417 return this; 418 } else if (length == 0) { 419 return of(); 420 } else { 421 return subListUnchecked(fromIndex, toIndex); 422 } 423 } 424 425 /** 426 * Called by the default implementation of {@link #subList} when {@code toIndex - fromIndex > 1}, 427 * after index validation has already been performed. 428 */ 429 ImmutableList<E> subListUnchecked(int fromIndex, int toIndex) { 430 return new SubList(fromIndex, toIndex - fromIndex); 431 } 432 433 class SubList extends ImmutableList<E> { 434 final transient int offset; 435 final transient int length; 436 437 SubList(int offset, int length) { 438 this.offset = offset; 439 this.length = length; 440 } 441 442 @Override 443 public int size() { 444 return length; 445 } 446 447 @Override 448 Object[] internalArray() { 449 return ImmutableList.this.internalArray(); 450 } 451 452 @Override 453 int internalArrayStart() { 454 return ImmutableList.this.internalArrayStart() + offset; 455 } 456 457 @Override 458 int internalArrayEnd() { 459 return ImmutableList.this.internalArrayStart() + offset + length; 460 } 461 462 @Override 463 public E get(int index) { 464 checkElementIndex(index, length); 465 return ImmutableList.this.get(index + offset); 466 } 467 468 @Override 469 public ImmutableList<E> subList(int fromIndex, int toIndex) { 470 checkPositionIndexes(fromIndex, toIndex, length); 471 return ImmutableList.this.subList(fromIndex + offset, toIndex + offset); 472 } 473 474 @Override 475 boolean isPartialView() { 476 return true; 477 } 478 } 479 480 /** 481 * Guaranteed to throw an exception and leave the list unmodified. 482 * 483 * @throws UnsupportedOperationException always 484 * @deprecated Unsupported operation. 485 */ 486 @CanIgnoreReturnValue 487 @Deprecated 488 @Override 489 public final boolean addAll(int index, Collection<? extends E> newElements) { 490 throw new UnsupportedOperationException(); 491 } 492 493 /** 494 * Guaranteed to throw an exception and leave the list unmodified. 495 * 496 * @throws UnsupportedOperationException always 497 * @deprecated Unsupported operation. 498 */ 499 @CanIgnoreReturnValue 500 @Deprecated 501 @Override 502 public final E set(int index, E element) { 503 throw new UnsupportedOperationException(); 504 } 505 506 /** 507 * Guaranteed to throw an exception and leave the list unmodified. 508 * 509 * @throws UnsupportedOperationException always 510 * @deprecated Unsupported operation. 511 */ 512 @Deprecated 513 @Override 514 public final void add(int index, E element) { 515 throw new UnsupportedOperationException(); 516 } 517 518 /** 519 * Guaranteed to throw an exception and leave the list unmodified. 520 * 521 * @throws UnsupportedOperationException always 522 * @deprecated Unsupported operation. 523 */ 524 @CanIgnoreReturnValue 525 @Deprecated 526 @Override 527 public final E remove(int index) { 528 throw new UnsupportedOperationException(); 529 } 530 531 /** 532 * Returns this list instance. 533 * 534 * @since 2.0 535 */ 536 @Override 537 public final ImmutableList<E> asList() { 538 return this; 539 } 540 541 @Override 542 int copyIntoArray(Object[] dst, int offset) { 543 // this loop is faster for RandomAccess instances, which ImmutableLists are 544 int size = size(); 545 for (int i = 0; i < size; i++) { 546 dst[offset + i] = get(i); 547 } 548 return offset + size; 549 } 550 551 /** 552 * Returns a view of this immutable list in reverse order. For example, {@code ImmutableList.of(1, 553 * 2, 3).reverse()} is equivalent to {@code ImmutableList.of(3, 2, 1)}. 554 * 555 * @return a view of this immutable list in reverse order 556 * @since 7.0 557 */ 558 public ImmutableList<E> reverse() { 559 return (size() <= 1) ? this : new ReverseImmutableList<E>(this); 560 } 561 562 private static class ReverseImmutableList<E> extends ImmutableList<E> { 563 private final transient ImmutableList<E> forwardList; 564 565 ReverseImmutableList(ImmutableList<E> backingList) { 566 this.forwardList = backingList; 567 } 568 569 private int reverseIndex(int index) { 570 return (size() - 1) - index; 571 } 572 573 private int reversePosition(int index) { 574 return size() - index; 575 } 576 577 @Override 578 public ImmutableList<E> reverse() { 579 return forwardList; 580 } 581 582 @Override 583 public boolean contains(@NullableDecl Object object) { 584 return forwardList.contains(object); 585 } 586 587 @Override 588 public int indexOf(@NullableDecl Object object) { 589 int index = forwardList.lastIndexOf(object); 590 return (index >= 0) ? reverseIndex(index) : -1; 591 } 592 593 @Override 594 public int lastIndexOf(@NullableDecl Object object) { 595 int index = forwardList.indexOf(object); 596 return (index >= 0) ? reverseIndex(index) : -1; 597 } 598 599 @Override 600 public ImmutableList<E> subList(int fromIndex, int toIndex) { 601 checkPositionIndexes(fromIndex, toIndex, size()); 602 return forwardList.subList(reversePosition(toIndex), reversePosition(fromIndex)).reverse(); 603 } 604 605 @Override 606 public E get(int index) { 607 checkElementIndex(index, size()); 608 return forwardList.get(reverseIndex(index)); 609 } 610 611 @Override 612 public int size() { 613 return forwardList.size(); 614 } 615 616 @Override 617 boolean isPartialView() { 618 return forwardList.isPartialView(); 619 } 620 } 621 622 @Override 623 public boolean equals(@NullableDecl Object obj) { 624 return Lists.equalsImpl(this, obj); 625 } 626 627 @Override 628 public int hashCode() { 629 int hashCode = 1; 630 int n = size(); 631 for (int i = 0; i < n; i++) { 632 hashCode = 31 * hashCode + get(i).hashCode(); 633 634 hashCode = ~~hashCode; 635 // needed to deal with GWT integer overflow 636 } 637 return hashCode; 638 } 639 640 /* 641 * Serializes ImmutableLists as their logical contents. This ensures that 642 * implementation types do not leak into the serialized representation. 643 */ 644 static class SerializedForm implements Serializable { 645 final Object[] elements; 646 647 SerializedForm(Object[] elements) { 648 this.elements = elements; 649 } 650 651 Object readResolve() { 652 return copyOf(elements); 653 } 654 655 private static final long serialVersionUID = 0; 656 } 657 658 private void readObject(ObjectInputStream stream) throws InvalidObjectException { 659 throw new InvalidObjectException("Use SerializedForm"); 660 } 661 662 @Override 663 Object writeReplace() { 664 return new SerializedForm(toArray()); 665 } 666 667 /** 668 * Returns a new builder. The generated builder is equivalent to the builder created by the {@link 669 * Builder} constructor. 670 */ 671 public static <E> Builder<E> builder() { 672 return new Builder<E>(); 673 } 674 675 /** 676 * Returns a new builder, expecting the specified number of elements to be added. 677 * 678 * <p>If {@code expectedSize} is exactly the number of elements added to the builder before {@link 679 * Builder#build} is called, the builder is likely to perform better than an unsized {@link 680 * #builder()} would have. 681 * 682 * <p>It is not specified if any performance benefits apply if {@code expectedSize} is close to, 683 * but not exactly, the number of elements added to the builder. 684 * 685 * @since 23.1 686 */ 687 @Beta 688 public static <E> Builder<E> builderWithExpectedSize(int expectedSize) { 689 checkNonnegative(expectedSize, "expectedSize"); 690 return new ImmutableList.Builder<E>(expectedSize); 691 } 692 693 /** 694 * A builder for creating immutable list instances, especially {@code public static final} lists 695 * ("constant lists"). Example: 696 * 697 * <pre>{@code 698 * public static final ImmutableList<Color> GOOGLE_COLORS 699 * = new ImmutableList.Builder<Color>() 700 * .addAll(WEBSAFE_COLORS) 701 * .add(new Color(0, 191, 255)) 702 * .build(); 703 * }</pre> 704 * 705 * <p>Elements appear in the resulting list in the same order they were added to the builder. 706 * 707 * <p>Builder instances can be reused; it is safe to call {@link #build} multiple times to build 708 * multiple lists in series. Each new list contains all the elements of the ones created before 709 * it. 710 * 711 * @since 2.0 712 */ 713 public static final class Builder<E> extends ImmutableCollection.ArrayBasedBuilder<E> { 714 /** 715 * Creates a new builder. The returned builder is equivalent to the builder generated by {@link 716 * ImmutableList#builder}. 717 */ 718 public Builder() { 719 this(DEFAULT_INITIAL_CAPACITY); 720 } 721 722 Builder(int capacity) { 723 super(capacity); 724 } 725 726 /** 727 * Adds {@code element} to the {@code ImmutableList}. 728 * 729 * @param element the element to add 730 * @return this {@code Builder} object 731 * @throws NullPointerException if {@code element} is null 732 */ 733 @CanIgnoreReturnValue 734 @Override 735 public Builder<E> add(E element) { 736 super.add(element); 737 return this; 738 } 739 740 /** 741 * Adds each element of {@code elements} to the {@code ImmutableList}. 742 * 743 * @param elements the {@code Iterable} to add to the {@code ImmutableList} 744 * @return this {@code Builder} object 745 * @throws NullPointerException if {@code elements} is null or contains a null element 746 */ 747 @CanIgnoreReturnValue 748 @Override 749 public Builder<E> add(E... elements) { 750 super.add(elements); 751 return this; 752 } 753 754 /** 755 * Adds each element of {@code elements} to the {@code ImmutableList}. 756 * 757 * @param elements the {@code Iterable} to add to the {@code ImmutableList} 758 * @return this {@code Builder} object 759 * @throws NullPointerException if {@code elements} is null or contains a null element 760 */ 761 @CanIgnoreReturnValue 762 @Override 763 public Builder<E> addAll(Iterable<? extends E> elements) { 764 super.addAll(elements); 765 return this; 766 } 767 768 /** 769 * Adds each element of {@code elements} to the {@code ImmutableList}. 770 * 771 * @param elements the {@code Iterable} to add to the {@code ImmutableList} 772 * @return this {@code Builder} object 773 * @throws NullPointerException if {@code elements} is null or contains a null element 774 */ 775 @CanIgnoreReturnValue 776 @Override 777 public Builder<E> addAll(Iterator<? extends E> elements) { 778 super.addAll(elements); 779 return this; 780 } 781 782 /** 783 * Returns a newly-created {@code ImmutableList} based on the contents of the {@code Builder}. 784 */ 785 @Override 786 public ImmutableList<E> build() { 787 forceCopy = true; 788 return asImmutableList(contents, size); 789 } 790 } 791}