001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.collection;
030:
031: import com.caucho.amber.AmberQuery;
032: import com.caucho.amber.AmberRuntimeException;
033: import com.caucho.amber.manager.AmberConnection;
034: import com.caucho.amber.query.UserQuery;
035: import com.caucho.util.Alarm;
036:
037: import java.sql.SQLException;
038: import java.util.AbstractList;
039: import java.util.ArrayList;
040: import java.util.Collection;
041: import java.util.Iterator;
042:
043: /**
044: * Represents a lazy collection.
045: */
046: public class CollectionImpl<E> extends AbstractList<E> implements
047: AmberCollection {
048: private transient AmberQuery _query;
049:
050: private transient AmberConnection _aConn;
051:
052: private ArrayList<E> _values = new ArrayList<E>();
053: private transient long _expireTime;
054:
055: public CollectionImpl(AmberConnection aConn, String query) {
056: _aConn = aConn;
057:
058: // jpa/0s2j
059: if (query != null) {
060: try {
061: _query = _aConn.prepareQuery(query);
062: } catch (SQLException e) {
063: throw new AmberRuntimeException(e);
064: }
065: }
066: }
067:
068: public CollectionImpl(AmberQuery query) {
069: _query = query;
070:
071: // jpa/0s2k
072: if (_query != null)
073: setSession(((UserQuery) _query).getConnection());
074: }
075:
076: /**
077: * Sets the session.
078: */
079: public void setSession(AmberConnection aConn) {
080: _aConn = aConn;
081:
082: if (_aConn != null)
083: _aConn.register(this );
084: }
085:
086: /**
087: * Returns the session.
088: */
089: public AmberConnection getSession() {
090: return _aConn;
091: }
092:
093: /**
094: * Returns the query.
095: */
096: public AmberQuery getQuery() {
097: return _query;
098: }
099:
100: /**
101: * Returns the number of items in the collection.
102: */
103: public int size() {
104: fill();
105:
106: return _values.size();
107: }
108:
109: /**
110: * Returns an iterator of the items.
111: */
112: public Iterator<E> iterator() {
113: fill();
114:
115: return new ValuesIterator(_values);
116: }
117:
118: /**
119: * Returns an iterator of the items.
120: */
121: public E get(int index) {
122: fill();
123:
124: return _values.get(index);
125: }
126:
127: /**
128: * Adds an item to the collection.
129: */
130: public boolean add(E o) {
131: fill();
132:
133: return _values.add(o);
134: }
135:
136: /**
137: * Adds an item to the collection.
138: */
139: public boolean addAll(int index, Collection<? extends E> collection) {
140: fill();
141:
142: // jpa/0i60
143: if (collection == null)
144: return true;
145:
146: return _values.addAll(index, collection);
147: }
148:
149: /**
150: * Clears the collection.
151: */
152: public void clear() {
153: _values.clear();
154: _expireTime = Alarm.getCurrentTime();
155: }
156:
157: /**
158: * Updates the collection.
159: */
160: public void update() {
161: _expireTime = 0;
162: }
163:
164: protected boolean isValid() {
165: return Alarm.getCurrentTime() <= _expireTime;
166: }
167:
168: private void fill() {
169: // jpa/0s2i
170: if (_query == null)
171: return;
172:
173: // If it is detached should not be updated.
174: if (_aConn == null)
175: return;
176:
177: if (Alarm.getCurrentTime() <= _expireTime)
178: return;
179:
180: try {
181: _expireTime = Alarm.getCurrentTime();
182:
183: ((UserQuery) _query).setSession(_aConn);
184: _values.clear();
185: _query.list((ArrayList) _values);
186: } catch (SQLException e) {
187: throw new AmberRuntimeException(e);
188: }
189: }
190:
191: private Object writeReplace() {
192: return _values;
193: }
194:
195: class ValuesIterator implements Iterator {
196: private ArrayList<E> _values;
197: private int i = 0;
198:
199: ValuesIterator(ArrayList<E> values) {
200: _values = new ArrayList<E>(values);
201: }
202:
203: public boolean hasNext() {
204: return i < _values.size();
205: }
206:
207: public E next() {
208: if (i < _values.size())
209: return _values.get(i++);
210: else
211: return null;
212: }
213:
214: public void remove() {
215: CollectionImpl.this .remove(_values.get(i - 1));
216: }
217: }
218: }
|