001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2005 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.genclass;
027:
028: import org.objectweb.jorm.api.PIndexedElem;
029: import org.objectweb.util.monolog.api.BasicLevel;
030: import org.objectweb.util.monolog.api.Logger;
031: import org.objectweb.speedo.api.SpeedoRuntimeException;
032: import org.objectweb.speedo.mim.api.StateItf;
033: import org.objectweb.speedo.pm.api.POManagerItf;
034:
035: import java.util.Collection;
036: import java.util.Iterator;
037: import java.util.NoSuchElementException;
038:
039: /**
040: * This class is an implementation of the iterator interface. It iterates over
041: * an inner iterator which the elements are GenClassElement. The main roles of
042: * this iterator implementation are
043: * - to skip the GenClassElement marked as deleted
044: * - to resolve PName references if the element are persistent object
045: *
046: * @author Sebastien Chassande-Barrioz
047: */
048: public class PIndexedElemIterator implements Iterator {
049:
050: /**
051: * Is the inner iterator over PIndexexElem instances
052: */
053: protected Iterator iter;
054:
055: /**
056: * is the next element which has not been deleted
057: */
058: protected GenClassElement next;
059:
060: /**
061: * indicates if the next element has been computed
062: */
063: protected boolean nextComputed;
064:
065: /**
066: * is the object to synchronize if an element must be removed
067: */
068: protected StateItf sa = null;
069:
070: /**
071: * is the persistence manager used to resolve the PName into reference.
072: * If this field has a null value, that means the elements of the gen class
073: * are not references but primitives.
074: */
075: protected POManagerItf pm = null;
076:
077: protected Logger logger = null;
078:
079: public PIndexedElemIterator(Collection _elements, StateItf _sa,
080: POManagerItf pm, Logger l) {
081: iter = _elements.iterator();
082: sa = _sa;
083: this .pm = pm;
084: logger = l;
085: }
086:
087: // IMPLEMENTATION OF THE Iterator INTERFACE //
088: //------------------------------------------//
089:
090: public boolean hasNext() {
091: if (!nextComputed) {
092: while (iter.hasNext()) {
093: next = (GenClassElement) iter.next();
094: if (next.getElemStatus() != PIndexedElem.ELEM_DELETED) {
095: try {
096: if (pm == null) {
097: next.getElement();
098: } else {
099: next.getElement(pm);
100: }
101: nextComputed = true;
102: return true;
103: } catch (SpeedoRuntimeException e) {
104: //The element cannot been load. It has been removed
105: //Then remove the collection element
106: logger
107: .log(BasicLevel.DEBUG,
108: "Remove a GCE because referenced element cannot be reached");
109: synchronized (sa) {
110: if (next != null) {
111: sa.getSpeedoPO().speedoGetHome()
112: .writeIntention(
113: sa.getSpeedoPO(), null);
114: next
115: .setStatus(PIndexedElem.ELEM_DELETED);
116: }
117: }
118:
119: }
120: }
121: }
122: next = null;
123: nextComputed = true;
124: return false;
125: } else {
126: return next != null;
127: }
128: }
129:
130: public Object next() {
131: if (!nextComputed) {
132: hasNext();
133: }
134: if (next == null) {
135: throw new NoSuchElementException();
136: } else {
137: nextComputed = false;
138: return (pm == null ? next.getElement() : next
139: .getElement(pm));
140: }
141: }
142:
143: public void remove() {
144: synchronized (sa) {
145: if (next != null) {
146: sa.getSpeedoPO().speedoGetHome().writeIntention(
147: sa.getSpeedoPO(), null);
148: next.setStatus(PIndexedElem.ELEM_DELETED);
149: }
150: }
151: }
152: }
|