KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > lib > MultiplePNameIterator


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; 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.jorm.lib;
19
20 import org.objectweb.jorm.api.PClassMapping;
21 import org.objectweb.jorm.api.PNameIterator;
22 import org.objectweb.jorm.api.PException;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26
27 /**
28  * is an implementation of the PNameIterator interface over several
29  * PClassMapping instances.
30  *
31  * @author S.Chassande-Barrioz
32  */

33 public class MultiplePNameIterator implements PNameIterator {
34
35     /**
36      * The array of PClassMapping instance giving all PName to return.
37      */

38     private PClassMapping[] pcms;
39
40     /**
41      * The current iterator
42      */

43     private Iterator currentIterator;
44
45     /**
46      * The index of the current PClassMapping used
47      */

48     private int pcmIdx;
49
50     /**
51      * The connection to the data support permitting the query evaluations on
52      * data support.
53      */

54     private Object JavaDoc connection;
55
56     /**
57      * The transaction context that can be used in case of prefetching.
58      */

59     private Object JavaDoc txctx;
60
61     /**
62      * is the next PName to return at the next call to the next() method.
63      * A null value means there are no more result in any PClassMapping. This
64      * is the calculateNext() method which compute this value.
65      */

66     private Object JavaDoc nextPName;
67
68     /**
69      * indicates if the data prefetching is activated. When the prefetching is
70      * activated, the sub PNameIterator are not closed immediatly. They will
71      * be closed by the prefetching mechanism itself.
72      */

73     private boolean prefetching;
74
75     /**
76      * Builds a PNameIterator over a set of PClassMapping instances
77      * @param pcms is the PClassMapping list
78      * @param connection is the connection to the data support
79      * @param prefetching indicates if prefetching is activated
80      * @throws PException if it is not possible to fetch the first element.
81      */

82     public MultiplePNameIterator(PClassMapping[] pcms,
83                                  Object JavaDoc connection,
84                                  boolean prefetching,
85                                  Object JavaDoc txctx) throws PException {
86         this(pcms, null, connection, prefetching, txctx);
87     }
88
89     /**
90      * Builds a PNameIterator over an iterator and a set of PClassMapping
91      * instances.
92      *
93      * @param pcms is the PClassMapping list
94      * @param currentIt is the first iterator to use which has not been obtained
95      * through a PClassMapping given in parameter.
96      * @param connection is the connection to the data support
97      * @param prefetching indicates if prefetching is activated
98      * @throws PException if it is not possible to fetch the first element.
99      */

100     public MultiplePNameIterator(PClassMapping[] pcms,
101                                  Iterator currentIt,
102                                  Object JavaDoc connection,
103                                  boolean prefetching,
104                                  Object JavaDoc txctx) throws PException {
105         this.pcms = pcms;
106         this.prefetching = prefetching;
107         this.connection = connection;
108         currentIterator = currentIt;
109         this.txctx = txctx;
110         pcmIdx = -1;
111         calculateNext();
112     }
113
114     /**
115      * Calculates the next element. The next element is stored into the
116      * nextPName variable. At the end if nextPName == null, that means not next
117      * element has been found.
118      * @throws PException if it is not possible to evaluate a sub PNameIterator
119      */

120     private void calculateNext() throws PException {
121         if (currentIterator != null && currentIterator.hasNext()) {
122             nextPName = currentIterator.next();
123         } else {
124             nextPName = null;
125             //close the previous iterator
126
if (!prefetching && currentIterator instanceof PNameIterator) {
127                 ((PNameIterator) currentIterator).close();
128             }
129             //use the next PClassMapping
130
pcmIdx ++;
131             if (pcmIdx < pcms.length) {
132                 //compute the next iterator
133
currentIterator = pcms[pcmIdx]
134                     .getPNameIterator(connection, true, prefetching, txctx);
135                 //recall to find the next PName
136
calculateNext();
137             } // else no next PCM then the iteration is finished
138
}
139     }
140
141     /**
142      * @return true if there is a next PName
143      */

144     public boolean hasNext() {
145         return nextPName != null;
146     }
147
148     public Object JavaDoc next() {
149         if (nextPName == null) {
150             throw new NoSuchElementException JavaDoc();
151         }
152         Object JavaDoc res = nextPName;
153         try {
154             calculateNext();
155         } catch (PException e) {
156             nextPName = null;
157         }
158         return res;
159     }
160
161     /**
162      * The remove operation is not supported
163      */

164     public void remove() {
165         throw new UnsupportedOperationException JavaDoc("Remove operation not supported");
166     }
167
168     /**
169      * Close the PNameIterator is the prefetch is not activated.
170      * @throws PException
171      */

172     public void close() throws PException {
173         nextPName = null;
174         if (!prefetching && currentIterator instanceof PNameIterator) {
175             ((PNameIterator) currentIterator).close();
176         }
177     }
178 }
179
Popular Tags