1/*
2 * Copyright (C) 2009 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied. See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15package com.google.common.collect;
16
17import com.google.common.base.Preconditions;
18
19import java.util.Comparator;
20
21import javax.annotation.Nullable;
22
23/**
24 * List returned by {@code ImmutableSortedSet.asList()} when the set isn't empty.
25 *
26 * @author Jared Levy
27 * @author Louis Wasserman
28 */
29@SuppressWarnings("serial")
30final class ImmutableSortedAsList<E> extends ImmutableList<E> implements SortedIterable<E> {
31  private final transient ImmutableSortedSet<E> backingSet;
32  private final transient ImmutableList<E> backingList;
33
34  ImmutableSortedAsList(
35      ImmutableSortedSet<E> backingSet, ImmutableList<E> backingList) {
36    this.backingSet = backingSet;
37    this.backingList = backingList;
38  }
39
40  @Override public Comparator<? super E> comparator() {
41    return backingSet.comparator();
42  }
43
44  // Override contains(), indexOf(), and lastIndexOf() to be O(log N) instead of O(N).
45
46  @Override public boolean contains(@Nullable Object target) {
47    // TODO: why not contains(target)?
48    return backingSet.indexOf(target) >= 0;
49  }
50
51  @Override public int indexOf(@Nullable Object target) {
52    return backingSet.indexOf(target);
53  }
54
55  @Override public int lastIndexOf(@Nullable Object target) {
56    return backingSet.indexOf(target);
57  }
58
59  // The returned ImmutableSortedAsList maintains the contains(), indexOf(), and
60  // lastIndexOf() performance benefits.
61  @Override public ImmutableList<E> subList(int fromIndex, int toIndex) {
62    Preconditions.checkPositionIndexes(fromIndex, toIndex, size());
63    return (fromIndex == toIndex) ? ImmutableList.<E>of()
64        : new RegularImmutableSortedSet<E>(
65            backingList.subList(fromIndex, toIndex), backingSet.comparator())
66            .asList();
67  }
68
69  // The ImmutableAsList serialized form has the correct behavior.
70  @Override Object writeReplace() {
71    return new ImmutableAsList.SerializedForm(backingSet);
72  }
73
74  @Override public UnmodifiableIterator<E> iterator() {
75    return backingList.iterator();
76  }
77
78  @Override public E get(int index) {
79    return backingList.get(index);
80  }
81
82  @Override public UnmodifiableListIterator<E> listIterator() {
83    return backingList.listIterator();
84  }
85
86  @Override public UnmodifiableListIterator<E> listIterator(int index) {
87    return backingList.listIterator(index);
88  }
89
90  @Override public int size() {
91    return backingList.size();
92  }
93
94  @Override public boolean equals(@Nullable Object obj) {
95    return backingList.equals(obj);
96  }
97
98  @Override public int hashCode() {
99    return backingList.hashCode();
100  }
101
102  @Override boolean isPartialView() {
103    return backingList.isPartialView();
104  }
105}
106