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 22 import java.io.Serializable; 23 import java.util.Map.Entry; 24 25 import javax.annotation.Nullable; 26 27 /** 28 * {@code values()} implementation for {@link ImmutableMap}. 29 * 30 * @author Jesse Wilson 31 * @author Kevin Bourrillion 32 */ 33 @GwtCompatible(emulated = true) 34 final class ImmutableMapValues<K, V> extends ImmutableCollection<V> { 35 private final ImmutableMap<K, V> map; 36 ImmutableMapValues(ImmutableMap<K, V> map)37 ImmutableMapValues(ImmutableMap<K, V> map) { 38 this.map = map; 39 } 40 41 @Override size()42 public int size() { 43 return map.size(); 44 } 45 46 @Override iterator()47 public UnmodifiableIterator<V> iterator() { 48 return Maps.valueIterator(map.entrySet().iterator()); 49 } 50 51 @Override contains(@ullable Object object)52 public boolean contains(@Nullable Object object) { 53 return object != null && Iterators.contains(iterator(), object); 54 } 55 56 @Override isPartialView()57 boolean isPartialView() { 58 return true; 59 } 60 61 @Override createAsList()62 ImmutableList<V> createAsList() { 63 final ImmutableList<Entry<K, V>> entryList = map.entrySet().asList(); 64 return new ImmutableAsList<V>() { 65 @Override 66 public V get(int index) { 67 return entryList.get(index).getValue(); 68 } 69 70 @Override 71 ImmutableCollection<V> delegateCollection() { 72 return ImmutableMapValues.this; 73 } 74 }; 75 } 76 77 @GwtIncompatible("serialization") 78 @Override Object writeReplace() { 79 return new SerializedForm<V>(map); 80 } 81 82 @GwtIncompatible("serialization") 83 private static class SerializedForm<V> implements Serializable { 84 final ImmutableMap<?, V> map; 85 SerializedForm(ImmutableMap<?, V> map) { 86 this.map = map; 87 } 88 Object readResolve() { 89 return map.values(); 90 } 91 private static final long serialVersionUID = 0; 92 } 93 } 94