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