001: package org.apache.ojb.soda;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.Vector;
021:
022: import org.apache.ojb.broker.PersistenceBroker;
023: import org.apache.ojb.broker.accesslayer.RsIterator;
024: import org.apache.ojb.broker.query.Query;
025: import org.odbms.ObjectSet;
026:
027: /**
028: * @version $Id: ObjectSetImpl.java,v 1.8.2.1 2005/12/21 22:30:46 tomdz Exp $
029: */
030: public class ObjectSetImpl implements ObjectSet {
031: protected Iterator ojbIterator;
032: protected int length;
033: protected Vector elements;
034: protected int position;
035: protected int scrolled;
036: protected boolean resultSetClosed;
037:
038: /**
039: * Constructor for ObjectSetImpl. Builds up an ObjectSet from an OJB Query object
040: */
041: public ObjectSetImpl(PersistenceBroker broker, Query query,
042: int limit) {
043: super ();
044: position = 0;
045: scrolled = 0;
046:
047: // avoid double query
048: // length = broker.getCount(query);
049: // if (limit >= 0)
050: // {
051: // length = Math.min(length,limit);
052: // }
053: // elements = new Vector(length);
054:
055: // thma:
056: // unfortunately Iterators are currently not extent-ware
057: // we have to use getCollectionBy Query () thus!
058: //ojbIterator = ojbBroker.getIteratorByQuery(query);
059: Collection col = broker.getCollectionByQuery(query);
060: ojbIterator = col.iterator();
061:
062: length = col.size();
063: if (limit >= 0) {
064: length = Math.min(length, limit);
065: }
066: elements = new Vector(length);
067:
068: setResultSetClosed(false);
069: }
070:
071: /*
072: * @see ObjectSet#hasNext()
073: */
074: public synchronized boolean hasNext() {
075: if (position < length) {
076: if (position < scrolled) {
077: return true;
078: } else {
079: boolean result = ojbIterator.hasNext();
080: return result;
081: }
082: } else {
083: releaseJdbcResources();
084: return false;
085: }
086:
087: }
088:
089: protected void releaseJdbcResources() {
090: if (!isResultSetClosed()) {
091: if (ojbIterator instanceof RsIterator) {
092: ((RsIterator) ojbIterator).releaseDbResources();
093: }
094: setResultSetClosed(true);
095: }
096: }
097:
098: /*
099: * @see ObjectSet#next()
100: */
101: public synchronized Object next() {
102: if (position < scrolled) {
103: position++;
104: return elements.get(position - 1);
105: } else {
106: Object next = ojbIterator.next();
107: elements.add(next);
108: position++;
109: scrolled++;
110: return next;
111: }
112: }
113:
114: /*
115: * @see ObjectSet#reset()
116: */
117: public synchronized void reset() {
118: position = 0;
119: }
120:
121: /*
122: * @see ObjectSet#size()
123: */
124: public int size() {
125: return length;
126: }
127:
128: /**
129: * Gets the resultSetClosed.
130: * @return Returns a boolean
131: */
132: public boolean isResultSetClosed() {
133: return resultSetClosed;
134: }
135:
136: /**
137: * Sets the resultSetClosed.
138: * @param resultSetClosed The resultSetClosed to set
139: */
140: public void setResultSetClosed(boolean resultSetClosed) {
141: this.resultSetClosed = resultSetClosed;
142: }
143: }
|