KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > QueryIterator


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo;
13
14 import com.versant.core.server.CompiledQuery;
15 import com.versant.core.server.QueryResultWrapper;
16 import com.versant.core.common.QueryResultContainer;
17 import com.versant.core.common.BindingSupportImpl;
18
19 import java.util.NoSuchElementException JavaDoc;
20
21 /**
22  * A ListIterator implemenation that supports forward iteration over the results.
23  */

24 public class QueryIterator implements JDOListIterator {
25     private PMProxy pm;
26
27     private Object JavaDoc[] data;
28     private int actualSize;
29     private int indexInData;
30     private QueryResultWrapper qrw;
31     private boolean noMoreDataInResultSet;
32     private boolean closed;
33     private int nextIndex;
34
35     public QueryIterator(PMProxy pm, CompiledQuery compiledQuery, Object JavaDoc[] params,
36             boolean doNotFlush) {
37         this.pm = pm;
38         if (!doNotFlush && !compiledQuery.getQueryDetails().isIgnoreCache()) {
39             pm.flushIfDepOn(compiledQuery.getEvictionClassBits());
40         }
41         qrw = pm.executeQuery(compiledQuery, params);
42     }
43
44     public void remove() {
45         throw BindingSupportImpl.getInstance().unsupported("Not allowed to modify");
46     }
47
48     public boolean hasNext() {
49         if (closed) return false;
50
51         if (!noMoreDataInResultSet && (data == null || indexInData == actualSize)) {
52             getMoreData();
53         }
54         return !(data == null || indexInData == actualSize);
55     }
56
57     /**
58      * If we have not started yet then execute the query and return the first result.
59      * If we have already started and there is still data available then return the
60      * next data. If we are at the end of the last fetched data then get more
61      * and return the first data.
62      * @return
63      */

64     public Object JavaDoc next() {
65         if (closed) {
66             throw new NoSuchElementException JavaDoc();
67         }
68
69         if (!noMoreDataInResultSet && (data == null || indexInData == actualSize)) {
70             getMoreData();
71         }
72         return getNextData();
73     }
74
75     private void getMoreData() {
76         //get data from server
77
QueryResultContainer container = pm.getNextQueryResult(qrw, 0);
78         pm.addToCache(container.container);
79
80         data = container.getDataArray();
81         actualSize = container.size();
82         noMoreDataInResultSet = container.isqFinished();
83         indexInData = 0;
84     }
85
86     private Object JavaDoc getNextData() {
87         if (indexInData == actualSize) throw new NoSuchElementException JavaDoc();
88         nextIndex++;
89         return QueryResultBase.resolveRow(data[indexInData++], pm);
90     }
91
92     /**
93      * Cleanup all resources.
94      */

95     public void close() {
96         if (closed) return;
97
98         if (qrw != null) {
99             pm.closeQuery(qrw);
100             qrw = null;
101         }
102         
103         pm = null;
104         data = null;
105         qrw = null;
106
107         closed = true;
108     }
109
110     public int nextIndex() {
111         return nextIndex;
112     }
113
114     public int previousIndex() {
115         return nextIndex - 1;
116     }
117
118     public boolean hasPrevious() {
119         return nextIndex != 0;
120     }
121
122     public Object JavaDoc previous() {
123         throw BindingSupportImpl.getInstance().unsupportedOperation(null);
124     }
125
126     public void add(Object JavaDoc o) {
127         throw BindingSupportImpl.getInstance().unsupported("Not allowed to modify");
128     }
129
130     public void set(Object JavaDoc o) {
131         throw BindingSupportImpl.getInstance().unsupported("Not allowed to modify");
132     }
133 }
134
Popular Tags