KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > genclass > PIndexedElemIterator


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28
29 package org.objectweb.speedo.genclass;
30
31 import org.objectweb.jorm.api.PIndexedElem;
32 import org.objectweb.util.monolog.api.BasicLevel;
33 import org.objectweb.util.monolog.api.Logger;
34 import org.objectweb.speedo.mim.api.SpeedoAccessor;
35
36 import javax.jdo.JDOException;
37 import javax.jdo.PersistenceManager;
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.NoSuchElementException JavaDoc;
41
42 /**
43  * This class is an implementation of the iterator interface. It iterates over
44  * an inner iterator which the elements are GenClassElement. The main roles of
45  * this iterator implementation are
46  * - to skip the GenClassElement marked as deleted
47  * - to resolve PName references if the element are persistent object
48  *
49  * @author Sebastien Chassande-Barrioz
50  */

51 public class PIndexedElemIterator implements Iterator JavaDoc {
52
53     /**
54      * Is the inner iterator over PIndexexElem instances
55      */

56     protected Iterator JavaDoc iter;
57
58     /**
59      * is the next element which has not been deleted
60      */

61     protected GenClassElement next;
62
63     /**
64      * indicates if the next element has been computed
65      */

66     protected boolean nextComputed;
67
68     /**
69      * is the object to synchronize if an element must be removed
70      */

71     protected SpeedoAccessor sa = null;
72
73     /**
74      * is the persistence manager used to resolve the PName into reference.
75      * If this field has a null value, that means the elements of the gen class
76      * are not references but primitives.
77      */

78     protected PersistenceManager pm = null;
79
80     protected Logger logger = null;
81
82
83     public PIndexedElemIterator(Collection JavaDoc _elements, SpeedoAccessor _sa,
84             PersistenceManager pm, Logger l) {
85         iter = _elements.iterator();
86         sa = _sa;
87         this.pm = pm;
88         logger = l;
89     }
90
91
92     // IMPLEMENTATION OF THE Iterator INTERFACE //
93
//------------------------------------------//
94

95     public boolean hasNext() {
96         if (!nextComputed) {
97             while (iter.hasNext()) {
98                 next = (GenClassElement) iter.next();
99                 if (next.getElemStatus() != PIndexedElem.ELEM_DELETED) {
100                     try {
101                         if (pm == null) {
102                             next.getElement();
103                         } else {
104                             next.getElement(pm);
105                         }
106                         nextComputed = true;
107                         return true;
108                     } catch (JDOException e) {
109                         //The element cannot been load. It has been removed
110
//Then remove the collection element
111
logger.log(BasicLevel.DEBUG,
112                                 "Remove a GCE because referenced element cannot be reached");
113                         synchronized (sa) {
114                             if (next != null) {
115                                 sa.getSpeedoProxy().getSpeedoHome().writeIntention(sa.getSpeedoProxy(), null);
116                                 next.setStatus(PIndexedElem.ELEM_DELETED);
117                             }
118                         }
119                         
120                     }
121                 }
122             }
123             next = null;
124             nextComputed = true;
125             return false;
126         } else {
127             return next != null;
128         }
129     }
130
131     public Object JavaDoc next() {
132         if (!nextComputed) {
133             hasNext();
134         }
135         if (next == null) {
136             throw new NoSuchElementException JavaDoc();
137         } else {
138             nextComputed = false;
139             return (pm == null ? next.getElement() : next.getElement(pm));
140         }
141     }
142
143     public void remove() {
144         synchronized (sa) {
145             if (next != null) {
146                 sa.getSpeedoProxy().getSpeedoHome().writeIntention(
147                         sa.getSpeedoProxy(), null);
148                 next.setStatus(PIndexedElem.ELEM_DELETED);
149             }
150         }
151     }
152 }
153
Popular Tags