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