1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2008 The Guava Authors
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable;
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection;
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator;
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * An immutable collection. Does not permit null elements.
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
30bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * <p>In addition to the {@link Collection} methods, this class has an {@link
31bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * #asList()} method, which returns a list view of the collection's elements.
32bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor *
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Note:</b> Although this class is not final, it cannot be subclassed
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * outside of this package as it has no public or protected constructors. Thus,
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * instances of this type are guaranteed to be immutable.
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jesse Wilson
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true)
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@SuppressWarnings("serial") // we're overriding default serialization
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic abstract class ImmutableCollection<E>
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    implements Collection<E>, Serializable {
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static final ImmutableCollection<Object> EMPTY_IMMUTABLE_COLLECTION
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      = new EmptyImmutableCollection();
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  ImmutableCollection() {}
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an unmodifiable iterator across the elements in this collection.
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public abstract UnmodifiableIterator<E> iterator();
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public Object[] toArray() {
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return ObjectArrays.toArrayImpl(this);
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public <T> T[] toArray(T[] other) {
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return ObjectArrays.toArrayImpl(this, other);
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public boolean contains(@Nullable Object object) {
671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return object != null && Iterators.contains(iterator(), object);
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public boolean containsAll(Collection<?> targets) {
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return Collections2.containsAllImpl(this, targets);
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public boolean isEmpty() {
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return size() == 0;
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public String toString() {
811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return Collections2.toStringImpl(this);
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the collection unmodified.
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public final boolean add(E e) {
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the collection unmodified.
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public final boolean remove(Object object) {
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the collection unmodified.
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public final boolean addAll(Collection<? extends E> newElements) {
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the collection unmodified.
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public final boolean removeAll(Collection<?> oldElements) {
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the collection unmodified.
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public final boolean retainAll(Collection<?> elementsToKeep) {
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the collection unmodified.
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public final void clear() {
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /*
1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * TODO(kevinb): Restructure code so ImmutableList doesn't contain this
1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * variable, which it doesn't use.
1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
148bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  private transient ImmutableList<E> asList;
149bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
150bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  /**
151bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * Returns a list view of the collection.
152bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
1531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
154bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   */
155bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  public ImmutableList<E> asList() {
156bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    ImmutableList<E> list = asList;
157bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    return (list == null) ? (asList = createAsList()) : list;
158bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
159bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
160bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  ImmutableList<E> createAsList() {
161bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    switch (size()) {
162bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      case 0:
163bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return ImmutableList.of();
164bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      case 1:
165bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return ImmutableList.of(iterator().next());
166bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      default:
167bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return new ImmutableAsList<E>(toArray(), this);
168bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    }
169bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
170bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  abstract boolean isPartialView();
1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class EmptyImmutableCollection
174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      extends ImmutableCollection<Object> {
1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public int size() {
177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return 0;
178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean isEmpty() {
181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return true;
182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean contains(@Nullable Object object) {
185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public UnmodifiableIterator<Object> iterator() {
189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Iterators.EMPTY_ITERATOR;
190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final Object[] EMPTY_ARRAY = new Object[0];
193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Object[] toArray() {
195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return EMPTY_ARRAY;
196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public <T> T[] toArray(T[] array) {
199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (array.length > 0) {
200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        array[0] = null;
201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return array;
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override ImmutableList<Object> createAsList() {
2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return ImmutableList.of();
2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override boolean isPartialView() {
2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return false;
2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Nonempty collection stored in an array.
2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class ArrayImmutableCollection<E>
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      extends ImmutableCollection<E> {
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final E[] elements;
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ArrayImmutableCollection(E[] elements) {
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.elements = elements;
223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public int size() {
227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return elements.length;
228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean isEmpty() {
231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public UnmodifiableIterator<E> iterator() {
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Iterators.forArray(elements);
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override ImmutableList<E> createAsList() {
2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return elements.length == 1 ? new SingletonImmutableList<E>(elements[0])
2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          : new RegularImmutableList<E>(elements);
2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override boolean isPartialView() {
2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return false;
2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /*
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Serializes ImmutableCollections as their logical contents. This ensures
250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * that implementation types do not leak into the serialized representation.
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class SerializedForm implements Serializable {
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final Object[] elements;
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    SerializedForm(Object[] elements) {
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.elements = elements;
256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Object readResolve() {
258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return elements.length == 0
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          ? EMPTY_IMMUTABLE_COLLECTION
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          : new ArrayImmutableCollection<Object>(Platform.clone(elements));
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  Object writeReplace() {
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new SerializedForm(toArray());
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Abstract base class for builders of {@link ImmutableCollection} types.
2711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public abstract static class Builder<E> {
2751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Builder() {
2771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
2781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Adds {@code element} to the {@code ImmutableCollection} being built.
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * <p>Note that each builder class covariantly returns its own type from
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * this method.
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @param element the element to add
286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @return this {@code Builder} instance
287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @throws NullPointerException if {@code element} is null
288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public abstract Builder<E> add(E element);
290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Adds each element of {@code elements} to the {@code ImmutableCollection}
293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * being built.
294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * <p>Note that each builder class overrides this method in order to
296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * covariantly return its own type.
297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @param elements the elements to add
299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @return this {@code Builder} instance
300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @throws NullPointerException if {@code elements} is null or contains a
301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *     null element
302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Builder<E> add(E... elements) {
304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      for (E element : elements) {
305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        add(element);
306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Adds each element of {@code elements} to the {@code ImmutableCollection}
312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * being built.
313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * <p>Note that each builder class overrides this method in order to
315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * covariantly return its own type.
316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @param elements the elements to add
318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @return this {@code Builder} instance
319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @throws NullPointerException if {@code elements} is null or contains a
320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *     null element
321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Builder<E> addAll(Iterable<? extends E> elements) {
323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      for (E element : elements) {
324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        add(element);
325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Adds each element of {@code elements} to the {@code ImmutableCollection}
331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * being built.
332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * <p>Note that each builder class overrides this method in order to
334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * covariantly return its own type.
335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @param elements the elements to add
337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @return this {@code Builder} instance
338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @throws NullPointerException if {@code elements} is null or contains a
339090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *     null element
340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Builder<E> addAll(Iterator<? extends E> elements) {
342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      while (elements.hasNext()) {
343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        add(elements.next());
344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Returns a newly-created {@code ImmutableCollection} of the appropriate
350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * type, containing the elements provided to this builder.
351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * <p>Note that each builder class covariantly returns the appropriate type
353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * of {@code ImmutableCollection} from this method.
354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public abstract ImmutableCollection<E> build();
356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
358