KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.IdentityHashMap JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 /**
36  * Represents a PHP array value.
37  */

38 public class CopyArrayValue extends ArrayValue {
39   private static final Logger JavaDoc log
40     = Logger.getLogger(CopyArrayValue.class.getName());
41
42   private final ConstArrayValue _constArray;
43   private ArrayValue _copyArray;
44
45   public CopyArrayValue(ConstArrayValue constArray)
46   {
47     _constArray = constArray;
48   }
49
50   /**
51    * Converts to a boolean.
52    */

53   public boolean toBoolean()
54   {
55     if (_copyArray != null)
56       return _copyArray.toBoolean();
57     else
58       return _constArray.toBoolean();
59   }
60   
61   /**
62    * Copy for assignment.
63    */

64   public Value copy()
65   {
66     if (_copyArray != null)
67       return _copyArray.copy();
68     else
69       return _constArray.copy();
70   }
71   
72   /**
73    * Copy for serialization
74    */

75   public Value copy(Env env, IdentityHashMap JavaDoc<Value,Value> map)
76   {
77     if (_copyArray != null)
78       return _copyArray.copy(env, map);
79     else
80       return _constArray.copy(env, map);
81   }
82
83   /**
84    * Returns the size.
85    */

86   public int getSize()
87   {
88     if (_copyArray != null)
89       return _copyArray.getSize();
90     else
91       return _constArray.getSize();
92   }
93
94   /**
95    * Clears the array
96    */

97   public void clear()
98   {
99     getCopyArray().clear();
100   }
101   
102   /**
103    * Adds a new value.
104    */

105   public Value put(Value key, Value value)
106   {
107     return getCopyArray().put(key, value);
108   }
109
110   /**
111    * Add
112    */

113   public Value put(Value value)
114   {
115     return getCopyArray().put(value);
116   }
117
118   /**
119    * Add
120    */

121   public ArrayValue unshift(Value value)
122   {
123     return getCopyArray().unshift(value);
124   }
125
126   /**
127    * Add
128    */

129   public ArrayValue splice(int start, int end, ArrayValue replace)
130   {
131     return getCopyArray().splice(start, end, replace);
132   }
133
134   /**
135    * Returns the value as an array.
136    */

137   public Value getArray(Value fieldName)
138   {
139     return getCopyArray().getArray(fieldName);
140   }
141
142   /**
143    * Returns the value as an argument which may be a reference.
144    */

145   public Value getArg(Value index)
146   {
147     return getCopyArray().getArg(index);
148   }
149
150   /**
151    * Returns the field value, creating an object if it's unset.
152    */

153   public Value getObject(Env env, Value fieldName)
154   {
155     return getCopyArray().getObject(env, fieldName);
156   }
157
158   /**
159    * Sets the array ref.
160    */

161   public Value putRef()
162   {
163     return getCopyArray().putRef();
164   }
165
166   /**
167    * Add
168    */

169   public ArrayValue append(Value key, Value value)
170   {
171     put(key, value.toArgValue());
172
173     return this;
174   }
175
176   /**
177    * Add
178    */

179   public ArrayValue append(Value value)
180   {
181     return getCopyArray().append(value);
182   }
183
184   /**
185    * Gets a new value.
186    */

187   public Value get(Value key)
188   {
189     if (_copyArray != null)
190       return _copyArray.get(key);
191     else
192       return _constArray.get(key);
193   }
194
195   /**
196    * Gets a new value.
197    */

198   public Value containsKey(Value key)
199   {
200     if (_copyArray != null)
201       return _copyArray.containsKey(key);
202     else
203       return _constArray.containsKey(key);
204   }
205
206   /**
207    * Removes a value.
208    */

209   public Value remove(Value key)
210   {
211     return getCopyArray().remove(key);
212   }
213
214   /**
215    * Returns the array ref.
216    */

217   public Var getRef(Value index)
218   {
219     return getCopyArray().getRef(index);
220   }
221
222   /**
223    * Convenience for lib.
224    */

225   public void put(String JavaDoc key, String JavaDoc value)
226   {
227     put(new StringValueImpl(key), new StringValueImpl(value));
228   }
229
230   /**
231    * Pops the top value.
232    */

233   public Value pop()
234   {
235     return getCopyArray().pop();
236   }
237
238   /**
239    * Pops the top value.
240    */

241   public Value createTailKey()
242   {
243     return getCopyArray().createTailKey();
244   }
245
246   /**
247    * Shuffles the array
248    */

249   public void shuffle()
250   {
251     getCopyArray().shuffle();
252   }
253
254   public Entry getHead()
255   {
256     if (_copyArray != null)
257       return _copyArray.getHead();
258     else
259       return _constArray.getHead();
260   }
261
262   protected Entry getTail()
263   {
264     if (_copyArray != null)
265       return _copyArray.getTail();
266     else
267       return _constArray.getTail();
268   }
269
270   private ArrayValue getCopyArray()
271   {
272     if (_copyArray == null)
273       _copyArray = new ArrayValueImpl(_constArray);
274
275     return _copyArray;
276   }
277 }
278
279
Popular Tags