1/* 2 * Copyright (C) 2009 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.io.Serializable; 22import java.util.Collection; 23import java.util.EnumSet; 24 25/** 26 * Implementation of {@link ImmutableSet} backed by a non-empty {@link 27 * java.util.EnumSet}. 28 * 29 * @author Jared Levy 30 */ 31@GwtCompatible(serializable = true, emulated = true) 32@SuppressWarnings("serial") // we're overriding default serialization 33final class ImmutableEnumSet<E extends Enum<E>> extends ImmutableSet<E> { 34 /* 35 * Notes on EnumSet and <E extends Enum<E>>: 36 * 37 * This class isn't an arbitrary ForwardingImmutableSet because we need to 38 * know that calling {@code clone()} during deserialization will return an 39 * object that no one else has a reference to, allowing us to guarantee 40 * immutability. Hence, we support only {@link EnumSet}. 41 */ 42 private final transient EnumSet<E> delegate; 43 44 ImmutableEnumSet(EnumSet<E> delegate) { 45 this.delegate = delegate; 46 } 47 48 @Override boolean isPartialView() { 49 return false; 50 } 51 52 @Override public UnmodifiableIterator<E> iterator() { 53 return Iterators.unmodifiableIterator(delegate.iterator()); 54 } 55 56 @Override 57 public int size() { 58 return delegate.size(); 59 } 60 61 @Override public boolean contains(Object object) { 62 return delegate.contains(object); 63 } 64 65 @Override public boolean containsAll(Collection<?> collection) { 66 return delegate.containsAll(collection); 67 } 68 69 @Override public boolean isEmpty() { 70 return delegate.isEmpty(); 71 } 72 73 @Override public Object[] toArray() { 74 return delegate.toArray(); 75 } 76 77 @Override public <T> T[] toArray(T[] array) { 78 return delegate.toArray(array); 79 } 80 81 @Override public boolean equals(Object object) { 82 return object == this || delegate.equals(object); 83 } 84 85 private transient int hashCode; 86 87 @Override public int hashCode() { 88 int result = hashCode; 89 return (result == 0) ? hashCode = delegate.hashCode() : result; 90 } 91 92 @Override public String toString() { 93 return delegate.toString(); 94 } 95 96 // All callers of the constructor are restricted to <E extends Enum<E>>. 97 @Override Object writeReplace() { 98 return new EnumSerializedForm<E>(delegate); 99 } 100 101 /* 102 * This class is used to serialize ImmutableEnumSet instances. 103 */ 104 private static class EnumSerializedForm<E extends Enum<E>> 105 implements Serializable { 106 final EnumSet<E> delegate; 107 EnumSerializedForm(EnumSet<E> delegate) { 108 this.delegate = delegate; 109 } 110 Object readResolve() { 111 // EJ2 #76: Write readObject() methods defensively. 112 return new ImmutableEnumSet<E>(delegate.clone()); 113 } 114 private static final long serialVersionUID = 0; 115 } 116} 117