View Javadoc

1   /*
2   * E-nspire Gemini.
3   * A Java and AspectJ based framework that enables transparent 
4   * bidirectional relationships between Plain Old Java Objects.
5   * 
6   * Copyright (C) 2005 Dragan Djuric
7   * 
8   * This program is free software; you can redistribute it and/or
9   * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  * 
22  * Contact the author at dragand@dev.java.net
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 }