KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > entity > CmpCollectionImpl


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

28
29 package com.caucho.ejb.entity;
30
31 import com.caucho.amber.AmberQuery;
32 import com.caucho.amber.manager.AmberConnection;
33
34 import java.util.AbstractList JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37   
38 /**
39  * Represents a lazy collection.
40  */

41 abstract public class CmpCollectionImpl extends AbstractList JavaDoc {
42   private ArrayList JavaDoc _base = new ArrayList JavaDoc();
43   private AmberQuery _query;
44
45   public void __caucho_init(AmberConnection aConn)
46   {
47     _query.init(aConn);
48   }
49   
50   protected void fill(AmberQuery query)
51   {
52     try {
53       _query = query;
54       
55       _base.clear();
56       query.list(_base);
57     } catch (RuntimeException JavaDoc e) {
58       throw e;
59     } catch (Exception JavaDoc e) {
60       throw new RuntimeException JavaDoc(e);
61     }
62   }
63   
64   public int size()
65   {
66     return _base.size();
67   }
68   
69   public Object JavaDoc get(int index)
70   {
71     return _base.get(index);
72   }
73   
74   public boolean add(Object JavaDoc value)
75   {
76     if (addImpl(value) && ! _base.contains(value)) {
77       _base.add(value);
78       return true;
79     }
80     else
81       return false;
82   }
83
84   protected boolean addImpl(Object JavaDoc value)
85   {
86     throw new UnsupportedOperationException JavaDoc();
87   }
88   
89   public Object JavaDoc remove(int index)
90   {
91     Object JavaDoc oldValue = _base.remove(index);
92
93     if (removeImpl(oldValue))
94       return oldValue;
95     else
96       return null;
97   }
98   
99   public boolean remove(Object JavaDoc value)
100   {
101     return removeImpl(value);
102   }
103
104   protected boolean removeImpl(Object JavaDoc oldValue)
105   {
106     throw new UnsupportedOperationException JavaDoc();
107   }
108   
109   public Iterator JavaDoc iterator()
110   {
111     return new Itr();
112   }
113
114   /**
115    * Iterator of the collection's elements.
116    */

117   public class Itr implements Iterator JavaDoc {
118     int _index;
119     int _size;
120
121     Itr()
122     {
123       _size = size();
124     }
125
126     /**
127      * Returns true if there are more items in the collection.
128      */

129     public boolean hasNext()
130     {
131       return _index < _size;
132     }
133     
134     /**
135      * Returns the next item in the collection.
136      */

137     public Object JavaDoc next()
138     {
139       if (_index < _size)
140     return get(_index++);
141       else
142     return null;
143     }
144     
145     /**
146      * Removes the previous item.
147      */

148     public void remove()
149     {
150       _index--;
151       _size--;
152       
153       CmpCollectionImpl.this.remove(_index);
154     }
155   }
156 }
157
Popular Tags