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