1 /* 2 * Copyright (C) 2008 Google Inc. 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 21 import java.util.Map; 22 23 import javax.annotation.Nullable; 24 25 import static com.google.common.base.Preconditions.checkNotNull; 26 27 /** 28 * Implementation of {@link ImmutableMap} with exactly one entry. 29 * 30 * @author Jesse Wilson 31 * @author Kevin Bourrillion 32 */ 33 @GwtCompatible(serializable = true) 34 @SuppressWarnings("serial") // uses writeReplace(), not default serialization 35 final class SingletonImmutableMap<K, V> extends ImmutableMap<K, V> { 36 final transient K singleKey; 37 final transient V singleValue; 38 39 private transient Entry<K, V> entry; 40 41 SingletonImmutableMap(K singleKey, V singleValue) { 42 this.singleKey = singleKey; 43 this.singleValue = singleValue; 44 } 45 46 SingletonImmutableMap(Entry<K, V> entry) { 47 this.entry = checkNotNull(entry); // checkNotNull for GWT. 48 this.singleKey = checkNotNull(entry.getKey()); // checkNotNull for GWT. 49 this.singleValue = checkNotNull(entry.getValue()); // checkNotNull for GWT. 50 } 51 52 private Entry<K, V> entry() { 53 Entry<K, V> e = entry; 54 return (e == null) 55 ? (entry = Maps.immutableEntry(singleKey, singleValue)) : e; 56 } 57 58 @Override public V get(Object key) { 59 return singleKey.equals(key) ? singleValue : null; 60 } 61 62 public int size() { 63 return 1; 64 } 65 66 @Override public boolean isEmpty() { 67 return false; 68 } 69 70 @Override public boolean containsKey(Object key) { 71 return singleKey.equals(key); 72 } 73 74 @Override public boolean containsValue(Object value) { 75 return singleValue.equals(value); 76 } 77 78 private transient ImmutableSet<Entry<K, V>> entrySet; 79 80 @Override public ImmutableSet<Entry<K, V>> entrySet() { 81 ImmutableSet<Entry<K, V>> es = entrySet; 82 return (es == null) ? (entrySet = ImmutableSet.of(entry())) : es; 83 } 84 85 private transient ImmutableSet<K> keySet; 86 87 @Override public ImmutableSet<K> keySet() { 88 ImmutableSet<K> ks = keySet; 89 return (ks == null) ? (keySet = ImmutableSet.of(singleKey)) : ks; 90 } 91 92 private transient ImmutableCollection<V> values; 93 94 @Override public ImmutableCollection<V> values() { 95 ImmutableCollection<V> v = values; 96 return (v == null) ? (values = new Values<V>(singleValue)) : v; 97 } 98 99 @SuppressWarnings("serial") // uses writeReplace(), not default serialization 100 private static class Values<V> extends ImmutableCollection<V> { 101 final V singleValue; 102 103 Values(V singleValue) { 104 this.singleValue = singleValue; 105 } 106 107 @Override public boolean contains(Object object) { 108 return singleValue.equals(object); 109 } 110 111 @Override public boolean isEmpty() { 112 return false; 113 } 114 115 public int size() { 116 return 1; 117 } 118 119 @Override public UnmodifiableIterator<V> iterator() { 120 return Iterators.singletonIterator(singleValue); 121 } 122 } 123 124 @Override public boolean equals(@Nullable Object object) { 125 if (object == this) { 126 return true; 127 } 128 if (object instanceof Map) { 129 Map<?, ?> that = (Map<?, ?>) object; 130 if (that.size() != 1) { 131 return false; 132 } 133 Entry<?, ?> entry = that.entrySet().iterator().next(); 134 return singleKey.equals(entry.getKey()) 135 && singleValue.equals(entry.getValue()); 136 } 137 return false; 138 } 139 140 @Override public int hashCode() { 141 return singleKey.hashCode() ^ singleValue.hashCode(); 142 } 143 144 @Override public String toString() { 145 return new StringBuilder() 146 .append('{') 147 .append(singleKey.toString()) 148 .append('=') 149 .append(singleValue.toString()) 150 .append('}') 151 .toString(); 152 } 153 } 154