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.AbstractSet;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037:
038: /**
039: * Represents a lazy set.
040: */
041: abstract public class CmpSetImpl extends AbstractSet {
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 Object remove(int index) {
071: Object oldValue = _base.remove(index);
072:
073: if (removeImpl(oldValue))
074: return oldValue;
075: else
076: return null;
077: }
078:
079: protected boolean removeImpl(Object oldValue) {
080: throw new UnsupportedOperationException();
081: }
082:
083: public Iterator iterator() {
084: return new Itr();
085: }
086:
087: /**
088: * Iterator of the collection's elements.
089: */
090: public class Itr implements Iterator {
091: int _index;
092: int _size;
093:
094: Itr() {
095: _size = size();
096: }
097:
098: /**
099: * Returns true if there are more items in the collection.
100: */
101: public boolean hasNext() {
102: return _index < _size;
103: }
104:
105: /**
106: * Returns the next item in the collection.
107: */
108: public Object next() {
109: if (_index < _size)
110: return get(_index++);
111: else
112: return null;
113: }
114:
115: /**
116: * Removes the previous item.
117: */
118: public void remove() {
119: _index--;
120: _size--;
121:
122: CmpSetImpl.this.remove(_index);
123: }
124: }
125: }
|