KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
15
16 /**
17  * This is a QueryResult that represent multiple datastore queries as one result.
18  * This is used for queries against an horisontal baseclass.
19  *
20  * This class can have 3 states.
21  * - sparse state: This is when only the 'get' method has been used.
22  * - resolved state: If all the results is available in a underlying result list.
23  * - not intialised: If there is no query state asociated whith this result.
24  */

25 public class MultiPartQueryResult extends QueryResultBase {
26     int size = -1;
27     private QueryResult[] queryResults;
28     private List totalResult;
29
30     private QueryResult curRes;
31     private int curIndex;
32
33     public MultiPartQueryResult(Set queryResultSet) {
34         queryResults = new QueryResult[queryResultSet.size()];
35         queryResultSet.toArray(queryResults);
36     }
37
38     public void close() {
39         for (int i = 0; i < queryResults.length; i++) {
40             queryResults[i].close();
41         }
42     }
43
44     public void setParams(Object JavaDoc[] params) {
45     }
46
47     public int size() {
48         if (totalResult == null) {
49             totalResult = new ArrayList();
50             for (int i = 0; i < queryResults.length; i++) {
51                 totalResult.addAll(queryResults[i]);
52             }
53             size = totalResult.size();
54         }
55         return size;
56     }
57
58     public boolean isEmpty() {
59         return size() == 0;
60     }
61
62     public Iterator createInternalIterNoFlush() {
63         return null;
64     }
65
66     public Iterator iterator() {
67         return new Iter(queryResults, false);
68     }
69
70     public Object JavaDoc[] toArray() {
71         size();
72         return totalResult.toArray();
73     }
74
75     public Object JavaDoc[] toArray(Object JavaDoc a[]) {
76         size();
77         return totalResult.toArray(a);
78     }
79
80     public Object JavaDoc get(int index) {
81         size();
82         return totalResult.get(index);
83     }
84
85     public boolean contains(Object JavaDoc o) {
86         size();
87         return totalResult.contains(o);
88     }
89
90     public boolean containsAll(Collection c) {
91         size();
92         return totalResult.containsAll(c);
93     }
94
95     public int indexOf(Object JavaDoc o) {
96         size();
97         return totalResult.indexOf(o);
98     }
99
100     public int lastIndexOf(Object JavaDoc o) {
101         size();
102         return totalResult.lastIndexOf(o);
103     }
104
105     public ListIterator listIterator() {
106         return null;
107     }
108
109     public ListIterator listIterator(int index) {
110         return null;
111     }
112
113     public List subList(int fromIndex, int toIndex) {
114         return null;
115     }
116
117     class Iter implements Iterator {
118         private QueryResult[] queryResults;
119         private int nextIterIndex = 1;
120         private Iterator curIterator;
121         private boolean noFlush;
122
123         public Iter(QueryResult[] queryResults, boolean noFlush) {
124             this.queryResults = queryResults;
125             curIterator = queryResults[0].iterator();
126             this.noFlush = noFlush;
127         }
128
129         private Iterator getIter(int index) {
130             if (noFlush) return queryResults[index].createInternalIterNoFlush();
131             else return queryResults[index].iterator();
132         }
133
134         public void remove() {
135             curIterator.remove();
136         }
137
138         public boolean hasNext() {
139             if (curIterator.hasNext()) return true;
140             if (nextIterIndex == queryResults.length) return false;
141             curIterator = queryResults[nextIterIndex++].iterator();
142             return curIterator.hasNext();
143         }
144
145         public Object JavaDoc next() {
146             return curIterator.next();
147         }
148     }
149 }
150
Popular Tags