1 21 package oracle.toplink.essentials.indirection; 23 24 import java.util.*; 25 26 45 public class IndirectMap extends Hashtable implements IndirectContainer { 46 47 48 protected Hashtable delegate; 49 50 51 protected ValueHolderInterface valueHolder; 52 53 54 private transient String attributeName; 55 56 57 protected int initialCapacity = 11; 58 59 60 protected float loadFactor = 0.75f; 61 62 67 public IndirectMap() { 68 this(11); 69 } 70 71 78 public IndirectMap(int initialCapacity) { 79 this(initialCapacity, 0.75f); 80 } 81 82 93 public IndirectMap(int initialCapacity, float loadFactor) { 94 super(0); 95 this.initialize(initialCapacity, loadFactor); 96 } 97 98 105 public IndirectMap(Map m) { 106 super(0); 107 this.initialize(m); 108 } 109 110 113 protected Hashtable buildDelegate() { 114 return (Hashtable)getValueHolder().getValue(); 115 } 116 117 120 public synchronized void clear() { 121 this.getDelegate().clear(); 122 } 123 124 128 129 141 public synchronized Object clone() { 142 IndirectMap result = (IndirectMap)super.clone(); 143 result.delegate = (Hashtable)this.getDelegate().clone(); 144 return result; 145 } 146 147 150 public synchronized boolean contains(Object value) { 151 return this.getDelegate().contains(value); 152 } 153 154 157 public synchronized boolean containsKey(Object key) { 158 return this.getDelegate().containsKey(key); 159 } 160 161 164 public boolean containsValue(Object value) { 165 return this.getDelegate().containsValue(value); 166 } 167 168 171 public synchronized Enumeration elements() { 172 return this.getDelegate().elements(); 173 } 174 175 178 public Set entrySet() { 179 return new Set (){ 180 Set delegateSet = IndirectMap.this.getDelegate().entrySet(); 181 182 public int size(){ 183 return this.delegateSet.size(); 184 } 185 186 public boolean isEmpty(){ 187 return this.delegateSet.isEmpty(); 188 } 189 190 public boolean contains(Object o){ 191 return this.delegateSet.contains(o); 192 } 193 194 public Iterator iterator(){ 195 return new Iterator() { 196 Iterator delegateIterator = delegateSet.iterator(); 197 Object currentObject; 198 199 public boolean hasNext() { 200 return this.delegateIterator.hasNext(); 201 } 202 203 public Object next() { 204 this.currentObject = this.delegateIterator.next(); 205 return this.currentObject; 206 } 207 208 public void remove() { 209 raiseRemoveChangeEvent(((Map.Entry)currentObject).getKey(), ((Map.Entry)currentObject).getValue()); 210 this.delegateIterator.remove(); 211 } 212 }; 213 } 214 215 public Object [] toArray(){ 216 return this.delegateSet.toArray(); 217 } 218 219 public Object [] toArray(Object a[]){ 220 return this.delegateSet.toArray(a); 221 } 222 223 public boolean add(Object o){ 224 return this.delegateSet.add(o); 225 } 226 227 public boolean remove(Object o){ 228 if (!(o instanceof Map.Entry)) { 229 return false; 230 } 231 return (IndirectMap.this.remove(((Map.Entry)o).getKey()) != null); 232 } 233 234 public boolean containsAll(Collection c){ 235 return this.delegateSet.containsAll(c); 236 } 237 238 public boolean addAll(Collection c){ 239 return this.delegateSet.addAll(c); 240 } 241 242 public boolean retainAll(Collection c){ 243 boolean result = false; 244 Iterator objects = delegateSet.iterator(); 245 while (objects.hasNext()) { 246 Map.Entry object = (Map.Entry)objects.next(); 247 if (!c.contains(object)) { 248 objects.remove(); 249 raiseRemoveChangeEvent(object.getKey(), object.getValue()); 250 result = true; 251 } 252 } 253 return result; 254 } 255 256 public boolean removeAll(Collection c){ 257 boolean result = false; 258 for (Iterator cs = c.iterator(); cs.hasNext(); ){ 259 Object object = cs.next(); 260 if ( ! (object instanceof Map.Entry)){ 261 continue; 262 } 263 Object removed = IndirectMap.this.remove(((Map.Entry)object).getKey()); 264 if (removed != null){ 265 result = true; 266 } 267 } 268 return result; 269 } 270 271 public void clear(){ 272 IndirectMap.this.clear(); 273 } 274 275 public boolean equals(Object o){ 276 return this.delegateSet.equals(o); 277 } 278 279 public int hashCode(){ 280 return this.delegateSet.hashCode(); 281 } 282 }; 283 } 284 285 288 public synchronized boolean equals(Object o) { 289 return this.getDelegate().equals(o); 290 } 291 292 295 public synchronized Object get(Object key) { 296 return this.getDelegate().get(key); 297 } 298 299 303 protected synchronized Hashtable getDelegate() { 304 if (delegate == null) { 305 delegate = this.buildDelegate(); 306 } 307 return delegate; 308 } 309 310 313 public String getTopLinkAttributeName() { 314 return attributeName; 315 } 316 317 321 public synchronized ValueHolderInterface getValueHolder() { 322 if (valueHolder == null) { 324 valueHolder = new ValueHolder(new Hashtable(initialCapacity, loadFactor)); 325 } 326 return valueHolder; 327 } 328 329 332 public synchronized int hashCode() { 333 return this.getDelegate().hashCode(); 334 } 335 336 339 protected void initialize(int initialCapacity, float loadFactor) { 340 this.delegate = null; 341 this.loadFactor = loadFactor; 342 this.initialCapacity = initialCapacity; 343 this.valueHolder = null; 344 } 345 346 349 protected void initialize(Map m) { 350 this.delegate = null; 351 Hashtable temp = new Hashtable(m); 352 353 this.valueHolder = new ValueHolder(temp); 354 } 355 356 359 public boolean isEmpty() { 360 return this.getDelegate().isEmpty(); 361 } 362 363 367 public boolean isInstantiated() { 368 return this.getValueHolder().isInstantiated(); 369 } 370 371 374 public synchronized Enumeration keys() { 375 return this.getDelegate().keys(); 376 } 377 378 381 public Set keySet() { 382 383 return new Set (){ 384 Set delegateSet = IndirectMap.this.getDelegate().keySet(); 385 386 public int size(){ 387 return this.delegateSet.size(); 388 } 389 390 public boolean isEmpty(){ 391 return this.delegateSet.isEmpty(); 392 } 393 394 public boolean contains(Object o){ 395 return this.delegateSet.contains(o); 396 } 397 398 public Iterator iterator(){ 399 return new Iterator() { 400 Iterator delegateIterator = delegateSet.iterator(); 401 Object currentObject; 402 403 public boolean hasNext() { 404 return this.delegateIterator.hasNext(); 405 } 406 407 public Object next() { 408 this.currentObject = this.delegateIterator.next(); 409 return this.currentObject; 410 } 411 412 public void remove() { 413 IndirectMap.this.raiseRemoveChangeEvent(currentObject, IndirectMap.this.getDelegate().get(currentObject)); 414 this.delegateIterator.remove(); 415 } 416 }; 417 } 418 419 public Object [] toArray(){ 420 return this.delegateSet.toArray(); 421 } 422 423 public Object [] toArray(Object a[]){ 424 return this.delegateSet.toArray(a); 425 } 426 427 public boolean add(Object o){ 428 return this.delegateSet.add(o); 429 } 430 431 public boolean remove(Object o){ 432 return (IndirectMap.this.remove(o) != null); 433 } 434 435 public boolean containsAll(Collection c){ 436 return this.delegateSet.containsAll(c); 437 } 438 439 public boolean addAll(Collection c){ 440 return this.delegateSet.addAll(c); 441 } 442 443 public boolean retainAll(Collection c){ 444 boolean result = false; 445 Iterator objects = delegateSet.iterator(); 446 while (objects.hasNext()) { 447 Object object = objects.next(); 448 if (!c.contains(object)) { 449 objects.remove(); 450 IndirectMap.this.raiseRemoveChangeEvent(object, IndirectMap.this.getDelegate().get(object)); 451 result = true; 452 } 453 } 454 return result; 455 } 456 457 public boolean removeAll(Collection c){ 458 boolean result = false; 459 for (Iterator cs = c.iterator(); cs.hasNext(); ){ 460 if (IndirectMap.this.remove(cs.next()) != null ) { 461 result = true; 462 } 463 } 464 return result; 465 } 466 467 public void clear(){ 468 IndirectMap.this.clear(); 469 } 470 471 public boolean equals(Object o){ 472 return this.delegateSet.equals(o); 473 } 474 475 public int hashCode(){ 476 return this.delegateSet.hashCode(); 477 } 478 }; 479 480 481 } 482 483 486 public synchronized Object put(Object key, Object value) { 487 Object oldValue = this.getDelegate().put(key, value); 488 if (oldValue != null){ 489 raiseRemoveChangeEvent(key, oldValue); 490 } 491 raiseAddChangeEvent(key, value); 492 return oldValue; 493 } 494 495 496 499 public synchronized void putAll(Map t) { 500 this.getDelegate().putAll(t); 501 } 502 503 506 protected void rehash() { 507 throw new InternalError ("unsupported"); 508 } 509 510 513 protected void raiseAddChangeEvent(Object key, Object value) { 514 } 516 517 520 protected void raiseRemoveChangeEvent(Object key, Object value) { 521 } 523 524 527 public synchronized Object remove(Object key) { 528 Object value = this.getDelegate().remove(key); 529 if (value != null){ 530 raiseRemoveChangeEvent(key, value); 531 } 532 return value; 533 } 534 535 539 public void setTopLinkAttributeName(String attributeName) { 540 this.attributeName = attributeName; 541 } 542 543 547 public void setValueHolder(ValueHolderInterface valueHolder) { 548 this.delegate = null; 549 this.valueHolder = valueHolder; 550 } 551 552 555 public int size() { 556 return this.getDelegate().size(); 557 } 558 559 566 public String toString() { 567 if (ValueHolderInterface.shouldToStringInstantiate) { 568 return this.getDelegate().toString(); 569 } 570 if (this.isInstantiated()) { 571 return "{" + this.getDelegate().toString() + "}"; 572 } else { 573 return "{" + oracle.toplink.essentials.internal.helper.Helper.getShortClassName(this.getClass()) + ": not instantiated}"; 574 } 575 } 576 577 580 public Collection values() { 581 return new Collection() { 582 protected Collection delegateCollection = IndirectMap.this.getDelegate().values(); 583 584 public int size(){ 585 return delegateCollection.size(); 586 } 587 588 public boolean isEmpty(){ 589 return delegateCollection.isEmpty(); 590 } 591 592 public boolean contains(Object o){ 593 return delegateCollection.contains(o); 594 } 595 596 public Iterator iterator() { 597 return new Iterator() { 598 Iterator delegateIterator = delegateCollection.iterator(); 599 Object currentObject; 600 601 public boolean hasNext() { 602 return this.delegateIterator.hasNext(); 603 } 604 605 public Object next() { 606 this.currentObject = this.delegateIterator.next(); 607 return this.currentObject; 608 } 609 610 public void remove() { 611 Iterator iterator = IndirectMap.this.getDelegate().entrySet().iterator(); 612 while (iterator.hasNext()){ 613 Map.Entry entry = (Map.Entry)iterator.next(); 614 if (entry.getValue().equals(currentObject)){ 615 IndirectMap.this.raiseRemoveChangeEvent(entry.getKey(), entry.getValue()); 616 } 617 618 } 619 this.delegateIterator.remove(); 620 } 621 }; 622 } 623 624 public Object [] toArray(){ 625 return this.delegateCollection.toArray(); 626 } 627 628 public Object [] toArray(Object a[]){ 629 return this.delegateCollection.toArray(a); 630 } 631 632 public boolean add(Object o){ 633 return this.delegateCollection.add(o); 634 } 635 636 public boolean remove(Object o){ 637 Iterator iterator = IndirectMap.this.getDelegate().entrySet().iterator(); 638 while (iterator.hasNext()){ 639 Map.Entry entry = (Map.Entry)iterator.next(); 640 if (entry.getValue().equals(o)){ 641 IndirectMap.this.raiseRemoveChangeEvent(entry.getKey(), entry.getValue()); 642 } 643 return true; 644 } 645 return false; 646 } 647 648 public boolean containsAll(Collection c){ 649 return this.delegateCollection.containsAll(c); 650 } 651 652 public boolean addAll(Collection c){ 653 return this.delegateCollection.addAll(c); 654 } 655 656 public boolean removeAll(Collection c){ 657 boolean result = false; 658 for (Iterator iterator = c.iterator(); iterator.hasNext();){ 659 if (remove(iterator.next()) ){ 660 result = true; 661 } 662 } 663 return result; 664 } 665 666 public boolean retainAll(Collection c){ 667 boolean result = false; 668 for (Iterator iterator = IndirectMap.this.entrySet().iterator(); iterator.hasNext();){ 669 Map.Entry entry = (Map.Entry)iterator.next(); 670 if (! c.contains(entry.getValue()) ) { 671 iterator.remove(); 672 result = true; 673 } 674 } 675 return result; 676 } 677 678 public void clear(){ 679 IndirectMap.this.clear(); 680 } 681 682 683 public boolean equals(Object o){ 684 return this.delegateCollection.equals(o); 685 } 686 687 public int hashCode(){ 688 return this.delegateCollection.hashCode(); 689 } 690 691 }; 692 } 693 } 694 | Popular Tags |