KickJava   Java API By Example, From Geeks To Geeks.

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


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
36 import java.util.*;
37 import java.util.logging.*;
38
39 /**
40  * Represents a marshalled Collection argument.
41  */

42 public class JavaListAdapter
43   extends JavaCollectionAdapter
44 {
45   private static final Logger log
46     = Logger.getLogger(JavaListAdapter.class.getName());
47
48   //XXX: parameterized type
49
private List _list;
50   
51   private int _next = 0;
52
53   public JavaListAdapter(Env env, List list)
54   {
55     this(env, list, env.getJavaClassDefinition(list.getClass().getName()));
56   }
57   
58   public JavaListAdapter(Env env, List list, JavaClassDef def)
59   {
60     super(env, list, def);
61     _list = list;
62   }
63
64   /**
65    * Adds a new value.
66    */

67   public Value putImpl(Value key, Value value)
68   {
69     int pos = key.toInt();
70     int size = size();
71     
72     if (0 <= pos && pos <= size) {
73       if (pos < size)
74         _list.remove(pos);
75       
76       _list.add(pos, value.toJavaObject());
77
78       return value;
79     }
80     else {
81       getEnv().warning(L.l("index {0} is out of range", pos));
82       log.log(Level.FINE, L.l("index {0} is out of range", pos));
83  
84       return UnsetValue.UNSET;
85     }
86   }
87   
88   /**
89    * Gets a new value.
90    */

91   public Value get(Value key)
92   {
93     int pos = key.toInt();
94     
95     if (0 <= pos && pos < size())
96       return wrapJava(_list.get(pos));
97     else
98       return UnsetValue.UNSET;
99   }
100
101   /**
102    * Removes a value.
103    */

104   public Value remove(Value key)
105   {
106     int pos = key.toInt();
107     
108     if (0 <= pos && pos < size())
109       return wrapJava(_list.remove(pos));
110     else
111       return UnsetValue.UNSET;
112   }
113
114   /**
115    * Pops the top value.
116    */

117   public Value pop()
118   {
119     if (size() == 0)
120       return BooleanValue.FALSE;
121     
122     return wrapJava(_list.remove(0));
123   }
124   
125   /**
126    * Returns the corresponding key if this array contains the given value
127    *
128    * @param value the value to search for in the array
129    *
130    * @return the key if it is found in the array, NULL otherwise
131    *
132    * @throws NullPointerException
133    */

134   public Value contains(Value value)
135   {
136     for (Map.Entry<Value,Value> entry : entrySet()) {
137       if (entry.getValue().equals(value))
138         return entry.getKey();
139     }
140     
141     return NullValue.NULL;
142   }
143   
144   /**
145    * Returns the current value.
146    */

147   public Value current()
148   {
149     if (_next < _list.size())
150       return wrapJava(_list.get(_next));
151     else
152       return BooleanValue.FALSE;
153   }
154
155   /**
156    * Returns the current key
157    */

158   public Value key()
159   {
160     if (_next < _list.size())
161       return LongValue.create(_next);
162     else
163       return NullValue.NULL;
164   }
165
166   /**
167    * Returns true if there are more elements.
168    */

169   public boolean hasCurrent()
170   {
171     return _next < _list.size();
172   }
173
174   /**
175    * Returns the next value.
176    */

177   public Value next()
178   {
179     if (_next < _list.size())
180       return wrapJava(_list.get(_next++));
181     else
182       return BooleanValue.FALSE;
183   }
184
185   /**
186    * Returns the previous value.
187    */

188   public Value prev()
189   {
190     if (_next > 0)
191       return wrapJava(_list.get(_next--));
192     else
193       return BooleanValue.FALSE;
194   }
195
196   /**
197    * The each iterator
198    */

199   public Value each()
200   {
201     if (_next < _list.size())
202     {
203       ArrayValue result = new ArrayValueImpl();
204
205       result.put(LongValue.ZERO, key());
206       result.put(KEY, key());
207
208       result.put(LongValue.ONE, current());
209       result.put(VALUE, current());
210
211       _next++;
212
213       return result;
214     }
215     else
216       return NullValue.NULL;
217   }
218
219   /**
220    * Returns the first value.
221    */

222   public Value reset()
223   {
224     _next = 0;
225
226     return current();
227   }
228
229   /**
230    * Returns the last value.
231    */

232   public Value end()
233   {
234     _next = _list.size();
235     
236     return current();
237   }
238   
239   /**
240    * Copy for assignment.
241    */

242   @Override JavaDoc
243   public Value copy()
244   {
245     try {
246       Class JavaDoc cl = _list.getClass();
247
248       List list = (List)cl.newInstance();
249
250       list.addAll(_list);
251
252       return new JavaListAdapter(getEnv(), list, getClassDef());
253     }
254     catch (Exception JavaDoc e) {
255       throw new QuercusRuntimeException(e);
256     }
257   }
258 }
259
Popular Tags