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