View Javadoc

1   package org.apache.ojb.soda;
2   
3   /* Copyright 2002-2005 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.Vector;
21  
22  import org.apache.ojb.broker.PersistenceBroker;
23  import org.apache.ojb.broker.accesslayer.RsIterator;
24  import org.apache.ojb.broker.query.Query;
25  import org.odbms.ObjectSet;
26  
27  /**
28   * @version $Id: ObjectSetImpl.java,v 1.1 2007-08-24 22:17:42 ewestfal Exp $
29   */
30  public class ObjectSetImpl implements ObjectSet
31  {
32      protected Iterator ojbIterator;
33      protected int length;
34      protected Vector elements;
35      protected int position;
36      protected int scrolled;
37      protected boolean resultSetClosed;
38  
39      /**
40       * Constructor for ObjectSetImpl. Builds up an ObjectSet from an OJB Query object
41       */
42      public ObjectSetImpl(PersistenceBroker broker, Query query, int limit)
43      {
44          super();
45          position = 0;
46          scrolled = 0;
47  
48          // avoid double query
49  //        length = broker.getCount(query);
50  //        if (limit >= 0)
51  //        {
52  //        	length = Math.min(length,limit);
53  //        }
54  //        elements = new Vector(length);
55  
56          // thma:
57          // unfortunately Iterators are currently not extent-ware
58          // we have to use getCollectionBy Query () thus!
59          //ojbIterator = ojbBroker.getIteratorByQuery(query);
60          Collection col = broker.getCollectionByQuery(query);
61          ojbIterator = col.iterator();
62  
63          length = col.size();
64          if (limit >= 0)
65          {
66          	length = Math.min(length,limit);
67          }
68          elements = new Vector(length);
69  
70          setResultSetClosed(false);
71      }
72  
73      /*
74       * @see ObjectSet#hasNext()
75       */
76      public synchronized boolean hasNext()
77      {
78          if (position < length)
79          {
80              if (position < scrolled)
81              {
82               	return true;
83              }
84              else
85              {
86                  boolean result = ojbIterator.hasNext();
87               	return result;
88              }
89          }
90          else
91          {
92  			releaseJdbcResources();
93              return false;
94          }
95  
96      }
97  
98      protected void releaseJdbcResources()
99      {
100         if (!isResultSetClosed())
101         {
102             if (ojbIterator instanceof RsIterator)
103             {
104                 ((RsIterator) ojbIterator).releaseDbResources();
105             }
106             setResultSetClosed(true);
107         }
108     }
109 
110     /*
111      * @see ObjectSet#next()
112      */
113     public synchronized Object next()
114     {
115         if (position < scrolled)
116         {
117             position++;
118             return elements.get(position - 1);
119         }
120         else
121         {
122             Object next = ojbIterator.next();
123             elements.add(next);
124             position++;
125             scrolled++;
126             return next;
127         }
128     }
129 
130     /*
131      * @see ObjectSet#reset()
132      */
133     public synchronized void reset()
134     {
135         position = 0;
136     }
137 
138     /*
139      * @see ObjectSet#size()
140      */
141     public int size()
142     {
143         return length;
144     }
145 
146     /**
147      * Gets the resultSetClosed.
148      * @return Returns a boolean
149      */
150     public boolean isResultSetClosed()
151     {
152         return resultSetClosed;
153     }
154 
155     /**
156      * Sets the resultSetClosed.
157      * @param resultSetClosed The resultSetClosed to set
158      */
159     public void setResultSetClosed(boolean resultSetClosed)
160     {
161         this.resultSetClosed = resultSetClosed;
162     }
163 }