1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 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 static com.google.common.base.Preconditions.checkElementIndex;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkPositionIndex;
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkPositionIndexes;
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible;
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection;
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List;
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.NoSuchElementException;
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * An empty immutable list.
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(serializable = true, emulated = true)
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonfinal class EmptyImmutableList extends ImmutableList<Object> {
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static final EmptyImmutableList INSTANCE = new EmptyImmutableList();
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static final UnmodifiableListIterator<Object> ITERATOR =
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      new UnmodifiableListIterator<Object>() {
411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public boolean hasNext() {
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          return false;
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public boolean hasPrevious() {
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          return false;
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public Object next() {
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          throw new NoSuchElementException();
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public int nextIndex() {
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          return 0;
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public Object previous() {
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          throw new NoSuchElementException();
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public int previousIndex() {
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          return -1;
641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      };
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private EmptyImmutableList() {}
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public int size() {
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return 0;
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean isEmpty() {
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return true;
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override boolean isPartialView() {
791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return false;
801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean contains(Object target) {
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return false;
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public UnmodifiableIterator<Object> iterator() {
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return Iterators.emptyIterator();
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final Object[] EMPTY_ARRAY = new Object[0];
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public Object[] toArray() {
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return EMPTY_ARRAY;
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public <T> T[] toArray(T[] a) {
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (a.length > 0) {
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      a[0] = null;
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return a;
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public Object get(int index) {
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // guaranteed to fail, but at least we get a consistent message
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkElementIndex(index, 0);
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new AssertionError("unreachable");
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override public int indexOf(@Nullable Object target) {
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return -1;
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override public int lastIndexOf(@Nullable Object target) {
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return -1;
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableList<Object> subList(int fromIndex, int toIndex) {
119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkPositionIndexes(fromIndex, toIndex, 0);
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return this;
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override public ImmutableList<Object> reverse() {
1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override public UnmodifiableListIterator<Object> listIterator(){
1281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return ITERATOR;
129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override public UnmodifiableListIterator<Object> listIterator(int start) {
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkPositionIndex(start, 0);
1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return ITERATOR;
134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean containsAll(Collection<?> targets) {
137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return targets.isEmpty();
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean equals(@Nullable Object object) {
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (object instanceof List) {
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      List<?> that = (List<?>) object;
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return that.isEmpty();
144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return false;
146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public int hashCode() {
149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return 1;
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public String toString() {
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return "[]";
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  Object readResolve() {
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return INSTANCE; // preserve singleton property
158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final long serialVersionUID = 0;
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
162