1 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 ; 39 import java.util.Iterator ; 40 import java.util.NoSuchElementException ; 41 42 51 public class PIndexedElemIterator implements Iterator { 52 53 56 protected Iterator iter; 57 58 61 protected GenClassElement next; 62 63 66 protected boolean nextComputed; 67 68 71 protected SpeedoAccessor sa = null; 72 73 78 protected PersistenceManager pm = null; 79 80 protected Logger logger = null; 81 82 83 public PIndexedElemIterator(Collection _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 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 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 next() { 132 if (!nextComputed) { 133 hasNext(); 134 } 135 if (next == null) { 136 throw new NoSuchElementException (); 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 |