1/*
2 * Copyright (C) 2008 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.io.Serializable;
22import java.util.AbstractCollection;
23import java.util.Collection;
24import java.util.Collections;
25import java.util.Iterator;
26
27import javax.annotation.Nullable;
28
29/**
30 * GWT emulated version of {@link ImmutableCollection}.
31 *
32 * @author Jesse Wilson
33 */
34@SuppressWarnings("serial") // we're overriding default serialization
35public abstract class ImmutableCollection<E> extends AbstractCollection<E>
36    implements Serializable {
37
38  static final ImmutableCollection<Object> EMPTY_IMMUTABLE_COLLECTION
39      = new ForwardingImmutableCollection<Object>(Collections.emptyList());
40
41  ImmutableCollection() {}
42
43  public abstract UnmodifiableIterator<E> iterator();
44
45  public boolean contains(@Nullable Object object) {
46    return object != null && super.contains(object);
47  }
48
49  public final boolean add(E e) {
50    throw new UnsupportedOperationException();
51  }
52
53  public final boolean remove(Object object) {
54    throw new UnsupportedOperationException();
55  }
56
57  public final boolean addAll(Collection<? extends E> newElements) {
58    throw new UnsupportedOperationException();
59  }
60
61  public final boolean removeAll(Collection<?> oldElements) {
62    throw new UnsupportedOperationException();
63  }
64
65  public final boolean retainAll(Collection<?> elementsToKeep) {
66    throw new UnsupportedOperationException();
67  }
68
69  public final void clear() {
70    throw new UnsupportedOperationException();
71  }
72
73  private transient ImmutableList<E> asList;
74
75  public ImmutableList<E> asList() {
76    ImmutableList<E> list = asList;
77    return (list == null) ? (asList = createAsList()) : list;
78  }
79
80  ImmutableList<E> createAsList() {
81    switch (size()) {
82      case 0:
83        return ImmutableList.of();
84      case 1:
85        return ImmutableList.of(iterator().next());
86      default:
87        return new RegularImmutableAsList<E>(this, toArray());
88    }
89  }
90  static <E> ImmutableCollection<E> unsafeDelegate(Collection<E> delegate) {
91    return new ForwardingImmutableCollection<E>(delegate);
92  }
93
94  boolean isPartialView() {
95    return false;
96  }
97
98  /**
99   * GWT emulated version of {@link ImmutableCollection.Builder}.
100   */
101  public abstract static class Builder<E> {
102
103    Builder() {}
104
105    public abstract Builder<E> add(E element);
106
107    public Builder<E> add(E... elements) {
108      checkNotNull(elements); // for GWT
109      for (E element : elements) {
110        add(checkNotNull(element));
111      }
112      return this;
113    }
114
115    public Builder<E> addAll(Iterable<? extends E> elements) {
116      checkNotNull(elements); // for GWT
117      for (E element : elements) {
118        add(checkNotNull(element));
119      }
120      return this;
121    }
122
123    public Builder<E> addAll(Iterator<? extends E> elements) {
124      checkNotNull(elements); // for GWT
125      while (elements.hasNext()) {
126        add(checkNotNull(elements.next()));
127      }
128      return this;
129    }
130
131    public abstract ImmutableCollection<E> build();
132  }
133}
134