KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > env > JavaCollectionAdapter


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

29
30 package com.caucho.quercus.env;
31
32 import com.caucho.quercus.program.JavaClassDef;
33
34 import com.caucho.quercus.QuercusRuntimeException;
35 import com.caucho.quercus.UnimplementedException;
36
37 import java.util.*;
38 import java.util.logging.*;
39
40 /**
41  * Represents a marshalled Collection argument.
42  */

43 public class JavaCollectionAdapter extends JavaAdapter
44 {
45   private static final Logger log
46     = Logger.getLogger(JavaCollectionAdapter.class.getName());
47
48   //XXX: parameterized type
49
private Collection _collection;
50
51   public JavaCollectionAdapter(Env env, Collection coll, JavaClassDef def)
52   {
53     super(env, coll, def);
54     
55     _collection = coll;
56   }
57
58   /**
59    * Clears the array
60    */

61   public void clear()
62   {
63     _collection.clear();
64   }
65
66   public int size()
67   {
68     return _collection.size();
69   }
70   
71   //
72
// Conversions
73
//
74

75   /**
76    * Copy for assignment.
77    */

78   public Value copy()
79   {
80     try {
81       Class JavaDoc cl = _collection.getClass();
82
83       Collection coll = (Collection)cl.newInstance();
84
85       coll.addAll(_collection);
86
87       return new JavaCollectionAdapter(getEnv(), coll, getClassDef());
88     }
89     catch (Exception JavaDoc e) {
90       throw new QuercusRuntimeException(e);
91     }
92   }
93
94   /**
95    * Returns the size.
96    */

97   public int getSize()
98   {
99     return size();
100   }
101
102   /**
103    * Creatse a tail index.
104    */

105   public Value createTailKey()
106   {
107     return LongValue.create(size());
108   }
109   
110   public Value putImpl(Value key, Value value)
111   {
112     if (key.toInt() != size())
113       throw new UnsupportedOperationException JavaDoc("random assignment into Collection");
114     
115     _collection.add(value.toJavaObject());
116     
117     return value;
118   }
119
120   /**
121    * Gets a new value.
122    */

123   public Value get(Value key)
124   {
125     int pos = key.toInt();
126     
127     if (pos < 0)
128       return UnsetValue.UNSET;
129     
130     for (Object JavaDoc obj : _collection) {
131       if (pos-- > 0)
132         continue;
133       
134       return wrapJava(obj);
135     }
136     
137     return UnsetValue.UNSET;
138   }
139   
140   /**
141    * Removes a value.
142    */

143   public Value remove(Value key)
144   {
145     int i = 0;
146     int pos = key.toInt();
147     
148     if (pos < 0)
149       return UnsetValue.UNSET;
150     
151     for (Object JavaDoc obj : _collection) {
152       if (pos-- > 0)
153         continue;
154       
155       Value val = wrapJava(obj);
156       
157       _collection.remove(obj);
158       return val;
159     }
160
161     return UnsetValue.UNSET;
162   }
163   
164   /**
165    * Returns a set of all the of the entries.
166    */

167   public Set<Map.Entry<Value,Value>> entrySet()
168   {
169     return new CollectionValueSet();
170   }
171
172   /**
173    * Returns a collection of the values.
174    */

175   public Set<Map.Entry> objectEntrySet()
176   {
177     return new CollectionSet();
178   }
179   
180   /**
181    * Returns a collection of the values.
182    */

183   public Collection<Value> values()
184   {
185     return new ValueCollection();
186   }
187
188   public Value []getValueArray(Env env)
189   {
190     Value[] values = new Value[getSize()];
191
192     int i = 0;
193     for (Object JavaDoc ob: _collection) {
194       values[i++] = env.wrapJava(ob);
195     }
196
197     return values;
198   }
199
200   public class CollectionSet
201     extends AbstractSet<Map.Entry>
202   {
203     CollectionSet()
204     {
205     }
206
207     public int size()
208     {
209       return getSize();
210     }
211
212     public Iterator<Map.Entry> iterator()
213     {
214       return new CollectionIterator(_collection);
215     }
216   }
217   
218   public class CollectionIterator
219     implements Iterator
220   {
221     private int _index;
222     private Iterator _iterator;
223
224     public CollectionIterator(Collection collection)
225     {
226       _index = 0;
227       _iterator = collection.iterator();
228     }
229
230     public boolean hasNext()
231     {
232       return _iterator.hasNext();
233     }
234
235     public Map.Entry next()
236     {
237       return new CollectionEntry(_index++, _iterator.next());
238     }
239
240     public void remove()
241     {
242       throw new UnsupportedOperationException JavaDoc();
243     }
244   }
245
246   public static class CollectionEntry
247     implements Map.Entry
248   {
249     private final int _key;
250     private Object JavaDoc _value;
251
252     public CollectionEntry(int key, Object JavaDoc value)
253     {
254       _key = key;
255       _value = value;
256     }
257
258     public Object JavaDoc getKey()
259     {
260       return Long.valueOf(_key);
261     }
262
263     public Object JavaDoc getValue()
264     {
265       return _value;
266     }
267
268     public Object JavaDoc setValue(Object JavaDoc value)
269     {
270       Object JavaDoc oldValue = _value;
271
272       _value = value;
273
274       return oldValue;
275     }
276   }
277
278   public class CollectionValueSet
279     extends AbstractSet<Map.Entry<Value,Value>>
280   {
281     CollectionValueSet()
282     {
283     }
284   
285     public int size()
286     {
287       return getSize();
288     }
289   
290     public Iterator<Map.Entry<Value,Value>> iterator()
291     {
292       return new CollectionValueIterator(_collection);
293     }
294   }
295
296   public class CollectionValueIterator
297     implements Iterator<Map.Entry<Value,Value>>
298   {
299     private int _index;
300     private Iterator _iterator;
301
302     public CollectionValueIterator(Collection collection)
303     {
304       _index = 0;
305       _iterator = collection.iterator();
306     }
307
308     public boolean hasNext()
309     {
310       return _iterator.hasNext();
311     }
312
313     public Map.Entry<Value,Value> next()
314     {
315        Value val = wrapJava(_iterator.next());
316
317        return new CollectionValueEntry(LongValue.create(_index++), val);
318     }
319
320     public void remove()
321     {
322       throw new UnsupportedOperationException JavaDoc();
323     }
324   }
325   
326   public static class CollectionValueEntry
327     implements Map.Entry<Value,Value>
328   {
329     private final Value _key;
330     private Value _value;
331
332     public CollectionValueEntry(Value key, Value value)
333     {
334       _key = key;
335       _value = value;
336     }
337
338     public Value getKey()
339     {
340       return _key;
341     }
342
343     public Value getValue()
344     {
345       return _value;
346     }
347
348     public Value setValue(Value value)
349     {
350       Value oldValue = _value;
351
352       _value = value;
353
354       return oldValue;
355     }
356   }
357   
358   public class ValueCollection
359     extends AbstractCollection<Value>
360   {
361     ValueCollection()
362     {
363     }
364
365     public int size()
366     {
367       return getSize();
368     }
369
370     public Iterator<Value> iterator()
371     {
372       return new ValueIterator(_collection);
373     }
374   }
375
376   public class ValueIterator
377     implements Iterator<Value>
378   {
379     private Iterator _iterator;
380
381     public ValueIterator(Collection collection)
382     {
383       _iterator = collection.iterator();
384     }
385
386     public boolean hasNext()
387     {
388       return _iterator.hasNext();
389     }
390
391     public Value next()
392     {
393       return wrapJava(_iterator.next());
394     }
395
396     public void remove()
397     {
398       throw new UnsupportedOperationException JavaDoc();
399     }
400   }
401
402 }
403
Popular Tags