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 com.google.common.annotations.GwtCompatible;
20
21import java.util.Collection;
22import java.util.Comparator;
23import java.util.NoSuchElementException;
24import java.util.Set;
25
26import javax.annotation.Nullable;
27
28/**
29 * An empty immutable sorted set.
30 *
31 * @author Jared Levy
32 */
33@GwtCompatible(serializable = true, emulated = true)
34@SuppressWarnings("serial") // uses writeReplace(), not default serialization
35class EmptyImmutableSortedSet<E> extends ImmutableSortedSet<E> {
36  EmptyImmutableSortedSet(Comparator<? super E> comparator) {
37    super(comparator);
38  }
39
40  @Override
41  public int size() {
42    return 0;
43  }
44
45  @Override public boolean isEmpty() {
46    return true;
47  }
48
49  @Override public boolean contains(Object target) {
50    return false;
51  }
52
53  @Override public UnmodifiableIterator<E> iterator() {
54    return Iterators.emptyIterator();
55  }
56
57  @Override boolean isPartialView() {
58    return false;
59  }
60
61  private static final Object[] EMPTY_ARRAY = new Object[0];
62
63  @Override public Object[] toArray() {
64    return EMPTY_ARRAY;
65  }
66
67  @Override public <T> T[] toArray(T[] a) {
68    if (a.length > 0) {
69      a[0] = null;
70    }
71    return a;
72  }
73
74  @Override public boolean containsAll(Collection<?> targets) {
75    return targets.isEmpty();
76  }
77
78  @Override public boolean equals(@Nullable Object object) {
79    if (object instanceof Set) {
80      Set<?> that = (Set<?>) object;
81      return that.isEmpty();
82    }
83    return false;
84  }
85
86  @Override public int hashCode() {
87    return 0;
88  }
89
90  @Override public String toString() {
91    return "[]";
92  }
93
94  @Override
95  public E first() {
96    throw new NoSuchElementException();
97  }
98
99  @Override
100  public E last() {
101    throw new NoSuchElementException();
102  }
103
104  @Override
105  ImmutableSortedSet<E> headSetImpl(E toElement, boolean inclusive) {
106    return this;
107  }
108
109  @Override
110  ImmutableSortedSet<E> subSetImpl(
111      E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
112    return this;
113  }
114
115  @Override
116  ImmutableSortedSet<E> tailSetImpl(E fromElement, boolean inclusive) {
117    return this;
118  }
119
120  @Override int indexOf(@Nullable Object target) {
121    return -1;
122  }
123}
124