EmptyContiguousSet.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
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; 17 18import java.util.NoSuchElementException; 19import java.util.Set; 20 21import 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 30final class EmptyContiguousSet<C extends Comparable> extends ContiguousSet<C> { 31 EmptyContiguousSet(DiscreteDomain<C> domain) { 32 super(domain); 33 } 34 35 @Override public C first() { 36 throw new NoSuchElementException(); 37 } 38 39 @Override public C last() { 40 throw new NoSuchElementException(); 41 } 42 43 @Override public int size() { 44 return 0; 45 } 46 47 @Override public ContiguousSet<C> intersection(ContiguousSet<C> other) { 48 return this; 49 } 50 51 @Override public Range<C> range() { 52 throw new NoSuchElementException(); 53 } 54 55 @Override public Range<C> range(BoundType lowerBoundType, BoundType upperBoundType) { 56 throw new NoSuchElementException(); 57 } 58 59 @Override ContiguousSet<C> headSetImpl(C toElement, boolean inclusive) { 60 return this; 61 } 62 63 @Override ContiguousSet<C> subSetImpl( 64 C fromElement, boolean fromInclusive, C toElement, boolean toInclusive) { 65 return this; 66 } 67 68 @Override ContiguousSet<C> tailSetImpl(C fromElement, boolean fromInclusive) { 69 return this; 70 } 71 72 //Abstract method doesn't exist in GWT emulation 73 /* @Override */ int indexOf(Object target) { 74 return -1; 75 } 76 77 @Override public UnmodifiableIterator<C> iterator() { 78 return Iterators.emptyIterator(); 79 } 80 81 @Override boolean isPartialView() { 82 return false; 83 } 84 85 @Override public boolean isEmpty() { 86 return true; 87 } 88 89 @Override public ImmutableList<C> asList() { 90 return ImmutableList.of(); 91 } 92 93 @Override public String toString() { 94 return "[]"; 95 } 96 97 @Override public boolean equals(@Nullable Object object) { 98 if (object instanceof Set) { 99 Set<?> that = (Set<?>) object; 100 return that.isEmpty(); 101 } 102 return false; 103 } 104 105 @Override public int hashCode() { 106 return 0; 107 } 108} 109 110