001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdo;
012:
013: import java.util.*;
014:
015: /**
016: * This is a QueryResult that represent multiple datastore queries as one result.
017: * This is used for queries against an horisontal baseclass.
018: *
019: * This class can have 3 states.
020: * - sparse state: This is when only the 'get' method has been used.
021: * - resolved state: If all the results is available in a underlying result list.
022: * - not intialised: If there is no query state asociated whith this result.
023: */
024: public class MultiPartQueryResult extends QueryResultBase {
025: int size = -1;
026: private QueryResult[] queryResults;
027: private List totalResult;
028:
029: private QueryResult curRes;
030: private int curIndex;
031:
032: public MultiPartQueryResult(Set queryResultSet) {
033: queryResults = new QueryResult[queryResultSet.size()];
034: queryResultSet.toArray(queryResults);
035: }
036:
037: public void close() {
038: for (int i = 0; i < queryResults.length; i++) {
039: queryResults[i].close();
040: }
041: }
042:
043: public void setParams(Object[] params) {
044: }
045:
046: public int size() {
047: if (totalResult == null) {
048: totalResult = new ArrayList();
049: for (int i = 0; i < queryResults.length; i++) {
050: totalResult.addAll(queryResults[i]);
051: }
052: size = totalResult.size();
053: }
054: return size;
055: }
056:
057: public boolean isEmpty() {
058: return size() == 0;
059: }
060:
061: public Iterator createInternalIterNoFlush() {
062: return null;
063: }
064:
065: public Iterator iterator() {
066: return new Iter(queryResults, false);
067: }
068:
069: public Object[] toArray() {
070: size();
071: return totalResult.toArray();
072: }
073:
074: public Object[] toArray(Object a[]) {
075: size();
076: return totalResult.toArray(a);
077: }
078:
079: public Object get(int index) {
080: size();
081: return totalResult.get(index);
082: }
083:
084: public boolean contains(Object o) {
085: size();
086: return totalResult.contains(o);
087: }
088:
089: public boolean containsAll(Collection c) {
090: size();
091: return totalResult.containsAll(c);
092: }
093:
094: public int indexOf(Object o) {
095: size();
096: return totalResult.indexOf(o);
097: }
098:
099: public int lastIndexOf(Object o) {
100: size();
101: return totalResult.lastIndexOf(o);
102: }
103:
104: public ListIterator listIterator() {
105: return null;
106: }
107:
108: public ListIterator listIterator(int index) {
109: return null;
110: }
111:
112: public List subList(int fromIndex, int toIndex) {
113: return null;
114: }
115:
116: class Iter implements Iterator {
117: private QueryResult[] queryResults;
118: private int nextIterIndex = 1;
119: private Iterator curIterator;
120: private boolean noFlush;
121:
122: public Iter(QueryResult[] queryResults, boolean noFlush) {
123: this .queryResults = queryResults;
124: curIterator = queryResults[0].iterator();
125: this .noFlush = noFlush;
126: }
127:
128: private Iterator getIter(int index) {
129: if (noFlush)
130: return queryResults[index].createInternalIterNoFlush();
131: else
132: return queryResults[index].iterator();
133: }
134:
135: public void remove() {
136: curIterator.remove();
137: }
138:
139: public boolean hasNext() {
140: if (curIterator.hasNext())
141: return true;
142: if (nextIterIndex == queryResults.length)
143: return false;
144: curIterator = queryResults[nextIterIndex++].iterator();
145: return curIterator.hasNext();
146: }
147:
148: public Object next() {
149: return curIterator.next();
150: }
151: }
152: }
|