1 17 18 package org.objectweb.jac.aspects.persistence; 19 20 import java.util.Iterator ; 21 import java.util.NoSuchElementException ; 22 import org.apache.log4j.Logger; 23 import org.objectweb.jac.core.Wrappee; 24 import org.objectweb.jac.core.Wrapping; 25 import org.objectweb.jac.util.ExtArrays; 26 import org.objectweb.jac.util.Log; 27 28 public abstract class StorageIterator implements Iterator { 29 static Logger logger = Logger.getLogger("persistence.iterator"); 30 31 int index = 0; 32 33 OID cid; 34 Storage storage; 35 Wrappee collection; 36 37 public StorageIterator(Wrappee collection) { 38 this.collection = collection; 39 storage = (Storage)Wrapping.invokeRoleMethod(collection,"getStorage",ExtArrays.emptyObjectArray); 40 cid = (OID)Wrapping.invokeRoleMethod(collection,"getOID",ExtArrays.emptyObjectArray); 41 } 42 43 public boolean hasNext() { 44 try { 45 logger.debug(cid+".hasNext(): "+ 46 index+"/"+getCollectionSize()); 47 if(index<getCollectionSize()) { 48 return true; 49 } 50 } catch(Exception e) { 51 e.printStackTrace(); 52 } 53 return false; 54 } 55 56 public Object next() throws NoSuchElementException { 57 logger.debug(cid+".next(): "+index); 58 try { 59 Object object = (OID)storage.getListItem(cid,index++); 60 Object ret = Wrapping.invokeRoleMethod( 61 collection,"normalizeOutput", 62 new Object []{object}); 63 logger.debug(" =>"+ret); 64 return ret; 65 } catch (IndexOutOfBoundsException e) { 66 throw new NoSuchElementException ("NoSuchElement with index="+index+" for collection "+cid); 67 } catch(Exception e) { 68 e.printStackTrace(); 69 } 70 return null; 71 } 72 73 public void remove() { 74 logger.warn("removing is not implemeted yet on storage iterators"); 75 } 76 77 80 protected abstract long getCollectionSize() throws Exception ; 81 82 } 83 | Popular Tags |