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