View Javadoc
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   */
14  package com.google.common.collect;
15  
16  import com.google.common.annotations.GwtCompatible;
17  import com.google.common.annotations.GwtIncompatible;
18  import java.io.Serializable;
19  import java.util.NoSuchElementException;
20  import java.util.Set;
21  import javax.annotation.Nullable;
22  
23  /**
24   * An empty contiguous set.
25   *
26   * @author Gregory Kick
27   */
28  @GwtCompatible(emulated = true)
29  @SuppressWarnings("unchecked") // allow ungenerified Comparable types
30  final class EmptyContiguousSet<C extends Comparable> extends ContiguousSet<C> {
31    EmptyContiguousSet(DiscreteDomain<C> domain) {
32      super(domain);
33    }
34  
35    @Override
36    public C first() {
37      throw new NoSuchElementException();
38    }
39  
40    @Override
41    public C last() {
42      throw new NoSuchElementException();
43    }
44  
45    @Override
46    public int size() {
47      return 0;
48    }
49  
50    @Override
51    public ContiguousSet<C> intersection(ContiguousSet<C> other) {
52      return this;
53    }
54  
55    @Override
56    public Range<C> range() {
57      throw new NoSuchElementException();
58    }
59  
60    @Override
61    public Range<C> range(BoundType lowerBoundType, BoundType upperBoundType) {
62      throw new NoSuchElementException();
63    }
64  
65    @Override
66    ContiguousSet<C> headSetImpl(C toElement, boolean inclusive) {
67      return this;
68    }
69  
70    @Override
71    ContiguousSet<C> subSetImpl(
72        C fromElement, boolean fromInclusive, C toElement, boolean toInclusive) {
73      return this;
74    }
75  
76    @Override
77    ContiguousSet<C> tailSetImpl(C fromElement, boolean fromInclusive) {
78      return this;
79    }
80  
81    @Override
82    public boolean contains(Object object) {
83      return false;
84    }
85  
86    @GwtIncompatible // not used by GWT emulation
87    @Override
88    int indexOf(Object target) {
89      return -1;
90    }
91  
92    @Override
93    public UnmodifiableIterator<C> iterator() {
94      return Iterators.emptyIterator();
95    }
96  
97    @GwtIncompatible // NavigableSet
98    @Override
99    public UnmodifiableIterator<C> descendingIterator() {
100     return Iterators.emptyIterator();
101   }
102 
103   @Override
104   boolean isPartialView() {
105     return false;
106   }
107 
108   @Override
109   public boolean isEmpty() {
110     return true;
111   }
112 
113   @Override
114   public ImmutableList<C> asList() {
115     return ImmutableList.of();
116   }
117 
118   @Override
119   public String toString() {
120     return "[]";
121   }
122 
123   @Override
124   public boolean equals(@Nullable Object object) {
125     if (object instanceof Set) {
126       Set<?> that = (Set<?>) object;
127       return that.isEmpty();
128     }
129     return false;
130   }
131 
132   @GwtIncompatible // not used in GWT
133   @Override
134   boolean isHashCodeFast() {
135     return true;
136   }
137 
138   @Override
139   public int hashCode() {
140     return 0;
141   }
142 
143   @GwtIncompatible // serialization
144   private static final class SerializedForm<C extends Comparable> implements Serializable {
145     private final DiscreteDomain<C> domain;
146 
147     private SerializedForm(DiscreteDomain<C> domain) {
148       this.domain = domain;
149     }
150 
151     private Object readResolve() {
152       return new EmptyContiguousSet<C>(domain);
153     }
154 
155     private static final long serialVersionUID = 0;
156   }
157 
158   @GwtIncompatible // serialization
159   @Override
160   Object writeReplace() {
161     return new SerializedForm<C>(domain);
162   }
163 
164   @GwtIncompatible // NavigableSet
165   ImmutableSortedSet<C> createDescendingSet() {
166     return ImmutableSortedSet.emptySet(Ordering.natural().reverse());
167   }
168 }