1/*
2 * Copyright (C) 2011 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 License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13*/
14package com.google.common.collect;
15
16import com.google.common.annotations.GwtCompatible;
17import com.google.common.annotations.GwtIncompatible;
18
19import java.io.Serializable;
20import java.util.NoSuchElementException;
21import java.util.Set;
22
23import javax.annotation.Nullable;
24
25/**
26 * An empty contiguous set.
27 *
28 * @author Gregory Kick
29 */
30@GwtCompatible(emulated = true)
31@SuppressWarnings("unchecked") // allow ungenerified Comparable types
32final class EmptyContiguousSet<C extends Comparable> extends ContiguousSet<C> {
33  EmptyContiguousSet(DiscreteDomain<C> domain) {
34    super(domain);
35  }
36
37  @Override public C first() {
38    throw new NoSuchElementException();
39  }
40
41  @Override public C last() {
42    throw new NoSuchElementException();
43  }
44
45  @Override public int size() {
46    return 0;
47  }
48
49  @Override public ContiguousSet<C> intersection(ContiguousSet<C> other) {
50    return this;
51  }
52
53  @Override public Range<C> range() {
54    throw new NoSuchElementException();
55  }
56
57  @Override public Range<C> range(BoundType lowerBoundType, BoundType upperBoundType) {
58    throw new NoSuchElementException();
59  }
60
61  @Override ContiguousSet<C> headSetImpl(C toElement, boolean inclusive) {
62    return this;
63  }
64
65  @Override ContiguousSet<C> subSetImpl(
66      C fromElement, boolean fromInclusive, C toElement, boolean toInclusive) {
67    return this;
68  }
69
70  @Override ContiguousSet<C> tailSetImpl(C fromElement, boolean fromInclusive) {
71    return this;
72  }
73
74  //Abstract method doesn't exist in GWT emulation
75  /* @Override */ int indexOf(Object target) {
76    return -1;
77  }
78
79  @Override public UnmodifiableIterator<C> iterator() {
80    return Iterators.emptyIterator();
81  }
82
83  @Override boolean isPartialView() {
84    return false;
85  }
86
87  @Override public boolean isEmpty() {
88    return true;
89  }
90
91  @Override public ImmutableList<C> asList() {
92    return ImmutableList.of();
93  }
94
95  @Override public String toString() {
96    return "[]";
97  }
98
99  @Override public boolean equals(@Nullable Object object) {
100    if (object instanceof Set) {
101      Set<?> that = (Set<?>) object;
102      return that.isEmpty();
103    }
104    return false;
105  }
106
107  @Override public int hashCode() {
108    return 0;
109  }
110
111  @GwtIncompatible("serialization")
112  private static final class SerializedForm<C extends Comparable> implements Serializable {
113    private final DiscreteDomain<C> domain;
114
115    private SerializedForm(DiscreteDomain<C> domain) {
116      this.domain = domain;
117    }
118
119    private Object readResolve() {
120      return new EmptyContiguousSet<C>(domain);
121    }
122
123    private static final long serialVersionUID = 0;
124  }
125
126  @GwtIncompatible("serialization")
127  @Override
128  Object writeReplace() {
129    return new SerializedForm<C>(domain);
130  }
131}
132