KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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

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

77   public void setSession(com.caucho.amber.AmberConnection aConn)
78   {
79     throw new UnsupportedOperationException JavaDoc();
80     // setSession(((UserAmberConnection) aConn).getManagedConnection());
81
}
82
83   /**
84    * Sets the session.
85    */

86   public void setSession(AmberConnection aConn)
87   {
88     _aConn = aConn;
89
90     _aConn.register(this);
91   }
92
93   /**
94    * Returns the query.
95    */

96   public AmberQuery getQuery()
97   {
98     return _query;
99   }
100
101   /**
102    * Returns the number of items in the collection.
103    */

104   public int size()
105   {
106     fill();
107
108     return _values.size();
109   }
110
111   /**
112    * Returns an iterator of the items.
113    */

114   public Iterator JavaDoc iterator()
115   {
116     fill();
117
118     return new ValuesIterator(_values);
119   }
120
121   /**
122    * Returns an iterator of the items.
123    */

124   public E get(int index)
125   {
126     fill();
127
128     return _values.get(index);
129   }
130
131   /**
132    * Adds all items to this collection.
133    */

134   public boolean addAll(Collection JavaDoc<? extends E> collection)
135   {
136     fill();
137
138     return _values.addAll(collection);
139   }
140
141   /**
142    * Clears the collection.
143    */

144   public void clear()
145   {
146     _values.clear();
147     _expireTime = Alarm.getCurrentTime();
148   }
149
150   /**
151    * Updates the collection.
152    */

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