KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractSet JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37   
38 /**
39  * Represents a lazy set.
40  */

41 abstract public class CmpSetImpl extends AbstractSet 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 Object JavaDoc remove(int index)
75   {
76     Object JavaDoc oldValue = _base.remove(index);
77
78     if (removeImpl(oldValue))
79       return oldValue;
80     else
81       return null;
82   }
83
84   protected boolean removeImpl(Object JavaDoc oldValue)
85   {
86     throw new UnsupportedOperationException JavaDoc();
87   }
88   
89   public Iterator JavaDoc iterator()
90   {
91     return new Itr();
92   }
93
94   /**
95    * Iterator of the collection's elements.
96    */

97   public class Itr implements Iterator JavaDoc {
98     int _index;
99     int _size;
100
101     Itr()
102     {
103       _size = size();
104     }
105
106     /**
107      * Returns true if there are more items in the collection.
108      */

109     public boolean hasNext()
110     {
111       return _index < _size;
112     }
113     
114     /**
115      * Returns the next item in the collection.
116      */

117     public Object JavaDoc next()
118     {
119       if (_index < _size)
120     return get(_index++);
121       else
122     return null;
123     }
124     
125     /**
126      * Removes the previous item.
127      */

128     public void remove()
129     {
130       _index--;
131       _size--;
132       
133       CmpSetImpl.this.remove(_index);
134     }
135   }
136 }
137
Popular Tags