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)
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@SuppressWarnings("serial") // we're overriding default serialization
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertfinal class ImmutableEnumSet<E extends Enum<E>> extends ImmutableSet<E> {
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /*
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Notes on EnumSet and <E extends Enum<E>>:
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * This class isn't an arbitrary ForwardingImmutableSet because we need to
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * know that calling {@code clone()} during deserialization will return an
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * object that no one else has a reference to, allowing us to guarantee
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * immutability. Hence, we support only {@link EnumSet}.
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private final transient EnumSet<E> delegate;
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  ImmutableEnumSet(EnumSet<E> delegate) {
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.delegate = delegate;
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override boolean isPartialView() {
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return false;
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public UnmodifiableIterator<E> iterator() {
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return Iterators.unmodifiableIterator(delegate.iterator());
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public int size() {
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate.size();
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean contains(Object object) {
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate.contains(object);
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean containsAll(Collection<?> collection) {
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate.containsAll(collection);
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean isEmpty() {
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate.isEmpty();
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public Object[] toArray() {
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate.toArray();
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public <T> T[] toArray(T[] array) {
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate.toArray(array);
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean equals(Object object) {
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return object == this || delegate.equals(object);
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private transient int hashCode;
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public int hashCode() {
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int result = hashCode;
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (result == 0) ? hashCode = delegate.hashCode() : result;
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public String toString() {
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate.toString();
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // All callers of the constructor are restricted to <E extends Enum<E>>.
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override Object writeReplace() {
981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new EnumSerializedForm<E>(delegate);
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /*
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * This class is used to serialize ImmutableEnumSet instances.
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class EnumSerializedForm<E extends Enum<E>>
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Serializable {
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final EnumSet<E> delegate;
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    EnumSerializedForm(EnumSet<E> delegate) {
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.delegate = delegate;
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Object readResolve() {
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // EJ2 #76: Write readObject() methods defensively.
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return new ImmutableEnumSet<E>(delegate.clone());
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
117