KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > persistence > StorageIterator


1 /*
2   Copyright (C) 2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.persistence;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
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 JavaDoc {
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 JavaDoc e) {
51             e.printStackTrace();
52         }
53         return false;
54     }
55
56     public Object JavaDoc next() throws NoSuchElementException JavaDoc {
57         logger.debug(cid+".next(): "+index);
58         try {
59             Object JavaDoc object = (OID)storage.getListItem(cid,index++);
60             Object JavaDoc ret = Wrapping.invokeRoleMethod(
61                 collection,"normalizeOutput",
62                 new Object JavaDoc[]{object});
63             logger.debug(" =>"+ret);
64             return ret;
65         } catch (IndexOutOfBoundsException JavaDoc e) {
66             throw new NoSuchElementException JavaDoc("NoSuchElement with index="+index+" for collection "+cid);
67         } catch(Exception JavaDoc 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     /**
78      * Returns the size of the collection
79      */

80     protected abstract long getCollectionSize() throws Exception JavaDoc;
81
82 }
83
Popular Tags