1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 The Guava Authors 3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License. 6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at 7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software 11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and 14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License. 15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect; 18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible; 20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable; 22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection; 23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.EnumSet; 24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** 26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Implementation of {@link ImmutableSet} backed by a non-empty {@link 27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * java.util.EnumSet}. 28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(serializable = true, emulated = true) 320888a09821a98ac0680fad765217302858e70fa4Paul Duffin@SuppressWarnings("serial") // we're overriding default serialization 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertfinal class ImmutableEnumSet<E extends Enum<E>> extends ImmutableSet<E> { 347dd252788645e940eada959bdde927426e2531c9Paul Duffin static <E extends Enum<E>> ImmutableSet<E> asImmutable(EnumSet<E> set) { 357dd252788645e940eada959bdde927426e2531c9Paul Duffin switch (set.size()) { 367dd252788645e940eada959bdde927426e2531c9Paul Duffin case 0: 377dd252788645e940eada959bdde927426e2531c9Paul Duffin return ImmutableSet.of(); 387dd252788645e940eada959bdde927426e2531c9Paul Duffin case 1: 397dd252788645e940eada959bdde927426e2531c9Paul Duffin return ImmutableSet.of(Iterables.getOnlyElement(set)); 407dd252788645e940eada959bdde927426e2531c9Paul Duffin default: 417dd252788645e940eada959bdde927426e2531c9Paul Duffin return new ImmutableEnumSet<E>(set); 427dd252788645e940eada959bdde927426e2531c9Paul Duffin } 437dd252788645e940eada959bdde927426e2531c9Paul Duffin } 447dd252788645e940eada959bdde927426e2531c9Paul Duffin 45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /* 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Notes on EnumSet and <E extends Enum<E>>: 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * This class isn't an arbitrary ForwardingImmutableSet because we need to 49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * know that calling {@code clone()} during deserialization will return an 50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * object that no one else has a reference to, allowing us to guarantee 51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * immutability. Hence, we support only {@link EnumSet}. 52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final transient EnumSet<E> delegate; 54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 557dd252788645e940eada959bdde927426e2531c9Paul Duffin private ImmutableEnumSet(EnumSet<E> delegate) { 56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.delegate = delegate; 57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 590888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override boolean isPartialView() { 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return false; 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 630888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public UnmodifiableIterator<E> iterator() { 64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.unmodifiableIterator(delegate.iterator()); 65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 670888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override 68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public int size() { 69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return delegate.size(); 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 720888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean contains(Object object) { 73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return delegate.contains(object); 74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 760888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean containsAll(Collection<?> collection) { 77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return delegate.containsAll(collection); 78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 800888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean isEmpty() { 81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return delegate.isEmpty(); 82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 840888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean equals(Object object) { 85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return object == this || delegate.equals(object); 86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private transient int hashCode; 89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 900888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public int hashCode() { 91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int result = hashCode; 92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (result == 0) ? hashCode = delegate.hashCode() : result; 93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 950888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public String toString() { 96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return delegate.toString(); 97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // All callers of the constructor are restricted to <E extends Enum<E>>. 1000888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override Object writeReplace() { 1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new EnumSerializedForm<E>(delegate); 102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /* 105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * This class is used to serialize ImmutableEnumSet instances. 106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1070888a09821a98ac0680fad765217302858e70fa4Paul Duffin private static class EnumSerializedForm<E extends Enum<E>> 1080888a09821a98ac0680fad765217302858e70fa4Paul Duffin implements Serializable { 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final EnumSet<E> delegate; 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson EnumSerializedForm(EnumSet<E> delegate) { 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.delegate = delegate; 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Object readResolve() { 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // EJ2 #76: Write readObject() methods defensively. 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new ImmutableEnumSet<E>(delegate.clone()); 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 120