1/* 2 * Copyright (C) 2007 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.Set; 23 24import javax.annotation.Nullable; 25 26/** 27 * An empty immutable set. 28 * 29 * @author Kevin Bourrillion 30 */ 31@GwtCompatible(serializable = true, emulated = true) 32final class EmptyImmutableSet extends ImmutableSet<Object> { 33 static final EmptyImmutableSet INSTANCE = new EmptyImmutableSet(); 34 35 private EmptyImmutableSet() {} 36 37 @Override 38 public int size() { 39 return 0; 40 } 41 42 @Override public boolean isEmpty() { 43 return true; 44 } 45 46 @Override public boolean contains(Object target) { 47 return false; 48 } 49 50 @Override public UnmodifiableIterator<Object> iterator() { 51 return Iterators.emptyIterator(); 52 } 53 54 @Override boolean isPartialView() { 55 return false; 56 } 57 58 private static final Object[] EMPTY_ARRAY = new Object[0]; 59 60 @Override public Object[] toArray() { 61 return EMPTY_ARRAY; 62 } 63 64 @Override public <T> T[] toArray(T[] a) { 65 if (a.length > 0) { 66 a[0] = null; 67 } 68 return a; 69 } 70 71 @Override public boolean containsAll(Collection<?> targets) { 72 return targets.isEmpty(); 73 } 74 75 @Override public boolean equals(@Nullable Object object) { 76 if (object instanceof Set) { 77 Set<?> that = (Set<?>) object; 78 return that.isEmpty(); 79 } 80 return false; 81 } 82 83 @Override public final int hashCode() { 84 return 0; 85 } 86 87 @Override boolean isHashCodeFast() { 88 return true; 89 } 90 91 @Override public String toString() { 92 return "[]"; 93 } 94 95 Object readResolve() { 96 return INSTANCE; // preserve singleton property 97 } 98 99 private static final long serialVersionUID = 0; 100} 101