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