| 534 | } |
| 535 | |
| 536 | @Override |
| 537 | public Iterator<Relation> iterator() { |
| 538 | |
| 539 | final Set<Relation> copy = new HashSet<Relation>(indexById.values()); |
| 540 | |
| 541 | return new Iterator<Relation>() { |
| 542 | private Relation nextElement, currentElement; |
| 543 | |
| 544 | private boolean hasNext; |
| 545 | |
| 546 | private Iterator<Relation> iterator = copy.iterator(); |
| 547 | |
| 548 | { |
| 549 | nextMatch(); |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | public boolean hasNext() { |
| 554 | return hasNext; |
| 555 | } |
| 556 | |
| 557 | @Override |
| 558 | public Relation next() { |
| 559 | if(!hasNext) { |
| 560 | throw new NoSuchElementException(); |
| 561 | } |
| 562 | |
| 563 | return (currentElement = nextMatch()); |
| 564 | } |
| 565 | |
| 566 | private Relation nextMatch() { |
| 567 | Relation oldMatch = nextElement; |
| 568 | |
| 569 | while(iterator.hasNext()) { |
| 570 | Relation o = iterator.next(); |
| 571 | |
| 572 | if(indexById.containsValue(o)) { |
| 573 | hasNext = true; |
| 574 | nextElement = o; |
| 575 | |
| 576 | return oldMatch; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | hasNext = false; |
| 581 | |
| 582 | return oldMatch; |
| 583 | } |
| 584 | |
| 585 | @Override |
| 586 | public void remove() { |
| 587 | if(currentElement != null) deleteRelation(currentElement); |
| 588 | } |
| 589 | |
| 590 | }; |
| 591 | } |
| 592 | |
| 593 | @Override |