1 48 49 package org.ozoneDB.collections; 50 51 import java.util.Collection ; 52 import java.util.Iterator ; 53 import java.util.Map ; 54 import java.util.Set ; 55 import org.ozoneDB.OzoneObject; 56 57 76 public abstract class AbstractOzoneMap extends OzoneObject implements OzoneMap { 77 78 private static final long serialVersionUID = 1L; 79 80 81 83 static final int KEYS = 0; 84 85 static final int VALUES = 1; 86 87 static final int ENTRIES = 2; 88 89 92 protected Set keys; 93 94 97 protected Collection values; 98 99 102 protected AbstractOzoneMap() { 103 } 104 105 116 public void clear() { 117 entrySet().clear(); 118 } 119 120 129 protected Object clone() throws CloneNotSupportedException { 130 AbstractOzoneMap copy = (AbstractOzoneMap) super.clone(); 132 copy.keys = null; 134 copy.values = null; 135 return copy; 136 } 137 138 151 public boolean containsKey(Object key) { 152 Iterator entries = ((OzoneSet) entrySet())._org_ozoneDB_internalIterator(); 153 int pos = size(); 154 while (--pos >= 0) 155 if (equals(key, ((Map.Entry ) entries.next()).getKey())) 156 return true; 157 return false; 158 } 159 160 172 public boolean containsValue(Object value) { 173 Iterator entries = ((OzoneSet) entrySet())._org_ozoneDB_internalIterator(); 174 int pos = size(); 175 while (--pos >= 0) 176 if (equals(value, ((Map.Entry ) entries.next()).getValue())) 177 return true; 178 return false; 179 } 180 181 194 public abstract Set entrySet(); 195 196 206 public boolean equals(Object o) { 207 return super.equals(o) || (o instanceof Map && entrySet().equals(((Map ) o).entrySet())); 208 } 209 210 222 public Object get(Object key) { 223 Iterator entries = ((OzoneSet) entrySet())._org_ozoneDB_internalIterator(); 224 int pos = size(); 225 while (--pos >= 0) { 226 Map.Entry entry = (Map.Entry ) entries.next(); 227 if (equals(key, entry.getKey())) 228 return entry.getValue(); 229 } 230 return null; 231 } 232 233 242 public int hashCode() { 243 return entrySet().hashCode(); 244 } 245 246 253 public boolean isEmpty() { 254 return size() == 0; 255 } 256 257 276 public Set keySet() { 277 if (keys == null) { 278 keys = (Set ) database().createObject(_AbstractOzoneMap_keySet.class, 283 new Class [] {OzoneMap.class}, 284 new Object [] {self()} 285 ); 286 } 287 return keys; 289 } 290 291 308 public Object put(Object key, Object value) { 309 throw new UnsupportedOperationException (); 310 } 311 312 327 public void putAll(Map m) { 328 Iterator entries; 329 if (m instanceof OzoneMap) { 330 entries = ((OzoneSet) entrySet())._org_ozoneDB_internalIterator(); 331 } else { 332 entries = m.entrySet().iterator(); 333 } 334 int pos = m.size(); 335 while (--pos >= 0) { 336 Map.Entry entry = (Map.Entry ) entries.next(); 337 put(entry.getKey(), entry.getValue()); 338 } 339 } 340 341 356 public Object remove(Object key) { 357 Iterator entries = ((OzoneSet) entrySet())._org_ozoneDB_internalIterator(); 358 int pos = size(); 359 while (--pos >= 0) { 360 Map.Entry entry = (Map.Entry ) entries.next(); 361 if (equals(key, entry.getKey())) { 362 Object r = entry.getValue(); 364 entries.remove(); 365 return r; 366 } 367 } 368 return null; 369 } 370 371 379 public int size() { 380 return entrySet().size(); 381 } 382 383 396 public String toString() { 397 Iterator entries = ((OzoneSet) entrySet())._org_ozoneDB_internalIterator(); 398 StringBuffer r = new StringBuffer ("{"); 399 for (int pos = size(); pos > 0; pos--) { 400 Map.Entry entry = (Map.Entry ) entries.next(); 401 r.append(entry.getKey()); 402 r.append('='); 403 r.append(entry.getValue()); 404 if (pos > 1) 405 r.append(", "); 406 } 407 r.append("}"); 408 return r.toString(); 409 } 410 411 431 public Collection values() { 432 if (values == null) { 433 values = (Collection ) database().createObject( 437 _AbstractOzoneMap_values.class, 438 new Class [] {OzoneMap.class}, 439 new Object [] {self()} 440 ); 441 } 442 return values; 443 } 444 445 452 static final boolean equals(Object o1, Object o2) { 455 return o1 == null ? o2 == null : o2 == null ? false : o1.equals(o2); 456 } 457 458 464 static final int hashCode(Object o) { 467 return o == null ? 0 : o.hashCode(); 468 } 469 470 public OzoneCollection ozoneValues() { 471 return (OzoneCollection) values(); 472 } 473 474 public OzoneSet ozoneKeySet() { 475 return (OzoneSet) keySet(); 476 } 477 478 public OzoneSet ozoneEntrySet() { 479 return (OzoneSet) entrySet(); 480 } 481 482 interface Node extends OzoneMap.Node { 483 484 } 485 486 487 class AbstractNode implements Node { 488 489 private Object key; 490 491 private Object value; 492 493 498 public AbstractNode(Object newKey, Object newValue) { 499 key = newKey; 500 value = newValue; 501 } 502 503 511 public final boolean equals(Object o) { 512 boolean result; 513 if (! (o instanceof Map.Entry )) { 514 result = false; 515 } else if (o instanceof AbstractNode) { 517 AbstractNode e = (AbstractNode) o; 518 result = (AbstractOzoneMap.equals(key, e.key) && AbstractOzoneMap.equals(value, e.value)); 519 } else { 520 Map.Entry e = (Map.Entry ) o; 521 result = (AbstractOzoneMap.equals(key, e.getKey()) && AbstractOzoneMap.equals(value, e.getValue())); 522 } 523 return result; 524 } 525 526 531 public final Object getKey() { 532 return key; 533 } 534 535 541 public final Object getValue() { 542 return value; 543 } 544 545 555 public final int hashCode() { 556 return (AbstractOzoneMap.hashCode(key) ^ AbstractOzoneMap.hashCode(value)); 557 } 558 559 568 public Object setValue(Object newVal) { 569 Object r = value; 570 value = newVal; 571 return r; 572 } 573 574 580 public final String toString() { 581 return key + "=" + value; 582 } 583 584 public void setKey(Object key) { 585 this.key = key; 586 } 587 588 } 589 590 } | Popular Tags |