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 SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.entity;
030:
031: import com.caucho.amber.AmberQuery;
032: import com.caucho.amber.manager.AmberConnection;
033:
034: import java.util.AbstractList;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037:
038: /**
039: * Represents a lazy collection.
040: */
041: abstract public class CmpCollectionImpl extends AbstractList {
042: private ArrayList _base = new ArrayList();
043: private AmberQuery _query;
044:
045: public void __caucho_init(AmberConnection aConn) {
046: _query.init(aConn);
047: }
048:
049: protected void fill(AmberQuery query) {
050: try {
051: _query = query;
052:
053: _base.clear();
054: query.list(_base);
055: } catch (RuntimeException e) {
056: throw e;
057: } catch (Exception e) {
058: throw new RuntimeException(e);
059: }
060: }
061:
062: public int size() {
063: return _base.size();
064: }
065:
066: public Object get(int index) {
067: return _base.get(index);
068: }
069:
070: public boolean add(Object value) {
071: if (addImpl(value) && !_base.contains(value)) {
072: _base.add(value);
073: return true;
074: } else
075: return false;
076: }
077:
078: protected boolean addImpl(Object value) {
079: throw new UnsupportedOperationException();
080: }
081:
082: public Object remove(int index) {
083: Object oldValue = _base.remove(index);
084:
085: if (removeImpl(oldValue))
086: return oldValue;
087: else
088: return null;
089: }
090:
091: public boolean remove(Object value) {
092: return removeImpl(value);
093: }
094:
095: protected boolean removeImpl(Object oldValue) {
096: throw new UnsupportedOperationException();
097: }
098:
099: public Iterator iterator() {
100: return new Itr();
101: }
102:
103: /**
104: * Iterator of the collection's elements.
105: */
106: public class Itr implements Iterator {
107: int _index;
108: int _size;
109:
110: Itr() {
111: _size = size();
112: }
113:
114: /**
115: * Returns true if there are more items in the collection.
116: */
117: public boolean hasNext() {
118: return _index < _size;
119: }
120:
121: /**
122: * Returns the next item in the collection.
123: */
124: public Object next() {
125: if (_index < _size)
126: return get(_index++);
127: else
128: return null;
129: }
130:
131: /**
132: * Removes the previous item.
133: */
134: public void remove() {
135: _index--;
136: _size--;
137:
138: CmpCollectionImpl.this.remove(_index);
139: }
140: }
141: }
|