1 16 package org.apache.myfaces.context.servlet; 17 18 import java.util.AbstractSet ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Enumeration ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.NoSuchElementException ; 26 import java.util.Set ; 27 28 29 49 public abstract class AbstractAttributeMap 50 implements Map 51 { 52 private Set _keySet; 53 private Collection _values; 54 private Set _entrySet; 55 56 public void clear() 57 { 58 List names = new ArrayList (); 59 for (Enumeration e = getAttributeNames(); e.hasMoreElements();) 60 { 61 names.add(e.nextElement()); 62 } 63 64 for (Iterator it = names.iterator(); it.hasNext();) 65 { 66 removeAttribute((String ) it.next()); 67 } 68 } 69 70 public boolean containsKey(Object key) 71 { 72 return getAttribute(key.toString()) != null; 73 } 74 75 public boolean containsValue(Object findValue) 76 { 77 if (findValue == null) 78 { 79 return false; 80 } 81 82 for (Enumeration e = getAttributeNames(); e.hasMoreElements();) 83 { 84 Object value = getAttribute((String ) e.nextElement()); 85 if (findValue.equals(value)) 86 { 87 return true; 88 } 89 } 90 91 return false; 92 } 93 94 public Set entrySet() 95 { 96 return (_entrySet != null) ? _entrySet : (_entrySet = new EntrySet()); 97 } 98 99 public Object get(Object key) 100 { 101 return getAttribute(key.toString()); 102 } 103 104 public boolean isEmpty() 105 { 106 return !getAttributeNames().hasMoreElements(); 107 } 108 109 public Set keySet() 110 { 111 return (_keySet != null) ? _keySet : (_keySet = new KeySet()); 112 } 113 114 public Object put(Object key, Object value) 115 { 116 String key_ = key.toString(); 117 Object retval = getAttribute(key_); 118 setAttribute(key_, value); 119 return retval; 120 } 121 122 public void putAll(Map t) 123 { 124 for (Iterator it = t.entrySet().iterator(); it.hasNext();) 125 { 126 Entry entry = (Entry) it.next(); 127 setAttribute(entry.getKey().toString(), entry.getValue()); 128 } 129 } 130 131 public Object remove(Object key) 132 { 133 String key_ = key.toString(); 134 Object retval = getAttribute(key_); 135 removeAttribute(key_); 136 return retval; 137 } 138 139 public int size() 140 { 141 int size = 0; 142 for (Enumeration e = getAttributeNames(); e.hasMoreElements();) 143 { 144 size++; 145 e.nextElement(); 146 } 147 return size; 148 } 149 150 public Collection values() 151 { 152 return (_values != null) ? _values : (_values = new Values()); 153 } 154 155 156 abstract protected Object getAttribute(String key); 157 158 abstract protected void setAttribute(String key, Object value); 159 160 abstract protected void removeAttribute(String key); 161 162 abstract protected Enumeration getAttributeNames(); 163 164 165 private class KeySet extends AbstractSet 166 { 167 public Iterator iterator() 168 { 169 return new KeyIterator(); 170 } 171 172 public boolean isEmpty() 173 { 174 return AbstractAttributeMap.this.isEmpty(); 175 } 176 177 public int size() 178 { 179 return AbstractAttributeMap.this.size(); 180 } 181 182 public boolean contains(Object o) 183 { 184 return AbstractAttributeMap.this.containsKey(o); 185 } 186 187 public boolean remove(Object o) 188 { 189 return AbstractAttributeMap.this.remove(o) != null; 190 } 191 192 public void clear() 193 { 194 AbstractAttributeMap.this.clear(); 195 } 196 } 197 198 private class KeyIterator 199 implements Iterator 200 { 201 protected final Enumeration _e = getAttributeNames(); 202 protected Object _currentKey; 203 204 public void remove() 205 { 206 if (_currentKey == null) 210 { 211 throw new NoSuchElementException ( 212 "You must call next() at least once"); 213 } 214 AbstractAttributeMap.this.remove(_currentKey); 215 } 216 217 public boolean hasNext() 218 { 219 return _e.hasMoreElements(); 220 } 221 222 public Object next() 223 { 224 return _currentKey = _e.nextElement(); 225 } 226 } 227 228 private class Values extends KeySet 229 { 230 public Iterator iterator() 231 { 232 return new ValuesIterator(); 233 } 234 235 public boolean contains(Object o) 236 { 237 return AbstractAttributeMap.this.containsValue(o); 238 } 239 240 public boolean remove(Object o) 241 { 242 if (o == null) 243 { 244 return false; 245 } 246 247 for (Iterator it = iterator(); it.hasNext();) 248 { 249 if (o.equals(it.next())) 250 { 251 it.remove(); 252 return true; 253 } 254 } 255 256 return false; 257 } 258 } 259 260 private class ValuesIterator extends KeyIterator 261 { 262 public Object next() 263 { 264 super.next(); 265 return AbstractAttributeMap.this.get(_currentKey); 266 } 267 } 268 269 private class EntrySet extends KeySet 270 { 271 public Iterator iterator() { 272 return new EntryIterator(); 273 } 274 275 public boolean contains(Object o) { 276 if (!(o instanceof Entry)) 277 { 278 return false; 279 } 280 281 Entry entry = (Entry) o; 282 Object key = entry.getKey(); 283 Object value = entry.getValue(); 284 if (key == null || value == null) 285 { 286 return false; 287 } 288 289 return value.equals(AbstractAttributeMap.this.get(key)); 290 } 291 292 public boolean remove(Object o) { 293 if (!(o instanceof Entry)) 294 { 295 return false; 296 } 297 298 Entry entry = (Entry) o; 299 Object key = entry.getKey(); 300 Object value = entry.getValue(); 301 if (key == null || value == null 302 || !value.equals(AbstractAttributeMap.this.get(key))) 303 { 304 return false; 305 } 306 307 return AbstractAttributeMap.this.remove(((Entry) o).getKey()) != null; 308 } 309 } 310 311 316 private class EntryIterator extends KeyIterator 317 { 318 public Object next() 319 { 320 super.next(); 321 return new EntrySetEntry(_currentKey); 324 } 325 } 326 327 private class EntrySetEntry implements Entry 328 { 329 private final Object _currentKey; 330 331 public EntrySetEntry(Object currentKey) 332 { 333 _currentKey = currentKey; 334 } 335 336 public Object getKey() 337 { 338 return _currentKey; 339 } 340 341 public Object getValue() 342 { 343 return AbstractAttributeMap.this.get(_currentKey); 344 } 345 346 public Object setValue(Object value) 347 { 348 return AbstractAttributeMap.this.put(_currentKey, value); 349 } 350 } 351 } 352 | Popular Tags |