Coverage Summary for Class: ImmutableEnumSet (com.google.common.collect)
| Class | Method, % | Line, % |
|---|---|---|
| ImmutableEnumSet | 0% (0/16) | 0% (0/29) |
| ImmutableEnumSet$EnumSerializedForm | 0% (0/2) | 0% (0/3) |
| Total | 0% (0/18) | 0% (0/32) |
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 17 package com.google.common.collect; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.errorprone.annotations.concurrent.LazyInit; 21 import java.io.Serializable; 22 import java.util.Collection; 23 import java.util.EnumSet; 24 import java.util.Spliterator; 25 import java.util.function.Consumer; 26 import javax.annotation.CheckForNull; 27 28 /** 29 * Implementation of {@link ImmutableSet} backed by a non-empty {@link java.util.EnumSet}. 30 * 31 * @author Jared Levy 32 */ 33 @GwtCompatible(serializable = true, emulated = true) 34 @SuppressWarnings("serial") // we're overriding default serialization 35 @ElementTypesAreNonnullByDefault 36 final class ImmutableEnumSet<E extends Enum<E>> extends ImmutableSet<E> { 37 @SuppressWarnings("rawtypes") // necessary to compile against Java 8 38 static ImmutableSet asImmutable(EnumSet set) { 39 switch (set.size()) { 40 case 0: 41 return ImmutableSet.of(); 42 case 1: 43 return ImmutableSet.of(Iterables.getOnlyElement(set)); 44 default: 45 return new ImmutableEnumSet(set); 46 } 47 } 48 49 /* 50 * Notes on EnumSet and <E extends Enum<E>>: 51 * 52 * This class isn't an arbitrary ForwardingImmutableSet because we need to 53 * know that calling {@code clone()} during deserialization will return an 54 * object that no one else has a reference to, allowing us to guarantee 55 * immutability. Hence, we support only {@link EnumSet}. 56 */ 57 private final transient EnumSet<E> delegate; 58 59 private ImmutableEnumSet(EnumSet<E> delegate) { 60 this.delegate = delegate; 61 } 62 63 @Override 64 boolean isPartialView() { 65 return false; 66 } 67 68 @Override 69 public UnmodifiableIterator<E> iterator() { 70 return Iterators.unmodifiableIterator(delegate.iterator()); 71 } 72 73 @Override 74 public Spliterator<E> spliterator() { 75 return delegate.spliterator(); 76 } 77 78 @Override 79 public void forEach(Consumer<? super E> action) { 80 delegate.forEach(action); 81 } 82 83 @Override 84 public int size() { 85 return delegate.size(); 86 } 87 88 @Override 89 public boolean contains(@CheckForNull Object object) { 90 return delegate.contains(object); 91 } 92 93 @Override 94 public boolean containsAll(Collection<?> collection) { 95 if (collection instanceof ImmutableEnumSet<?>) { 96 collection = ((ImmutableEnumSet<?>) collection).delegate; 97 } 98 return delegate.containsAll(collection); 99 } 100 101 @Override 102 public boolean isEmpty() { 103 return delegate.isEmpty(); 104 } 105 106 @Override 107 public boolean equals(@CheckForNull Object object) { 108 if (object == this) { 109 return true; 110 } 111 if (object instanceof ImmutableEnumSet) { 112 object = ((ImmutableEnumSet<?>) object).delegate; 113 } 114 return delegate.equals(object); 115 } 116 117 @Override 118 boolean isHashCodeFast() { 119 return true; 120 } 121 122 @LazyInit private transient int hashCode; 123 124 @Override 125 public int hashCode() { 126 int result = hashCode; 127 return (result == 0) ? hashCode = delegate.hashCode() : result; 128 } 129 130 @Override 131 public String toString() { 132 return delegate.toString(); 133 } 134 135 // All callers of the constructor are restricted to <E extends Enum<E>>. 136 @Override 137 Object writeReplace() { 138 return new EnumSerializedForm<E>(delegate); 139 } 140 141 /* 142 * This class is used to serialize ImmutableEnumSet instances. 143 */ 144 private static class EnumSerializedForm<E extends Enum<E>> implements Serializable { 145 final EnumSet<E> delegate; 146 147 EnumSerializedForm(EnumSet<E> delegate) { 148 this.delegate = delegate; 149 } 150 151 Object readResolve() { 152 // EJ2 #76: Write readObject() methods defensively. 153 return new ImmutableEnumSet<E>(delegate.clone()); 154 } 155 156 private static final long serialVersionUID = 0; 157 } 158 }