View Javadoc
1   /*
2    * Copyright (C) 2008 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.checkNotNull;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.annotations.GwtIncompatible;
23  import com.google.j2objc.annotations.Weak;
24  import java.io.Serializable;
25  import java.util.Map.Entry;
26  import java.util.Spliterator;
27  import java.util.Spliterators;
28  import java.util.function.Consumer;
29  import javax.annotation.Nullable;
30  
31  /**
32   * {@code entrySet()} implementation for {@link ImmutableMap}.
33   *
34   * @author Jesse Wilson
35   * @author Kevin Bourrillion
36   */
37  @GwtCompatible(emulated = true)
38  abstract class ImmutableMapEntrySet<K, V> extends ImmutableSet<Entry<K, V>> {
39    static final class RegularEntrySet<K, V> extends ImmutableMapEntrySet<K, V> {
40      @Weak private final transient ImmutableMap<K, V> map;
41      private final transient Entry<K, V>[] entries;
42  
43      RegularEntrySet(ImmutableMap<K, V> map, Entry<K, V>[] entries) {
44        this.map = map;
45        this.entries = entries;
46      }
47  
48      @Override
49      ImmutableMap<K, V> map() {
50        return map;
51      }
52  
53      @Override
54      public UnmodifiableIterator<Entry<K, V>> iterator() {
55        return Iterators.forArray(entries);
56      }
57  
58      @Override
59      public Spliterator<Entry<K, V>> spliterator() {
60        return Spliterators.spliterator(entries, ImmutableSet.SPLITERATOR_CHARACTERISTICS);
61      }
62  
63      @Override
64      public void forEach(Consumer<? super Entry<K, V>> action) {
65        checkNotNull(action);
66        for (Entry<K, V> entry : entries) {
67          action.accept(entry);
68        }
69      }
70  
71      @Override
72      ImmutableList<Entry<K, V>> createAsList() {
73        return new RegularImmutableAsList<>(this, entries);
74      }
75    }
76  
77    ImmutableMapEntrySet() {}
78  
79    abstract ImmutableMap<K, V> map();
80  
81    @Override
82    public int size() {
83      return map().size();
84    }
85  
86    @Override
87    public boolean contains(@Nullable Object object) {
88      if (object instanceof Entry) {
89        Entry<?, ?> entry = (Entry<?, ?>) object;
90        V value = map().get(entry.getKey());
91        return value != null && value.equals(entry.getValue());
92      }
93      return false;
94    }
95  
96    @Override
97    boolean isPartialView() {
98      return map().isPartialView();
99    }
100 
101   @Override
102   @GwtIncompatible // not used in GWT
103   boolean isHashCodeFast() {
104     return map().isHashCodeFast();
105   }
106 
107   @Override
108   public int hashCode() {
109     return map().hashCode();
110   }
111 
112   @GwtIncompatible // serialization
113   @Override
114   Object writeReplace() {
115     return new EntrySetSerializedForm<>(map());
116   }
117 
118   @GwtIncompatible // serialization
119   private static class EntrySetSerializedForm<K, V> implements Serializable {
120     final ImmutableMap<K, V> map;
121 
122     EntrySetSerializedForm(ImmutableMap<K, V> map) {
123       this.map = map;
124     }
125 
126     Object readResolve() {
127       return map.entrySet();
128     }
129 
130     private static final long serialVersionUID = 0;
131   }
132 }