1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 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
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkNotNull;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Preconditions;
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Implementation of {@link ImmutableList} with exactly one element.
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Hayward Chan
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(serializable = true, emulated = true)
340888a09821a98ac0680fad765217302858e70fa4Paul Duffin@SuppressWarnings("serial") // uses writeReplace(), not default serialization
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonfinal class SingletonImmutableList<E> extends ImmutableList<E> {
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  final transient E element;
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  SingletonImmutableList(E element) {
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.element = checkNotNull(element);
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
430888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public E get(int index) {
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Preconditions.checkElementIndex(index, 1);
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return element;
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
490888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public int indexOf(@Nullable Object object) {
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return element.equals(object) ? 0 : -1;
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
530888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public UnmodifiableIterator<E> iterator() {
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return Iterators.singletonIterator(element);
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
570888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public int lastIndexOf(@Nullable Object object) {
587dd252788645e940eada959bdde927426e2531c9Paul Duffin    return indexOf(object);
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
610888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public int size() {
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return 1;
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
660888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public ImmutableList<E> subList(int fromIndex, int toIndex) {
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Preconditions.checkPositionIndexes(fromIndex, toIndex, 1);
680888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return (fromIndex == toIndex) ? ImmutableList.<E>of() : this;
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
710888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public ImmutableList<E> reverse() {
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return this;
731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
750888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public boolean contains(@Nullable Object object) {
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return element.equals(object);
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
790888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public boolean equals(@Nullable Object object) {
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (object == this) {
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return true;
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (object instanceof List) {
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      List<?> that = (List<?>) object;
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return that.size() == 1 && element.equals(that.get(0));
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return false;
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
900888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public int hashCode() {
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // not caching hash code since it could change if the element is mutable
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // in a way that modifies its hash code.
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return 31 + element.hashCode();
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
960888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public String toString() {
971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    String elementToString = element.toString();
980888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new StringBuilder(elementToString.length() + 2)
990888a09821a98ac0680fad765217302858e70fa4Paul Duffin        .append('[')
1000888a09821a98ac0680fad765217302858e70fa4Paul Duffin        .append(elementToString)
1010888a09821a98ac0680fad765217302858e70fa4Paul Duffin        .append(']')
1020888a09821a98ac0680fad765217302858e70fa4Paul Duffin        .toString();
1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1050888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override public boolean isEmpty() {
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return false;
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1090888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override boolean isPartialView() {
1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return false;
1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1137dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Override
1140888a09821a98ac0680fad765217302858e70fa4Paul Duffin  int copyIntoArray(Object[] dst, int offset) {
1150888a09821a98ac0680fad765217302858e70fa4Paul Duffin    dst[offset] = element;
1160888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return offset + 1;
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
119