EmptyImmutableSet.java revision 3c77433663281544363151bf284b0240dfd22a42
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(@Nullable Object target) { 47 return false; 48 } 49 50 @Override public boolean containsAll(Collection<?> targets) { 51 return targets.isEmpty(); 52 } 53 54 @Override public UnmodifiableIterator<Object> iterator() { 55 return Iterators.emptyIterator(); 56 } 57 58 @Override boolean isPartialView() { 59 return false; 60 } 61 62 @Override public Object[] toArray() { 63 return ObjectArrays.EMPTY_ARRAY; 64 } 65 66 @Override public <T> T[] toArray(T[] a) { 67 return asList().toArray(a); 68 } 69 70 @Override 71 public ImmutableList<Object> asList() { 72 return ImmutableList.of(); 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