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 public E get(int index) { 449 checkElementIndex(index, length); 450 return ImmutableList.this.get(index + offset); 451 } 452 453 @Override 454 public ImmutableList<E> subList(int fromIndex, int toIndex) { 455 checkPositionIndexes(fromIndex, toIndex, length); 456 return ImmutableList.this.subList(fromIndex + offset, toIndex + offset); 457 } 458 459 @Override 460 boolean isPartialView() { 461 return true; 462 } 463 } 464 465 /** 466 * Guaranteed to throw an exception and leave the list unmodified. 467 * 468 * @throws UnsupportedOperationException always 469 * @deprecated Unsupported operation. 470 */ 471 @CanIgnoreReturnValue 472 @Deprecated 473 @Override 474 public final boolean addAll(int index, Collection<? extends E> newElements) { 475 throw new UnsupportedOperationException(); 476 } 477 478 /** 479 * Guaranteed to throw an exception and leave the list unmodified. 480 * 481 * @throws UnsupportedOperationException always 482 * @deprecated Unsupported operation. 483 */ 484 @CanIgnoreReturnValue 485 @Deprecated 486 @Override 487 public final E set(int index, E element) { 488 throw new UnsupportedOperationException(); 489 } 490 491 /** 492 * Guaranteed to throw an exception and leave the list unmodified. 493 * 494 * @throws UnsupportedOperationException always 495 * @deprecated Unsupported operation. 496 */ 497 @Deprecated 498 @Override 499 public final void add(int index, E element) { 500 throw new UnsupportedOperationException(); 501 } 502 503 /** 504 * Guaranteed to throw an exception and leave the list unmodified. 505 * 506 * @throws UnsupportedOperationException always 507 * @deprecated Unsupported operation. 508 */ 509 @CanIgnoreReturnValue 510 @Deprecated 511 @Override 512 public final E remove(int index) { 513 throw new UnsupportedOperationException(); 514 } 515 516 /** 517 * Returns this list instance. 518 * 519 * @since 2.0 520 */ 521 @Override 522 public final ImmutableList<E> asList() { 523 return this; 524 } 525 526 @Override 527 int copyIntoArray(Object[] dst, int offset) { 528 // this loop is faster for RandomAccess instances, which ImmutableLists are 529 int size = size(); 530 for (int i = 0; i < size; i++) { 531 dst[offset + i] = get(i); 532 } 533 return offset + size; 534 } 535 536 /** 537 * Returns a view of this immutable list in reverse order. For example, {@code ImmutableList.of(1, 538 * 2, 3).reverse()} is equivalent to {@code ImmutableList.of(3, 2, 1)}. 539 * 540 * @return a view of this immutable list in reverse order 541 * @since 7.0 542 */ 543 public ImmutableList<E> reverse() { 544 return (size() <= 1) ? this : new ReverseImmutableList<E>(this); 545 } 546 547 private static class ReverseImmutableList<E> extends ImmutableList<E> { 548 private final transient ImmutableList<E> forwardList; 549 550 ReverseImmutableList(ImmutableList<E> backingList) { 551 this.forwardList = backingList; 552 } 553 554 private int reverseIndex(int index) { 555 return (size() - 1) - index; 556 } 557 558 private int reversePosition(int index) { 559 return size() - index; 560 } 561 562 @Override 563 public ImmutableList<E> reverse() { 564 return forwardList; 565 } 566 567 @Override 568 public boolean contains(@NullableDecl Object object) { 569 return forwardList.contains(object); 570 } 571 572 @Override 573 public int indexOf(@NullableDecl Object object) { 574 int index = forwardList.lastIndexOf(object); 575 return (index >= 0) ? reverseIndex(index) : -1; 576 } 577 578 @Override 579 public int lastIndexOf(@NullableDecl Object object) { 580 int index = forwardList.indexOf(object); 581 return (index >= 0) ? reverseIndex(index) : -1; 582 } 583 584 @Override 585 public ImmutableList<E> subList(int fromIndex, int toIndex) { 586 checkPositionIndexes(fromIndex, toIndex, size()); 587 return forwardList.subList(reversePosition(toIndex), reversePosition(fromIndex)).reverse(); 588 } 589 590 @Override 591 public E get(int index) { 592 checkElementIndex(index, size()); 593 return forwardList.get(reverseIndex(index)); 594 } 595 596 @Override 597 public int size() { 598 return forwardList.size(); 599 } 600 601 @Override 602 boolean isPartialView() { 603 return forwardList.isPartialView(); 604 } 605 } 606 607 @Override 608 public boolean equals(@NullableDecl Object obj) { 609 return Lists.equalsImpl(this, obj); 610 } 611 612 @Override 613 public int hashCode() { 614 int hashCode = 1; 615 int n = size(); 616 for (int i = 0; i < n; i++) { 617 hashCode = 31 * hashCode + get(i).hashCode(); 618 619 hashCode = ~~hashCode; 620 // needed to deal with GWT integer overflow 621 } 622 return hashCode; 623 } 624 625 /* 626 * Serializes ImmutableLists as their logical contents. This ensures that 627 * implementation types do not leak into the serialized representation. 628 */ 629 static class SerializedForm implements Serializable { 630 final Object[] elements; 631 632 SerializedForm(Object[] elements) { 633 this.elements = elements; 634 } 635 636 Object readResolve() { 637 return copyOf(elements); 638 } 639 640 private static final long serialVersionUID = 0; 641 } 642 643 private void readObject(ObjectInputStream stream) throws InvalidObjectException { 644 throw new InvalidObjectException("Use SerializedForm"); 645 } 646 647 @Override 648 Object writeReplace() { 649 return new SerializedForm(toArray()); 650 } 651 652 /** 653 * Returns a new builder. The generated builder is equivalent to the builder created by the {@link 654 * Builder} constructor. 655 */ 656 public static <E> Builder<E> builder() { 657 return new Builder<E>(); 658 } 659 660 /** 661 * Returns a new builder, expecting the specified number of elements to be added. 662 * 663 * <p>If {@code expectedSize} is exactly the number of elements added to the builder before {@link 664 * Builder#build} is called, the builder is likely to perform better than an unsized {@link 665 * #builder()} would have. 666 * 667 * <p>It is not specified if any performance benefits apply if {@code expectedSize} is close to, 668 * but not exactly, the number of elements added to the builder. 669 * 670 * @since 23.1 671 */ 672 @Beta 673 public static <E> Builder<E> builderWithExpectedSize(int expectedSize) { 674 checkNonnegative(expectedSize, "expectedSize"); 675 return new ImmutableList.Builder<E>(expectedSize); 676 } 677 678 /** 679 * A builder for creating immutable list instances, especially {@code public static final} lists 680 * ("constant lists"). Example: 681 * 682 * <pre>{@code 683 * public static final ImmutableList<Color> GOOGLE_COLORS 684 * = new ImmutableList.Builder<Color>() 685 * .addAll(WEBSAFE_COLORS) 686 * .add(new Color(0, 191, 255)) 687 * .build(); 688 * }</pre> 689 * 690 * <p>Elements appear in the resulting list in the same order they were added to the builder. 691 * 692 * <p>Builder instances can be reused; it is safe to call {@link #build} multiple times to build 693 * multiple lists in series. Each new list contains all the elements of the ones created before 694 * it. 695 * 696 * @since 2.0 697 */ 698 public static final class Builder<E> extends ImmutableCollection.ArrayBasedBuilder<E> { 699 /** 700 * Creates a new builder. The returned builder is equivalent to the builder generated by {@link 701 * ImmutableList#builder}. 702 */ 703 public Builder() { 704 this(DEFAULT_INITIAL_CAPACITY); 705 } 706 707 Builder(int capacity) { 708 super(capacity); 709 } 710 711 /** 712 * Adds {@code element} to the {@code ImmutableList}. 713 * 714 * @param element the element to add 715 * @return this {@code Builder} object 716 * @throws NullPointerException if {@code element} is null 717 */ 718 @CanIgnoreReturnValue 719 @Override 720 public Builder<E> add(E element) { 721 super.add(element); 722 return this; 723 } 724 725 /** 726 * Adds each element of {@code elements} to the {@code ImmutableList}. 727 * 728 * @param elements the {@code Iterable} to add to the {@code ImmutableList} 729 * @return this {@code Builder} object 730 * @throws NullPointerException if {@code elements} is null or contains a null element 731 */ 732 @CanIgnoreReturnValue 733 @Override 734 public Builder<E> add(E... elements) { 735 super.add(elements); 736 return this; 737 } 738 739 /** 740 * Adds each element of {@code elements} to the {@code ImmutableList}. 741 * 742 * @param elements the {@code Iterable} to add to the {@code ImmutableList} 743 * @return this {@code Builder} object 744 * @throws NullPointerException if {@code elements} is null or contains a null element 745 */ 746 @CanIgnoreReturnValue 747 @Override 748 public Builder<E> addAll(Iterable<? extends E> elements) { 749 super.addAll(elements); 750 return this; 751 } 752 753 /** 754 * Adds each element of {@code elements} to the {@code ImmutableList}. 755 * 756 * @param elements the {@code Iterable} to add to the {@code ImmutableList} 757 * @return this {@code Builder} object 758 * @throws NullPointerException if {@code elements} is null or contains a null element 759 */ 760 @CanIgnoreReturnValue 761 @Override 762 public Builder<E> addAll(Iterator<? extends E> elements) { 763 super.addAll(elements); 764 return this; 765 } 766 767 /** 768 * Returns a newly-created {@code ImmutableList} based on the contents of the {@code Builder}. 769 */ 770 @Override 771 public ImmutableList<E> build() { 772 forceCopy = true; 773 return asImmutableList(contents, size); 774 } 775 } 776}