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#singletonList}, 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, "the total number of elements must fit in an int"); 189 Object[] array = new Object[12 + others.length]; 190 array[0] = e1; 191 array[1] = e2; 192 array[2] = e3; 193 array[3] = e4; 194 array[4] = e5; 195 array[5] = e6; 196 array[6] = e7; 197 array[7] = e8; 198 array[8] = e9; 199 array[9] = e10; 200 array[10] = e11; 201 array[11] = e12; 202 System.arraycopy(others, 0, array, 12, others.length); 203 return construct(array); 204 } 205 206 /** 207 * Returns an immutable list containing the given elements, in order. If {@code elements} is a 208 * {@link Collection}, this method behaves exactly as {@link #copyOf(Collection)}; otherwise, it 209 * behaves exactly as {@code copyOf(elements.iterator()}. 210 * 211 * @throws NullPointerException if {@code elements} contains a null element 212 */ 213 public static <E> ImmutableList<E> copyOf(Iterable<? extends E> elements) { 214 checkNotNull(elements); // TODO(kevinb): is this here only for GWT? 215 return (elements instanceof Collection) 216 ? copyOf((Collection<? extends E>) elements) 217 : copyOf(elements.iterator()); 218 } 219 220 /** 221 * Returns an immutable list containing the given elements, in order. 222 * 223 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is 224 * safe to do so. The exact circumstances under which a copy will or will not be performed are 225 * undocumented and subject to change. 226 * 227 * <p>Note that if {@code list} is a {@code List<String>}, then {@code ImmutableList.copyOf(list)} 228 * returns an {@code ImmutableList<String>} containing each of the strings in {@code list}, while 229 * ImmutableList.of(list)} returns an {@code ImmutableList<List<String>>} containing one element 230 * (the given list itself). 231 * 232 * <p>This method is safe to use even when {@code elements} is a synchronized or concurrent 233 * collection that is currently being modified by another thread. 234 * 235 * @throws NullPointerException if {@code elements} contains a null element 236 */ 237 public static <E> ImmutableList<E> copyOf(Collection<? extends E> elements) { 238 if (elements instanceof ImmutableCollection) { 239 @SuppressWarnings("unchecked") // all supported methods are covariant 240 ImmutableList<E> list = ((ImmutableCollection<E>) elements).asList(); 241 return list.isPartialView() ? ImmutableList.<E>asImmutableList(list.toArray()) : list; 242 } 243 return construct(elements.toArray()); 244 } 245 246 /** 247 * Returns an immutable list containing the given elements, in order. 248 * 249 * @throws NullPointerException if {@code elements} contains a null element 250 */ 251 public static <E> ImmutableList<E> copyOf(Iterator<? extends E> elements) { 252 // We special-case for 0 or 1 elements, but going further is madness. 253 if (!elements.hasNext()) { 254 return of(); 255 } 256 E first = elements.next(); 257 if (!elements.hasNext()) { 258 return of(first); 259 } else { 260 return new ImmutableList.Builder<E>().add(first).addAll(elements).build(); 261 } 262 } 263 264 /** 265 * Returns an immutable list containing the given elements, in order. 266 * 267 * @throws NullPointerException if {@code elements} contains a null element 268 * @since 3.0 269 */ 270 public static <E> ImmutableList<E> copyOf(E[] elements) { 271 return (elements.length == 0) 272 ? ImmutableList.<E>of() 273 : ImmutableList.<E>construct(elements.clone()); 274 } 275 276 /** 277 * Returns an immutable list containing the given elements, sorted according to their natural 278 * order. The sorting algorithm used is stable, so elements that compare as equal will stay in the 279 * order in which they appear in the input. 280 * 281 * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code 282 * ImmutableSortedSet.copyOf(elements)}; if you want a {@code List} you can use its {@code 283 * asList()} view. 284 * 285 * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted 286 * {@code ImmutableList}, use {@code stream.sorted().collect(toImmutableList())}. 287 * 288 * @throws NullPointerException if any element in the input is null 289 * @since 21.0 290 */ 291 public static <E extends Comparable<? super E>> ImmutableList<E> sortedCopyOf( 292 Iterable<? extends E> elements) { 293 Comparable<?>[] array = Iterables.toArray(elements, new Comparable<?>[0]); 294 checkElementsNotNull((Object[]) array); 295 Arrays.sort(array); 296 return asImmutableList(array); 297 } 298 299 /** 300 * Returns an immutable list containing the given elements, in sorted order relative to the 301 * specified comparator. The sorting algorithm used is stable, so elements that compare as equal 302 * will stay in the order in which they appear in the input. 303 * 304 * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code 305 * ImmutableSortedSet.copyOf(comparator, elements)}; if you want a {@code List} you can use its 306 * {@code asList()} view. 307 * 308 * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted 309 * {@code ImmutableList}, use {@code stream.sorted(comparator).collect(toImmutableList())}. 310 * 311 * @throws NullPointerException if any element in the input is null 312 * @since 21.0 313 */ 314 public static <E> ImmutableList<E> sortedCopyOf( 315 Comparator<? super E> comparator, Iterable<? extends E> elements) { 316 checkNotNull(comparator); 317 @SuppressWarnings("unchecked") // all supported methods are covariant 318 E[] array = (E[]) Iterables.toArray(elements); 319 checkElementsNotNull(array); 320 Arrays.sort(array, comparator); 321 return asImmutableList(array); 322 } 323 324 /** Views the array as an immutable list. Checks for nulls; does not copy. */ 325 private static <E> ImmutableList<E> construct(Object... elements) { 326 return asImmutableList(checkElementsNotNull(elements)); 327 } 328 329 /** 330 * Views the array as an immutable list. Does not check for nulls; does not copy. 331 * 332 * <p>The array must be internally created. 333 */ 334 static <E> ImmutableList<E> asImmutableList(Object[] elements) { 335 return asImmutableList(elements, elements.length); 336 } 337 338 /** Views the array as an immutable list. Does not check for nulls. */ 339 static <E> ImmutableList<E> asImmutableList(Object[] elements, int length) { 340 if (length == 0) { 341 return of(); 342 } 343 return new RegularImmutableList<E>(elements, length); 344 } 345 346 ImmutableList() {} 347 348 // This declaration is needed to make List.iterator() and 349 // ImmutableCollection.iterator() consistent. 350 @Override 351 public UnmodifiableIterator<E> iterator() { 352 return listIterator(); 353 } 354 355 @Override 356 public UnmodifiableListIterator<E> listIterator() { 357 return listIterator(0); 358 } 359 360 @SuppressWarnings("unchecked") 361 @Override 362 public UnmodifiableListIterator<E> listIterator(int index) { 363 checkPositionIndex(index, size()); 364 if (isEmpty()) { 365 return (UnmodifiableListIterator<E>) EMPTY_ITR; 366 } else { 367 return new Itr<E>(this, index); 368 } 369 } 370 371 /** A singleton implementation of iterator() for the empty ImmutableList. */ 372 private static final UnmodifiableListIterator<Object> EMPTY_ITR = 373 new Itr<Object>(RegularImmutableList.EMPTY, 0); 374 375 static class Itr<E> extends AbstractIndexedListIterator<E> { 376 private final ImmutableList<E> list; 377 378 Itr(ImmutableList<E> list, int index) { 379 super(list.size(), index); 380 this.list = list; 381 } 382 383 @Override 384 protected E get(int index) { 385 return list.get(index); 386 } 387 } 388 389 @Override 390 public int indexOf(@NullableDecl Object object) { 391 return (object == null) ? -1 : Lists.indexOfImpl(this, object); 392 } 393 394 @Override 395 public int lastIndexOf(@NullableDecl Object object) { 396 return (object == null) ? -1 : Lists.lastIndexOfImpl(this, object); 397 } 398 399 @Override 400 public boolean contains(@NullableDecl Object object) { 401 return indexOf(object) >= 0; 402 } 403 404 // constrain the return type to ImmutableList<E> 405 406 /** 407 * Returns an immutable list of the elements between the specified {@code fromIndex}, inclusive, 408 * and {@code toIndex}, exclusive. (If {@code fromIndex} and {@code toIndex} are equal, the empty 409 * immutable list is returned.) 410 */ 411 @Override 412 public ImmutableList<E> subList(int fromIndex, int toIndex) { 413 checkPositionIndexes(fromIndex, toIndex, size()); 414 int length = toIndex - fromIndex; 415 if (length == size()) { 416 return this; 417 } else if (length == 0) { 418 return of(); 419 } else { 420 return subListUnchecked(fromIndex, toIndex); 421 } 422 } 423 424 /** 425 * Called by the default implementation of {@link #subList} when {@code toIndex - fromIndex > 1}, 426 * after index validation has already been performed. 427 */ 428 ImmutableList<E> subListUnchecked(int fromIndex, int toIndex) { 429 return new SubList(fromIndex, toIndex - fromIndex); 430 } 431 432 class SubList extends ImmutableList<E> { 433 final transient int offset; 434 final transient int length; 435 436 SubList(int offset, int length) { 437 this.offset = offset; 438 this.length = length; 439 } 440 441 @Override 442 public int size() { 443 return length; 444 } 445 446 @Override 447 Object[] internalArray() { 448 return ImmutableList.this.internalArray(); 449 } 450 451 @Override 452 int internalArrayStart() { 453 return ImmutableList.this.internalArrayStart() + offset; 454 } 455 456 @Override 457 int internalArrayEnd() { 458 return ImmutableList.this.internalArrayStart() + offset + length; 459 } 460 461 @Override 462 public E get(int index) { 463 checkElementIndex(index, length); 464 return ImmutableList.this.get(index + offset); 465 } 466 467 @Override 468 public ImmutableList<E> subList(int fromIndex, int toIndex) { 469 checkPositionIndexes(fromIndex, toIndex, length); 470 return ImmutableList.this.subList(fromIndex + offset, toIndex + offset); 471 } 472 473 @Override 474 boolean isPartialView() { 475 return true; 476 } 477 } 478 479 /** 480 * Guaranteed to throw an exception and leave the list unmodified. 481 * 482 * @throws UnsupportedOperationException always 483 * @deprecated Unsupported operation. 484 */ 485 @CanIgnoreReturnValue 486 @Deprecated 487 @Override 488 public final boolean addAll(int index, Collection<? extends E> newElements) { 489 throw new UnsupportedOperationException(); 490 } 491 492 /** 493 * Guaranteed to throw an exception and leave the list unmodified. 494 * 495 * @throws UnsupportedOperationException always 496 * @deprecated Unsupported operation. 497 */ 498 @CanIgnoreReturnValue 499 @Deprecated 500 @Override 501 public final E set(int index, E element) { 502 throw new UnsupportedOperationException(); 503 } 504 505 /** 506 * Guaranteed to throw an exception and leave the list unmodified. 507 * 508 * @throws UnsupportedOperationException always 509 * @deprecated Unsupported operation. 510 */ 511 @Deprecated 512 @Override 513 public final void add(int index, E element) { 514 throw new UnsupportedOperationException(); 515 } 516 517 /** 518 * Guaranteed to throw an exception and leave the list unmodified. 519 * 520 * @throws UnsupportedOperationException always 521 * @deprecated Unsupported operation. 522 */ 523 @CanIgnoreReturnValue 524 @Deprecated 525 @Override 526 public final E remove(int index) { 527 throw new UnsupportedOperationException(); 528 } 529 530 /** 531 * Returns this list instance. 532 * 533 * @since 2.0 534 */ 535 @Override 536 public final ImmutableList<E> asList() { 537 return this; 538 } 539 540 @Override 541 int copyIntoArray(Object[] dst, int offset) { 542 // this loop is faster for RandomAccess instances, which ImmutableLists are 543 int size = size(); 544 for (int i = 0; i < size; i++) { 545 dst[offset + i] = get(i); 546 } 547 return offset + size; 548 } 549 550 /** 551 * Returns a view of this immutable list in reverse order. For example, {@code ImmutableList.of(1, 552 * 2, 3).reverse()} is equivalent to {@code ImmutableList.of(3, 2, 1)}. 553 * 554 * @return a view of this immutable list in reverse order 555 * @since 7.0 556 */ 557 public ImmutableList<E> reverse() { 558 return (size() <= 1) ? this : new ReverseImmutableList<E>(this); 559 } 560 561 private static class ReverseImmutableList<E> extends ImmutableList<E> { 562 private final transient ImmutableList<E> forwardList; 563 564 ReverseImmutableList(ImmutableList<E> backingList) { 565 this.forwardList = backingList; 566 } 567 568 private int reverseIndex(int index) { 569 return (size() - 1) - index; 570 } 571 572 private int reversePosition(int index) { 573 return size() - index; 574 } 575 576 @Override 577 public ImmutableList<E> reverse() { 578 return forwardList; 579 } 580 581 @Override 582 public boolean contains(@NullableDecl Object object) { 583 return forwardList.contains(object); 584 } 585 586 @Override 587 public int indexOf(@NullableDecl Object object) { 588 int index = forwardList.lastIndexOf(object); 589 return (index >= 0) ? reverseIndex(index) : -1; 590 } 591 592 @Override 593 public int lastIndexOf(@NullableDecl Object object) { 594 int index = forwardList.indexOf(object); 595 return (index >= 0) ? reverseIndex(index) : -1; 596 } 597 598 @Override 599 public ImmutableList<E> subList(int fromIndex, int toIndex) { 600 checkPositionIndexes(fromIndex, toIndex, size()); 601 return forwardList.subList(reversePosition(toIndex), reversePosition(fromIndex)).reverse(); 602 } 603 604 @Override 605 public E get(int index) { 606 checkElementIndex(index, size()); 607 return forwardList.get(reverseIndex(index)); 608 } 609 610 @Override 611 public int size() { 612 return forwardList.size(); 613 } 614 615 @Override 616 boolean isPartialView() { 617 return forwardList.isPartialView(); 618 } 619 } 620 621 @Override 622 public boolean equals(@NullableDecl Object obj) { 623 return Lists.equalsImpl(this, obj); 624 } 625 626 @Override 627 public int hashCode() { 628 int hashCode = 1; 629 int n = size(); 630 for (int i = 0; i < n; i++) { 631 hashCode = 31 * hashCode + get(i).hashCode(); 632 633 hashCode = ~~hashCode; 634 // needed to deal with GWT integer overflow 635 } 636 return hashCode; 637 } 638 639 /* 640 * Serializes ImmutableLists as their logical contents. This ensures that 641 * implementation types do not leak into the serialized representation. 642 */ 643 static class SerializedForm implements Serializable { 644 final Object[] elements; 645 646 SerializedForm(Object[] elements) { 647 this.elements = elements; 648 } 649 650 Object readResolve() { 651 return copyOf(elements); 652 } 653 654 private static final long serialVersionUID = 0; 655 } 656 657 private void readObject(ObjectInputStream stream) throws InvalidObjectException { 658 throw new InvalidObjectException("Use SerializedForm"); 659 } 660 661 @Override 662 Object writeReplace() { 663 return new SerializedForm(toArray()); 664 } 665 666 /** 667 * Returns a new builder. The generated builder is equivalent to the builder created by the {@link 668 * Builder} constructor. 669 */ 670 public static <E> Builder<E> builder() { 671 return new Builder<E>(); 672 } 673 674 /** 675 * Returns a new builder, expecting the specified number of elements to be added. 676 * 677 * <p>If {@code expectedSize} is exactly the number of elements added to the builder before {@link 678 * Builder#build} is called, the builder is likely to perform better than an unsized {@link 679 * #builder()} would have. 680 * 681 * <p>It is not specified if any performance benefits apply if {@code expectedSize} is close to, 682 * but not exactly, the number of elements added to the builder. 683 * 684 * @since 23.1 685 */ 686 @Beta 687 public static <E> Builder<E> builderWithExpectedSize(int expectedSize) { 688 checkNonnegative(expectedSize, "expectedSize"); 689 return new ImmutableList.Builder<E>(expectedSize); 690 } 691 692 /** 693 * A builder for creating immutable list instances, especially {@code public static final} lists 694 * ("constant lists"). Example: 695 * 696 * <pre>{@code 697 * public static final ImmutableList<Color> GOOGLE_COLORS 698 * = new ImmutableList.Builder<Color>() 699 * .addAll(WEBSAFE_COLORS) 700 * .add(new Color(0, 191, 255)) 701 * .build(); 702 * }</pre> 703 * 704 * <p>Elements appear in the resulting list in the same order they were added to the builder. 705 * 706 * <p>Builder instances can be reused; it is safe to call {@link #build} multiple times to build 707 * multiple lists in series. Each new list contains all the elements of the ones created before 708 * it. 709 * 710 * @since 2.0 711 */ 712 public static final class Builder<E> extends ImmutableCollection.ArrayBasedBuilder<E> { 713 /** 714 * Creates a new builder. The returned builder is equivalent to the builder generated by {@link 715 * ImmutableList#builder}. 716 */ 717 public Builder() { 718 this(DEFAULT_INITIAL_CAPACITY); 719 } 720 721 Builder(int capacity) { 722 super(capacity); 723 } 724 725 /** 726 * Adds {@code element} to the {@code ImmutableList}. 727 * 728 * @param element the element to add 729 * @return this {@code Builder} object 730 * @throws NullPointerException if {@code element} is null 731 */ 732 @CanIgnoreReturnValue 733 @Override 734 public Builder<E> add(E element) { 735 super.add(element); 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> add(E... elements) { 749 super.add(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(Iterable<? extends E> elements) { 763 super.addAll(elements); 764 return this; 765 } 766 767 /** 768 * Adds each element of {@code elements} to the {@code ImmutableList}. 769 * 770 * @param elements the {@code Iterator} to add to the {@code ImmutableList} 771 * @return this {@code Builder} object 772 * @throws NullPointerException if {@code elements} is null or contains a null element 773 */ 774 @CanIgnoreReturnValue 775 @Override 776 public Builder<E> addAll(Iterator<? extends E> elements) { 777 super.addAll(elements); 778 return this; 779 } 780 781 @CanIgnoreReturnValue 782 Builder<E> combine(Builder<E> other) { 783 addAll(other.contents, other.size); 784 return this; 785 } 786 787 /** 788 * Returns a newly-created {@code ImmutableList} based on the contents of the {@code Builder}. 789 */ 790 @Override 791 public ImmutableList<E> build() { 792 forceCopy = true; 793 return asImmutableList(contents, size); 794 } 795 } 796}