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