KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > genclass > collection > CollectionImpl


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.genclass.collection;
19
20 import org.objectweb.speedo.mim.api.SpeedoAccessor;
21 import org.objectweb.speedo.genclass.GenClass;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * This class is an implementation of the java.util.Collection interface.
29  *
30  * @author Eric.Bruneton, S.Chassande-Barrioz
31  */

32 public class CollectionImpl extends GenClass implements Collection JavaDoc {
33
34     protected final static int DEFAULT_SIZE = -1;
35
36     CollectionAccessor accessor;
37
38     /**
39      * Instanciates and initializes a new collection with an initial size.
40      */

41     public CollectionImpl() {
42         super();
43         accessor = (CollectionAccessor) createAccessor();
44     }
45
46     //protected BOject
47

48     // ------------------------------------------------------------------------
49
// IMPLEMENTATION OF THE Collection INTERFACE
50
// ------------------------------------------------------------------------
51

52     public boolean add(Object JavaDoc o) {
53         boolean result;
54         if (!jdoIsActive) {
55             result = accessor.collection.add(o);
56         } else {
57             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().writeIntention(this, null, getDataIdentifier(o));
58             result = ca.add(o);
59         }
60         return result;
61     }
62
63     public boolean addAll(Collection JavaDoc c) {
64         boolean result;
65         if (!jdoIsActive) {
66             result = accessor.collection.addAll(c);
67         } else {
68             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().writeIntention(this, null, getDataIdentifiers(c));
69             result = ca.addAll(c);
70         }
71         return result;
72     }
73
74     public void clear() {
75         if (!jdoIsActive) {
76             accessor.collection.clear();
77         } else {
78             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().writeIntention(this, null);
79             ca.clear();
80         }
81     }
82
83     public boolean contains(Object JavaDoc o) {
84         if (!jdoIsActive) {
85             return accessor.collection.contains(o);
86         } else {
87             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().readIntention(this, null);
88             return ca.contains(o);
89         }
90     }
91
92     public boolean containsAll(Collection JavaDoc c) {
93         if (!jdoIsActive) {
94             return accessor.collection.containsAll(c);
95         } else {
96             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().readIntention(this, null);
97             return ca.containsAll(c);
98         }
99     }
100
101     public boolean equals(Object JavaDoc o) {
102         if (!jdoIsActive) {
103             return accessor.collection.equals(o);
104         } else {
105             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().readIntention(this, null);
106             return ca.equals(o);
107         }
108     }
109
110     public boolean isEmpty() {
111         if (!jdoIsActive) {
112             return accessor.collection.isEmpty();
113         } else {
114             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().readIntention(this, null);
115             return ca.isEmpty();
116         }
117     }
118
119     public Iterator JavaDoc iterator() {
120         if (!jdoIsActive) {
121             return accessor.collection.iterator();
122         } else {
123             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().readIntention(this, null);
124             return ca.iterator();
125         }
126     }
127
128     public boolean remove(Object JavaDoc o) {
129         boolean result;
130         if (!jdoIsActive) {
131             result = accessor.collection.remove(o);
132         } else {
133             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().writeIntention(this, null, getDataIdentifier(o));
134             result = ca.remove(o);
135         }
136         return result;
137     }
138
139     public boolean removeAll(Collection JavaDoc c) {
140         boolean result;
141         if (!jdoIsActive) {
142             result = accessor.collection.removeAll(c);
143         } else {
144             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().writeIntention(this, null, getDataIdentifiers(c));
145             result = ca.removeAll(c);
146         }
147         return result;
148     }
149
150     public boolean retainAll(Collection JavaDoc c) {
151         throw new UnsupportedOperationException JavaDoc();
152     }
153
154     public int size() {
155         if (!jdoIsActive) {
156             return accessor.collection.size();
157         } else {
158             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().readIntention(this, null);
159             return ca.size();
160         }
161     }
162
163     public Object JavaDoc[] toArray() {
164         if (!jdoIsActive) {
165             return accessor.collection.toArray();
166         } else {
167             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().readIntention(this, null);
168             return ca.toArray();
169         }
170     }
171
172     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
173         if (!jdoIsActive) {
174             return accessor.collection.toArray(a);
175         } else {
176             CollectionAccessor ca = (CollectionAccessor) getSpeedoHome().readIntention(this, null);
177             return ca.toArray(a);
178         }
179     }
180
181     // OTHER METHODS //
182
// --------------//
183

184     public SpeedoAccessor createAccessor() {
185         return new CollectionAccessor(this);
186     }
187
188     public Object JavaDoc createGenClass() {
189         return new ArrayList JavaDoc();
190     }
191
192     public SpeedoAccessor getReferenceAccessor() {
193         return accessor;
194     }
195     public void setReferenceAccessor(SpeedoAccessor refAcc) {
196         accessor = (CollectionAccessor) refAcc;
197     }
198
199
200
201 }
202
Popular Tags