1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.enspire.gemini.bidirectional;
25
26 import java.util.Iterator;
27
28 import com.enspire.gemini.BidirectionalProperty;
29
30 /***
31 * <p><a href="http://www.e-nspire.com">e-nspire site</a></p>
32 * Decorator around another <code>Iterator</code>. Ensures bidirectional
33 * behaviour over iterated elements.
34 *
35 * @author Dragan Djuric <dragand@dev.java.net>
36 * @since 1.0
37 */
38 public class BidirectionalIterator implements Iterator {
39
40 private Iterator decorated;
41 private BidirectionalProperty bidirectionalProperty;
42 private Object last;
43
44 /***
45 * Constructor that decorates the specified iterator.
46 *
47 * @param bidirectionalProperty the source of the decorated iterator
48 * @param iterator the iterator to decorate, must not be null
49 * @throws IllegalArgumentException if the collection is null
50 */
51 public BidirectionalIterator(BidirectionalProperty bidirectionalProperty,
52 Iterator iterator) {
53 if (iterator == null || bidirectionalProperty == null) {
54 throw new IllegalArgumentException("Iterator must not be null");
55 }
56 this.decorated = iterator;
57 this.bidirectionalProperty = bidirectionalProperty;
58 }
59
60 /***
61 * @return Returns the decorated.
62 */
63 public Iterator getDecorated() {
64 return this.decorated;
65 }
66
67 /***
68 * @return Returns the bidirectionalProperty.
69 */
70 public BidirectionalProperty getBidirectionalProperty() {
71 return this.bidirectionalProperty;
72 }
73
74 /***
75 * @return Returns the last.
76 */
77 public Object getLast() {
78 return this.last;
79 }
80
81 /***
82 * @see java.util.Iterator#hasNext()
83 */
84 public boolean hasNext() {
85 return this.getDecorated().hasNext();
86 }
87
88 /***
89 * @see java.util.Iterator#next()
90 */
91 public Object next() {
92 this.last = this.getDecorated().next();
93 return this.last;
94 }
95
96 /***
97 * Removes the current element and updates the opposite property.
98 * @see java.util.Iterator#remove()
99 */
100 public void remove() {
101 getDecorated().remove();
102 getBidirectionalProperty().getRelationshipUpdater().unset(
103 getLast(), getBidirectionalProperty().getOppositeName(),
104 getBidirectionalProperty().getOwner());
105 }
106
107 }