KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > query > lib > QueryResultList


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S. Chassande-Barrioz
25  *
26  */

27
28 package org.objectweb.speedo.query.lib;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.ListIterator JavaDoc;
36 import java.util.NoSuchElementException JavaDoc;
37
38 import javax.jdo.JDOUnsupportedOptionException;
39 import javax.jdo.JDOUserException;
40 import javax.jdo.PersistenceManager;
41
42 import org.objectweb.medor.api.MedorException;
43 import org.objectweb.medor.tuple.api.TupleCollection;
44 import org.objectweb.speedo.api.SpeedoException;
45 import org.objectweb.util.monolog.api.BasicLevel;
46 import org.objectweb.util.monolog.api.Logger;
47
48 /**
49  * is the result of a JDO query. The result is exported as a java.util.List.
50  * This implementation supports user class or persistent class as query result.
51  * This implementation does not supports simultaneous iterations.
52  *
53  * @author S.Chassande-Barrioz
54  */

55 public class QueryResultList extends QueryResultCommon implements List JavaDoc {
56
57     /**
58      * if all values are loaded, this field represents the result of the query.
59      */

60     private List JavaDoc values = null;
61     
62     /**
63      * Is the current iteraor over the query result.
64      */

65     private QueryIterator iterator;
66
67     /**
68      * Builds a QueryResultList.
69      * @param _tc the tuple collection representing the query result
70      * @param _pm is the peristence manager linked to the query
71      * @param _conns is the connection to the underlying support to close in
72      * same time than the query.
73      * @param _resultClazz is the class encapsulated the result
74      */

75     public QueryResultList(TupleCollection _tc,
76             PersistenceManager _pm,
77             Object JavaDoc[] _conns,
78             Class JavaDoc _resultClazz,
79             Class JavaDoc[] _selectedFieldTypes,
80             boolean staticFirstElementIndex,
81             Logger _logger)
82             throws MedorException, SpeedoException {
83         super(_tc, _pm, _conns, _resultClazz, _selectedFieldTypes,
84                 staticFirstElementIndex, _logger);
85     }
86     
87     /**
88      *
89      * @return true if the calculation of the next element can be done,
90      * otherwise false.
91      */

92     protected boolean assertNotClosed() {
93         return pm != null;
94     }
95         
96     /**
97      * @return true if the query has not been closed
98      */

99     protected boolean assertBeforeNext() {
100         return pm != null && !pm.isClosed();
101     }
102     
103     /**
104      * Computes the query result.
105      */

106     protected List JavaDoc getList() {
107         if (!assertNotClosed()) {
108             throw new JDOUserException("Impossible to use a closed query result");
109         }
110         if (!assertBeforeNext()) {
111             throw new JDOUserException(
112                     "Impossible to use a query result without opened persistence manager");
113         }
114         if (values != null)
115             return values;
116
117         values = new ArrayList JavaDoc();
118         try {
119             if (!tc.isEmpty()) {
120                 tc.first();
121                 do {
122                     Object JavaDoc o = getValue(tc.getTuple());
123                     if (o != null) {
124                         values.add(o);
125                     }
126                 } while (tc.next());
127             }
128         } catch (MedorException me) {
129             values = null;
130             throw new JDOUnsupportedOptionException(
131                     "Persistence manager problem to get result", new Exception JavaDoc[]{me});
132         }
133         return values;
134     }
135     
136     private class QueryIterator implements Iterator JavaDoc {
137
138         private Object JavaDoc next;
139         private int idx = 0;
140
141         public QueryIterator() {
142             try {
143                 tc.first();
144                 if (assertBeforeNext()) {
145                     calculateNext(true);
146                 } else {
147                     next = null;
148                 }
149             } catch (MedorException e) {
150                 next = null;
151             }
152         }
153
154         public void desactivate() {
155             next = null;
156         }
157         public boolean hasNext() {
158             return next != null;
159         }
160
161         public Object JavaDoc next() {
162             if (next == null) {
163                 throw new NoSuchElementException JavaDoc("No such element");
164             }
165             Object JavaDoc res = next;
166             calculateNext(false);
167             return res;
168         }
169
170         private void calculateNext(boolean isfirst) {
171             boolean first = isfirst;
172             next = null;
173             try {
174                 while(next == null && (first || tc.next())) {
175                     first = false;
176                     next = getValue(tc.getTuple());
177                 }
178             } catch (MedorException e) {
179                 logger.log(BasicLevel.ERROR, "Impossible to fetch the pname", e);
180                 throw new NoSuchElementException JavaDoc(e.getMessage());
181             }
182         }
183
184         public void remove() {
185             throw new UnsupportedOperationException JavaDoc(
186                     "This method is not supported in this implementation");
187         }
188     }
189
190     // IMPLEMENTATION OF THE Collection INTERFACE //
191
//--------------------------------------------//
192

193     /**
194      * Ensures that this collection contains the specified element
195      * (optional operation).
196      */

197     public boolean add(Object JavaDoc o) {
198         throw new JDOUnsupportedOptionException(
199                 "This method is not supported in this implementation");
200     }
201
202     /**
203      * Adds all of the elements in the specified collection to this collection
204      * (optional operation).
205      */

206     public boolean addAll(Collection JavaDoc c) {
207         throw new JDOUnsupportedOptionException(
208                 "This method is not supported in this implementation");
209     }
210
211     /**
212      * Removes all of the elements from this collection
213      * (optional operation).
214      */

215     public void clear() {
216         throw new JDOUnsupportedOptionException(
217                 "This method is not supported in this implementation");
218     }
219
220     /**
221      * Returns true if this collection contains the specified element.
222      */

223     public boolean contains(Object JavaDoc o) {
224         return indexOf(o) != -1;
225     }
226
227     /**
228      * Returns true if this collection contains all of the elements in the
229      * specified collection.
230      */

231     public boolean containsAll(Collection JavaDoc c) {
232         if (values != null) {
233             return values.containsAll(c);
234         }
235         ArrayList JavaDoc tofind = new ArrayList JavaDoc(c);
236         for(Iterator JavaDoc it = iterator(); it.hasNext() && !tofind.isEmpty();) {
237             Object JavaDoc current = it.next();
238             boolean found = false;
239             for (Iterator JavaDoc iter = tofind.iterator(); iter.hasNext() && !found;) {
240                 Object JavaDoc o = iter.next();
241                 if ((o == null && current == null)
242                         || (o != null && o.equals(current))){
243                     found = true;
244                     iter.remove();
245                 }
246             }
247             if (!found) {
248                 return false;
249             }
250         }
251         return true;
252     }
253
254     /**
255      * Compares the specified object with this collection for equality.
256      */

257     public boolean equals(Object JavaDoc o) {
258         throw new JDOUnsupportedOptionException(
259                 "This method is not supported in this implementation");
260     }
261
262     /**
263      * Returns the hash code value for this collection.
264      */

265     public int hashCode() {
266         return getList().hashCode();
267     }
268
269     /**
270      * Returns true if this collection contains no elements.
271      */

272     public boolean isEmpty() {
273         try {
274             return tc == null || tc.isEmpty();
275         } catch (MedorException me) {
276             throw new JDOUnsupportedOptionException(
277                     "a MedorException has been catched",
278                     new Exception JavaDoc[]{me});
279         }
280     }
281
282     /**
283      * Returns an iterator over the elements in this collection.
284      */

285     public Iterator JavaDoc iterator() {
286         if (iterator != null) {
287             iterator.desactivate();
288         }
289         iterator = new QueryIterator();
290         return iterator;
291     }
292
293     /**
294      * Removes a single instance of the specified element from this collection,
295      * if it is present (optional operation).
296      */

297     public boolean remove(Object JavaDoc o) {
298         throw new JDOUnsupportedOptionException(
299                 "This method is not supported in this implementation");
300     }
301
302     /**
303      * Removes all this collection's elements that are also contained in the
304      * specified collection (optional operation).
305      */

306     public boolean removeAll(Collection JavaDoc c) {
307         throw new JDOUnsupportedOptionException(
308                 "This method is not supported in this implementation");
309     }
310
311     /**
312      * Retains only the elements in this collection that are contained in the
313      * specified collection (optional operation).
314      */

315     public boolean retainAll(Collection JavaDoc c) {
316         throw new JDOUnsupportedOptionException(
317                 "This method is not supported in this implementation");
318     }
319
320     /**
321      * Returns the number of elements in this collection.
322      */

323     public int size() {
324         return getList().size();
325     }
326
327     /**
328      * Returns an array containing all of the elements in this collection.
329      */

330     public Object JavaDoc[] toArray() {
331         return getList().toArray();
332     }
333
334     /**
335      * Returns an array containing all of the elements in this collection whose
336      * runtime type is that of the specified array.
337      */

338     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
339         return getList().toArray(a);
340     }
341     
342     public void add(int index, Object JavaDoc element) {
343         throw new JDOUnsupportedOptionException(
344         "This method is not supported in this implementation");
345     }
346     public boolean addAll(int index, Collection JavaDoc c) {
347         throw new JDOUnsupportedOptionException(
348         "This method is not supported in this implementation");
349     }
350     public Object JavaDoc get(int index) {
351         if (values != null) {
352             return values.get(index);
353         } else {
354             int i=-1;
355             Object JavaDoc current = null;
356             for(Iterator JavaDoc it = iterator(); it.hasNext()&& i<index;i++) {
357                 current = it.next();
358             }
359             if (i == index) {
360                 return current;
361             } else {
362                 return null;
363             }
364         }
365     }
366     public int indexOf(Object JavaDoc o) {
367         if (values != null) {
368             return values.indexOf(o);
369         }
370         int i=0;
371         for(Iterator JavaDoc it = iterator(); it.hasNext();i++) {
372             Object JavaDoc current = it.next();
373             if ((o == null && current == null)
374                     || (o != null && o.equals(current))){
375                 return i;
376             }
377         }
378         return -1;
379     }
380     
381     public int lastIndexOf(Object JavaDoc o) {
382         return getList().lastIndexOf(o);
383     }
384     
385     public ListIterator JavaDoc listIterator() {
386         return listIterator(0);
387     }
388     
389     public ListIterator JavaDoc listIterator(int index) {
390         if (values != null) {
391             return values.listIterator();
392         } else if (index == 0) {
393             return getList().listIterator();
394         } else {
395             //TODO: implement listIterator(int) efficiency
396
return getList().listIterator(index);
397         }
398     }
399     public Object JavaDoc remove(int index) {
400         throw new JDOUnsupportedOptionException(
401         "This method is not supported in this implementation");
402     }
403     public Object JavaDoc set(int index, Object JavaDoc element) {
404         throw new JDOUnsupportedOptionException(
405         "This method is not supported in this implementation");
406     }
407     public List JavaDoc subList(int fromIndex, int toIndex) {
408         if (toIndex < fromIndex) {
409             throw new IllegalArgumentException JavaDoc("toIndex(" + toIndex
410                     + ") < fromIndex(" + fromIndex +")");
411         } else if (toIndex == fromIndex) {
412             return Collections.EMPTY_LIST;
413         }else if (values != null) {
414             return values.subList(fromIndex, toIndex);
415         } else {
416             ArrayList JavaDoc result = new ArrayList JavaDoc();
417             int i=-1;
418             for(Iterator JavaDoc it = iterator(); it.hasNext() && i<toIndex;i++) {
419                 Object JavaDoc current = it.next();
420             }
421             return null;
422         }
423     }
424 }
425
Popular Tags