KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > collection > CollectionImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.collection;
30
31 import com.caucho.amber.AmberQuery;
32 import com.caucho.amber.AmberRuntimeException;
33 import com.caucho.amber.manager.AmberConnection;
34 import com.caucho.amber.query.UserQuery;
35 import com.caucho.util.Alarm;
36
37 import java.sql.SQLException JavaDoc;
38 import java.util.AbstractList JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43 /**
44  * Represents a lazy collection.
45  */

46 public class CollectionImpl<E> extends AbstractList JavaDoc<E>
47   implements AmberCollection {
48   private AmberQuery _query;
49
50   private AmberConnection _aConn;
51
52   private ArrayList JavaDoc<E> _values = new ArrayList JavaDoc<E>();
53   private long _expireTime;
54
55   public CollectionImpl(AmberConnection aConn, String JavaDoc query)
56   {
57     _aConn = aConn;
58
59     // jpa/0s2j
60
if (query != null) {
61       try {
62         _query = _aConn.prepareQuery(query);
63       } catch (SQLException JavaDoc e) {
64         throw new AmberRuntimeException(e);
65       }
66     }
67   }
68
69   public CollectionImpl(AmberQuery query)
70   {
71     _query = query;
72
73     setSession(((UserQuery) _query).getConnection());
74   }
75
76   /**
77    * Sets the session.
78    */

79   public void setSession(AmberConnection aConn)
80   {
81     _aConn = aConn;
82
83     if (_aConn != null)
84       _aConn.register(this);
85   }
86
87   /**
88    * Returns the query.
89    */

90   public AmberQuery getQuery()
91   {
92     return _query;
93   }
94
95   /**
96    * Returns the number of items in the collection.
97    */

98   public int size()
99   {
100     fill();
101
102     return _values.size();
103   }
104
105   /**
106    * Returns an iterator of the items.
107    */

108   public Iterator JavaDoc<E> iterator()
109   {
110     fill();
111
112     return new ValuesIterator(_values);
113   }
114
115   /**
116    * Returns an iterator of the items.
117    */

118   public E get(int index)
119   {
120     fill();
121
122     return _values.get(index);
123   }
124
125   /**
126    * Adds an item to the collection.
127    */

128   public boolean add(E o)
129   {
130     fill();
131
132     return _values.add(o);
133   }
134
135   /**
136    * Adds an item to the collection.
137    */

138   public boolean addAll(int index,
139                         Collection JavaDoc<? extends E> collection)
140   {
141     fill();
142
143     return _values.addAll(index, collection);
144   }
145
146   /**
147    * Clears the collection.
148    */

149   public void clear()
150   {
151     _values.clear();
152     _expireTime = Alarm.getCurrentTime();
153   }
154
155   /**
156    * Updates the collection.
157    */

158   public void update()
159   {
160     _expireTime = 0;
161   }
162
163   protected boolean isValid()
164   {
165     return Alarm.getCurrentTime() <= _expireTime;
166   }
167
168   private void fill()
169   {
170     // jpa/0s2i
171
if (_query == null)
172       return;
173
174     if (Alarm.getCurrentTime() <= _expireTime)
175       return;
176
177     try {
178       _expireTime = Alarm.getCurrentTime();
179
180       ((UserQuery) _query).setSession(_aConn);
181       _values.clear();
182       _query.list((ArrayList JavaDoc) _values);
183     } catch (SQLException JavaDoc e) {
184       throw new AmberRuntimeException(e);
185     }
186   }
187
188   class ValuesIterator implements Iterator JavaDoc {
189     private ArrayList JavaDoc<E> _values;
190     private int i = 0;
191
192     ValuesIterator(ArrayList JavaDoc<E> values)
193     {
194       _values = new ArrayList JavaDoc<E>(values);
195     }
196
197     public boolean hasNext()
198     {
199       return i < _values.size();
200     }
201
202     public E next()
203     {
204       if (i < _values.size())
205         return _values.get(i++);
206       else
207         return null;
208     }
209
210     public void remove()
211     {
212       CollectionImpl.this.remove(_values.get(i - 1));
213     }
214   }
215 }
216
Popular Tags