1 33 34 37 package com.icesoft.faces.context; 38 39 import java.util.AbstractCollection ; 40 import java.util.AbstractSet ; 41 import java.util.ArrayList ; 42 import java.util.Collection ; 43 import java.util.Enumeration ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 import java.util.NoSuchElementException ; 48 import java.util.Set ; 49 50 public abstract class AbstractAttributeMap implements Map { 51 52 private Collection values; 53 private Set keySet; 54 private Set entrySet; 55 56 59 public int size() { 60 int size = 0; 61 Enumeration e = getAttributeNames(); 62 while (e.hasMoreElements()) { 63 e.nextElement(); 64 size++; 65 } 66 return size; 67 } 68 69 72 public void clear() { 73 Enumeration e = getAttributeNames(); 74 List keys = new ArrayList (); 75 while (e.hasMoreElements()) 76 keys.add(e.nextElement()); 77 78 Iterator iterator = keys.iterator(); 79 while (iterator.hasNext()) 80 removeAttribute((String ) iterator.next()); 81 } 82 83 86 public boolean isEmpty() { 87 return !getAttributeNames().hasMoreElements(); 88 } 89 90 93 public boolean containsKey(Object key) { 94 return getAttribute(key.toString()) != null; 95 } 96 97 100 public boolean containsValue(Object value) { 101 if (value == null) 102 return false; 103 104 Enumeration e = getAttributeNames(); 105 while (e.hasMoreElements()) { 106 if (value.equals(getAttribute(e.nextElement().toString()))) { 107 return true; 108 } 109 } 110 return false; 111 } 112 113 116 public Collection values() { 117 return (values != null) ? values : (values = new Values()); 118 } 119 120 123 public void putAll(Map map) { 124 Iterator iterator = map.entrySet().iterator(); 125 while (iterator.hasNext()) { 126 Entry entry = (Entry) iterator.next(); 127 setAttribute(entry.getKey().toString(), entry.getValue()); 128 } 129 } 130 131 134 public Set entrySet() { 135 return (entrySet != null) ? entrySet : (entrySet = new EntrySet()); 136 } 137 138 141 public Set keySet() { 142 return (keySet != null) ? keySet : (keySet = new KeySet()); 143 } 144 145 148 public Object get(Object key) { 149 return getAttribute(key.toString()); 150 } 151 152 155 public Object remove(Object key) { 156 Object associatedValue = getAttribute(key.toString()); 157 removeAttribute(key.toString()); 158 return associatedValue; 159 } 160 161 164 public Object put(Object key, Object value) { 165 Object previousValue = getAttribute(key.toString()); 166 setAttribute(key.toString(), value); 167 return previousValue; 168 } 169 170 abstract protected Object getAttribute(String key); 171 172 abstract protected void setAttribute(String key, Object value); 173 174 abstract protected void removeAttribute(String key); 175 176 abstract protected Enumeration getAttributeNames(); 177 178 private class KeySet extends AbstractSet { 179 180 183 public Iterator iterator() { 184 return new KeyIterator(); 185 } 186 187 190 public int size() { 191 return AbstractAttributeMap.this.size(); 192 } 193 194 public boolean isEmpty() { 195 return AbstractAttributeMap.this.isEmpty(); 196 } 197 198 public boolean contains(Object key) { 199 return AbstractAttributeMap.this.containsKey(key); 200 } 201 202 public boolean remove(Object key) { 203 return AbstractAttributeMap.this.remove(key) != null; 204 } 205 206 public void clear() { 207 AbstractAttributeMap.this.clear(); 208 } 209 } 210 211 private class KeyIterator implements Iterator { 212 213 protected final Enumeration e = getAttributeNames(); 214 protected Object currentKey; 215 216 219 public boolean hasNext() { 220 return e.hasMoreElements(); 221 } 222 223 226 public Object next() { 227 return currentKey = e.nextElement(); 228 } 229 230 233 public void remove() { 234 if (currentKey == null) { 235 throw new NoSuchElementException ( 236 "No element is pointed for remove operation"); 237 } 238 AbstractAttributeMap.this.remove(currentKey); 239 } 240 } 241 242 private class Values extends AbstractCollection { 243 244 public Iterator iterator() { 245 return new ValuesIterator(); 246 } 247 248 public boolean contains(Object value) { 249 return containsValue(value); 250 } 251 252 public boolean remove(Object value) { 253 if (value == null) 254 return false; 255 256 Iterator iterator = iterator(); 257 while (iterator.hasNext()) { 258 if (value.equals(iterator.next())) { 259 iterator.remove(); 260 return true; 261 } 262 } 263 return false; 264 } 265 266 public int size() { 267 return AbstractAttributeMap.this.size(); 268 } 269 270 public boolean isEmpty() { 271 return AbstractAttributeMap.this.isEmpty(); 272 } 273 274 public void clear() { 275 AbstractAttributeMap.this.clear(); 276 } 277 } 278 279 private class ValuesIterator extends KeyIterator { 280 public Object next() { 281 super.next(); 282 return AbstractAttributeMap.this.get(currentKey); 283 } 284 } 285 286 private class EntrySet extends KeySet { 287 public Iterator iterator() { 288 return new EntryIterator(); 289 } 290 291 public boolean contains(Object object) { 292 if (object == null) 293 return false; 294 295 if (!(object instanceof Entry)) 296 return false; 297 298 Entry entry = (Entry) object; 299 Object key = entry.getKey(); 300 Object value = entry.getValue(); 301 302 if (key == null || value == null) 303 return false; 304 305 return value.equals(AbstractAttributeMap.this.get(key)); 306 } 307 308 public boolean remove(Object object) { 309 if (object == null) 310 return false; 311 312 if (!(object instanceof Entry)) 313 return false; 314 315 Entry entry = (Entry) object; 316 Object key = entry.getKey(); 317 Object value = entry.getValue(); 318 if (!value.equals(AbstractAttributeMap.this.get(key)) || 319 key == null || value == null) 320 return false; 321 322 return AbstractAttributeMap.this.remove(key) != null; 323 } 324 } 325 326 private class EntryIterator extends KeyIterator { 327 public Object next() { 328 super.next(); 329 return new EntrySetEntry(currentKey); 330 } 331 } 332 333 private class EntrySetEntry implements Entry { 334 private final Object currentKey; 335 336 public EntrySetEntry(Object currentKey) { 337 this.currentKey = currentKey; 338 } 339 340 public Object getKey() { 341 return currentKey; 342 } 343 344 public Object getValue() { 345 return AbstractAttributeMap.this.get(currentKey); 346 } 347 348 public Object setValue(Object value) { 349 return AbstractAttributeMap.this.put(currentKey, value); 350 } 351 } 352 } 353 | Popular Tags |