1 /* 2 * Copyright (C) 2012 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 static com.google.common.base.Preconditions.checkArgument; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.J2ktIncompatible; 23 import com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap; 24 import java.io.InvalidObjectException; 25 import java.io.ObjectInputStream; 26 import java.io.Serializable; 27 import java.util.EnumMap; 28 import javax.annotation.CheckForNull; 29 30 /** 31 * Implementation of {@link ImmutableMap} backed by a non-empty {@link java.util.EnumMap}. 32 * 33 * @author Louis Wasserman 34 */ 35 @GwtCompatible(serializable = true, emulated = true) 36 @SuppressWarnings("serial") // we're overriding default serialization 37 @ElementTypesAreNonnullByDefault 38 final class ImmutableEnumMap<K extends Enum<K>, V> extends IteratorBasedImmutableMap<K, V> { asImmutable(EnumMap<K, V> map)39 static <K extends Enum<K>, V> ImmutableMap<K, V> asImmutable(EnumMap<K, V> map) { 40 switch (map.size()) { 41 case 0: 42 return ImmutableMap.of(); 43 case 1: 44 Entry<K, V> entry = Iterables.getOnlyElement(map.entrySet()); 45 return ImmutableMap.of(entry.getKey(), entry.getValue()); 46 default: 47 return new ImmutableEnumMap<>(map); 48 } 49 } 50 51 private final transient EnumMap<K, V> delegate; 52 ImmutableEnumMap(EnumMap<K, V> delegate)53 private ImmutableEnumMap(EnumMap<K, V> delegate) { 54 this.delegate = delegate; 55 checkArgument(!delegate.isEmpty()); 56 } 57 58 @Override keyIterator()59 UnmodifiableIterator<K> keyIterator() { 60 return Iterators.unmodifiableIterator(delegate.keySet().iterator()); 61 } 62 63 @Override size()64 public int size() { 65 return delegate.size(); 66 } 67 68 @Override containsKey(@heckForNull Object key)69 public boolean containsKey(@CheckForNull Object key) { 70 return delegate.containsKey(key); 71 } 72 73 @Override 74 @CheckForNull get(@heckForNull Object key)75 public V get(@CheckForNull Object key) { 76 return delegate.get(key); 77 } 78 79 @Override equals(@heckForNull Object object)80 public boolean equals(@CheckForNull Object object) { 81 if (object == this) { 82 return true; 83 } 84 if (object instanceof ImmutableEnumMap) { 85 object = ((ImmutableEnumMap<?, ?>) object).delegate; 86 } 87 return delegate.equals(object); 88 } 89 90 @Override entryIterator()91 UnmodifiableIterator<Entry<K, V>> entryIterator() { 92 return Maps.unmodifiableEntryIterator(delegate.entrySet().iterator()); 93 } 94 95 @Override isPartialView()96 boolean isPartialView() { 97 return false; 98 } 99 100 // All callers of the constructor are restricted to <K extends Enum<K>>. 101 @Override 102 @J2ktIncompatible // serialization writeReplace()103 Object writeReplace() { 104 return new EnumSerializedForm<>(delegate); 105 } 106 107 @J2ktIncompatible // serialization readObject(ObjectInputStream stream)108 private void readObject(ObjectInputStream stream) throws InvalidObjectException { 109 throw new InvalidObjectException("Use EnumSerializedForm"); 110 } 111 112 /* 113 * This class is used to serialize ImmutableEnumMap instances. 114 */ 115 @J2ktIncompatible // serialization 116 private static class EnumSerializedForm<K extends Enum<K>, V> implements Serializable { 117 final EnumMap<K, V> delegate; 118 EnumSerializedForm(EnumMap<K, V> delegate)119 EnumSerializedForm(EnumMap<K, V> delegate) { 120 this.delegate = delegate; 121 } 122 readResolve()123 Object readResolve() { 124 return new ImmutableEnumMap<>(delegate); 125 } 126 127 private static final long serialVersionUID = 0; 128 } 129 } 130