KickJava   Java API By Example, From Geeks To Geeks.

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


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.vfs.WriteStream;
33
34 import java.io.IOException JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.io.Serializable JavaDoc;
37 import java.util.IdentityHashMap JavaDoc;
38
39 /**
40  * Represents a PHP long value.
41  */

42 public class LongValue extends NumberValue
43   implements Serializable JavaDoc
44 {
45   public static final LongValue MINUS_ONE = new LongValue(-1);
46   public static final LongValue ZERO = new LongValue(0);
47   public static final LongValue ONE = new LongValue(1);
48
49   public static final int STATIC_MIN = -256;
50   public static final int STATIC_MAX = 256;
51
52   public static final LongValue[]STATIC_VALUES;
53
54   private final long _value;
55
56   public LongValue(long value)
57   {
58     _value = value;
59   }
60
61   public static LongValue create(long value)
62   {
63     if (STATIC_MIN <= value && value <= STATIC_MAX)
64       return STATIC_VALUES[(int) (value - STATIC_MIN)];
65     else
66       return new LongValue(value);
67   }
68
69   public static LongValue create(Number JavaDoc value)
70   {
71     if (value == null)
72       return LongValue.ZERO;
73     else
74       return new LongValue(value.longValue());
75   }
76
77   /**
78    * Returns the type.
79    */

80   public String JavaDoc getType()
81   {
82     return "integer";
83   }
84
85   /**
86    * Returns true for a long.
87    */

88   public boolean isLongConvertible()
89   {
90     return true;
91   }
92
93   /**
94    * Returns true for is_numeric
95    */

96   @Override JavaDoc
97   public boolean isNumeric()
98   {
99     return true;
100   }
101
102   /**
103    * Returns true for a scalar
104    */

105   public boolean isScalar()
106   {
107     return true;
108   }
109
110   /**
111    * Converts to a boolean.
112    */

113   public boolean toBoolean()
114   {
115     return _value != 0;
116   }
117
118   /**
119    * Converts to a long.
120    */

121   public long toLong()
122   {
123     return _value;
124   }
125
126   /**
127    * Converts to a double.
128    */

129   public double toDouble()
130   {
131     return _value;
132   }
133
134   /**
135    * Converts to a string.
136    */

137   public String JavaDoc toString()
138   {
139     return String.valueOf(_value);
140   }
141
142   /**
143    * Converts to a long value
144    */

145   public Value toLongValue()
146   {
147     return this;
148   }
149
150   /**
151    * Converts to a key.
152    */

153   public Value toKey()
154   {
155     return this;
156   }
157
158   /**
159    * Converts to an object.
160    */

161   public Object JavaDoc toObject()
162   {
163     return String.valueOf(_value);
164   }
165
166   /**
167    * Converts to a java object.
168    */

169   public Object JavaDoc toJavaObject()
170   {
171     return new Long JavaDoc(_value);
172   }
173
174   /**
175    * Negates the value.
176    */

177   public Value neg()
178   {
179     return new LongValue(- _value);
180   }
181
182   /**
183    * Negates the value.
184    */

185   public Value pos()
186   {
187     return this;
188   }
189
190   /**
191    * Pre-increment the following value.
192    */

193   public Value preincr(int incr)
194   {
195     return LongValue.create(_value + incr);
196   }
197
198   /**
199    * Post-increment the following value.
200    */

201   public Value postincr(int incr)
202   {
203     return LongValue.create(_value + incr);
204   }
205
206   /**
207    * Adds to the following value.
208    */

209   @Override JavaDoc
210   public Value add(long lLong)
211   {
212     return LongValue.create(lLong + _value);
213   }
214
215   /**
216    * Subtracts the following value.
217    */

218   public Value sub(long rLong)
219   {
220     return LongValue.create(_value - rLong);
221   }
222
223   /**
224    * Returns true for equality
225    */

226   public boolean eql(Value rValue)
227   {
228     rValue = rValue.toValue();
229
230     if (! (rValue instanceof LongValue))
231       return false;
232
233     long rLong = ((LongValue) rValue)._value;
234
235     return _value == rLong;
236   }
237
238   /**
239    * Prints the value.
240    * @param env
241    */

242   public void print(Env env)
243   {
244     env.print(_value);
245   }
246
247   /**
248    * Append to a string builder.
249    */

250   @Override JavaDoc
251   public void appendTo(StringBuilderValue sb)
252   {
253     sb.append(_value);
254   }
255
256   /**
257    * Append to a binary builder.
258    */

259   @Override JavaDoc
260   public void appendTo(BinaryBuilderValue sb)
261   {
262     sb.append(_value);
263   }
264
265   /**
266    * Serializes the value.
267    */

268   public void serialize(StringBuilder JavaDoc sb)
269   {
270     sb.append("i:");
271     sb.append(_value);
272     sb.append(";");
273   }
274
275   /**
276    * Exports the value.
277    */

278   public void varExport(StringBuilder JavaDoc sb)
279   {
280     sb.append(_value);
281   }
282
283   //
284
// Java generator code
285
//
286

287   /**
288    * Generates code to recreate the expression.
289    *
290    * @param out the writer to the Java source code.
291    */

292   public void generate(PrintWriter JavaDoc out)
293     throws IOException JavaDoc
294   {
295     if (_value == 0)
296       out.print("LongValue.ZERO");
297     else if (_value == 1)
298       out.print("LongValue.ONE");
299     else if (_value == -1)
300       out.print("LongValue.MINUS_ONE");
301     else if (STATIC_MIN <= _value && _value <= STATIC_MAX)
302       out.print("LongValue.STATIC_VALUES[" + (_value - STATIC_MIN) + "]");
303     else
304       out.print("new LongValue(" + _value + "L)");
305   }
306
307   /**
308    * Returns the hash code
309    */

310   public final int hashCode()
311   {
312     long v = _value;
313     
314     return (int) (17 * v + 65537 * (v >> 32));
315   }
316
317   /**
318    * Compare for equality.
319    */

320   public boolean equals(Object JavaDoc o)
321   {
322     if (this == o)
323       return true;
324     else if (! (o instanceof LongValue))
325       return false;
326
327     LongValue value = (LongValue) o;
328
329     return _value == value._value;
330   }
331
332   public void varDumpImpl(Env env,
333                           WriteStream out,
334                           int depth,
335                           IdentityHashMap JavaDoc<Value,String JavaDoc> valueSet)
336     throws IOException JavaDoc
337   {
338     out.print("int(" + toLong() + ")");
339   }
340   
341   //
342
// Java Serialization
343
//
344

345   private Object JavaDoc readResolve()
346   {
347     if (STATIC_MIN <= _value && _value <= STATIC_MAX)
348       return STATIC_VALUES[(int) (_value - STATIC_MIN)];
349     else
350       return this;
351   }
352
353   static {
354     STATIC_VALUES = new LongValue[STATIC_MAX - STATIC_MIN + 1];
355
356     for (int i = STATIC_MIN; i <= STATIC_MAX; i++) {
357       STATIC_VALUES[i - STATIC_MIN] = new LongValue(i);
358     }
359   }
360 }
361
Popular Tags