001    /*
002      Copyright (C) 2003 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
017    
018    package org.objectweb.jac.aspects.persistence;
019    
020    import java.util.Iterator;
021    import java.util.NoSuchElementException;
022    import org.apache.log4j.Logger;
023    import org.objectweb.jac.core.Wrappee;
024    import org.objectweb.jac.core.Wrapping;
025    import org.objectweb.jac.util.ExtArrays;
026    import org.objectweb.jac.util.Log;
027    
028    public abstract class StorageIterator implements Iterator {
029        static Logger logger = Logger.getLogger("persistence.iterator");
030    
031        int index = 0;
032       
033        OID cid;
034        Storage storage;
035        Wrappee collection;
036    
037        public StorageIterator(Wrappee collection) {
038            this.collection = collection;
039            storage = (Storage)Wrapping.invokeRoleMethod(collection,"getStorage",ExtArrays.emptyObjectArray);
040            cid = (OID)Wrapping.invokeRoleMethod(collection,"getOID",ExtArrays.emptyObjectArray);
041        }
042    
043        public boolean hasNext() {
044            try {
045                logger.debug(cid+".hasNext(): "+
046                          index+"/"+getCollectionSize());
047                if(index<getCollectionSize()) {
048                    return true;
049                }
050            } catch(Exception e) {
051                e.printStackTrace();
052            }
053            return false;
054        }
055    
056        public Object next() throws NoSuchElementException {
057            logger.debug(cid+".next(): "+index);
058            try {
059                Object object = (OID)storage.getListItem(cid,index++);
060                Object ret = Wrapping.invokeRoleMethod(
061                    collection,"normalizeOutput",
062                    new Object[]{object});
063                logger.debug("  =>"+ret);
064                return ret;
065            } catch (IndexOutOfBoundsException e) {
066                throw new NoSuchElementException("NoSuchElement with index="+index+" for collection "+cid);
067            } catch(Exception e) {
068                e.printStackTrace();
069            }
070            return null;
071        }
072    
073        public void remove() {
074            logger.warn("removing is not implemeted yet on storage iterators");
075        }
076    
077        /**
078         * Returns the size of the collection
079         */
080        protected abstract long getCollectionSize() throws Exception;
081    
082    }