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