| 1 52 53 package com.go.trove.util; 54 55 import java.util.*; 56 import java.io.Serializable ; 57 58 70 public class NullKeyMap extends AbstractMap implements Serializable { 71 private static final Object NULL = new Serializable () {}; 73 74 private Map mMap; 75 76 private transient Set mKeySet; 77 private transient Set mEntrySet; 78 79 82 public NullKeyMap(Map map) { 83 mMap = map; 84 } 85 86 public int size() { 87 return mMap.size(); 88 } 89 90 public boolean isEmpty() { 91 return mMap.isEmpty(); 92 } 93 94 public boolean containsKey(Object key) { 95 return (key == null) ? mMap.containsKey(NULL) : mMap.containsKey(key); 96 } 97 98 public boolean containsValue(Object value) { 99 return mMap.containsValue(value); 100 } 101 102 public Object get(Object key) { 103 return (key == null) ? mMap.get(NULL) : mMap.get(key); 104 } 105 106 public Object put(Object key, Object value) { 107 return mMap.put((key == null) ? NULL : key, value); 108 } 109 110 public Object remove(Object key) { 111 return (key == null) ? mMap.remove(NULL) : mMap.remove(key); 112 } 113 114 public void putAll(Map map) { 115 Iterator it = map.entrySet().iterator(); 116 while (it.hasNext()) { 117 Map.Entry entry = (Map.Entry)it.next(); 118 put(entry.getKey(), entry.getValue()); 119 } 120 } 121 122 public void clear() { 123 mMap.clear(); 124 } 125 126 public Set keySet() { 127 if (mKeySet == null) { 128 mKeySet = new AbstractSet() { 129 public Iterator iterator() { 130 final Iterator it = mMap.keySet().iterator(); 131 132 return new Iterator() { 133 public boolean hasNext() { 134 return it.hasNext(); 135 } 136 137 public Object next() { 138 Object key = it.next(); 139 return (key == NULL) ? null : key; 140 } 141 142 public void remove() { 143 it.remove(); 144 } 145 }; 146 } 147 148 public boolean contains(Object key) { 149 return containsKey((key == null) ? NULL : key); 150 } 151 152 public boolean remove(Object key) { 153 if (key == null) { 154 key = NULL; 155 } 156 if (containsKey(key)) { 157 NullKeyMap.this.remove(key); 158 return true; 159 } 160 else { 161 return false; 162 } 163 } 164 165 public int size() { 166 return NullKeyMap.this.size(); 167 } 168 169 public void clear() { 170 NullKeyMap.this.clear(); 171 } 172 }; 173 } 174 175 return mKeySet; 176 } 177 178 public Collection values() { 179 return mMap.values(); 180 } 181 182 public Set entrySet() { 183 if (mEntrySet == null) { 184 mEntrySet = new AbstractSet() { 185 public Iterator iterator() { 186 final Iterator it = mMap.entrySet().iterator(); 187 188 return new Iterator() { 189 public boolean hasNext() { 190 return it.hasNext(); 191 } 192 193 public Object next() { 194 final Map.Entry entry = (Map.Entry)it.next(); 195 if (entry.getKey() == NULL) { 196 return new AbstractMapEntry() { 197 public Object getKey() { 198 return null; 199 } 200 201 public Object getValue() { 202 return entry.getValue(); 203 } 204 205 public Object setValue(Object value) { 206 return entry.setValue(value); 207 } 208 }; 209 } 210 else { 211 return entry; 212 } 213 } 214 215 public void remove() { 216 it.remove(); 217 } 218 }; 219 } 220 221 public boolean contains(Object obj) { 222 if (!(obj instanceof Map.Entry)) { 223 return false; 224 } 225 Map.Entry entry = (Map.Entry)obj; 226 Object key = entry.getKey(); 227 Object value = entry.getValue(); 228 if (key == null) { 229 key = NULL; 230 } 231 Object lookup = get(key); 232 if (lookup == null) { 233 return value == null; 234 } 235 else { 236 return lookup.equals(value); 237 } 238 } 239 240 public boolean remove(Object obj) { 241 if (!(obj instanceof Map.Entry)) { 242 return false; 243 } 244 Map.Entry entry = ((Map.Entry)obj); 245 Object key = entry.getKey(); 246 if (key == null) { 247 key = NULL; 248 } 249 if (containsKey(key)) { 250 NullKeyMap.this.remove(key); 251 return true; 252 } 253 else { 254 return false; 255 } 256 } 257 258 public int size() { 259 return NullKeyMap.this.size(); 260 } 261 262 public void clear() { 263 NullKeyMap.this.clear(); 264 } 265 }; 266 } 267 268 return mEntrySet; 269 } 270 } 271 | Popular Tags |