KickJava   Java API By Example, From Geeks To Geeks.

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


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 double value.
41  */

42 public class DoubleValue extends NumberValue
43   implements Serializable JavaDoc
44 {
45   public static final DoubleValue ZERO = new DoubleValue(0);
46
47   private final double _value;
48
49   public DoubleValue(double value)
50   {
51     _value = value;
52   }
53
54   public static DoubleValue create(double value)
55   {
56     return new DoubleValue(value);
57   }
58
59   public static DoubleValue create(Number JavaDoc value)
60   {
61     if (value == null)
62       return DoubleValue.ZERO;
63     else
64       return new DoubleValue(value.doubleValue());
65   }
66
67   /**
68    * Returns the type.
69    */

70   public String JavaDoc getType()
71   {
72     return "float";
73   }
74
75   /**
76    * Returns true for a double.
77    */

78   public boolean isDoubleConvertible()
79   {
80     return true;
81   }
82
83   /**
84    * Returns true for is_numeric
85    */

86   @Override JavaDoc
87   public boolean isNumeric()
88   {
89     return true;
90   }
91
92   /**
93    * Returns true for a scalar
94    */

95   public boolean isScalar()
96   {
97     return true;
98   }
99
100   /**
101    * Converts to a boolean.
102    */

103   public boolean toBoolean()
104   {
105     return _value != 0;
106   }
107
108   /**
109    * Converts to a long.
110    */

111   public long toLong()
112   {
113     return (long) _value;
114   }
115
116   /**
117    * Converts to a double.
118    */

119   public double toDouble()
120   {
121     return _value;
122   }
123
124   /**
125    * Converts to a double.
126    */

127   public Value toDoubleValue()
128   {
129     return this;
130   }
131
132   /**
133    * Converts to a key.
134    */

135   public Value toKey()
136   {
137     return LongValue.create((long) _value);
138   }
139
140   /**
141    * Converts to a java object.
142    */

143   public Object JavaDoc toJavaObject()
144   {
145     return new Double JavaDoc(_value);
146   }
147
148   /**
149    * Negates the value.
150    */

151   public Value neg()
152   {
153     return new DoubleValue(- _value);
154   }
155
156   /**
157    * Returns the value
158    */

159   public Value pos()
160   {
161     return this;
162   }
163
164   /**
165    * Multiplies to the following value.
166    */

167   public Value add(Value rValue)
168   {
169     return new DoubleValue(_value + rValue.toDouble());
170   }
171
172   /**
173    * Multiplies to the following value.
174    */

175   public Value add(long lValue)
176   {
177     return new DoubleValue(lValue + _value);
178   }
179
180   /**
181    * Pre-increment the following value.
182    */

183   public Value preincr(int incr)
184   {
185     return new DoubleValue(_value + incr);
186   }
187
188   /**
189    * Post-increment the following value.
190    */

191   public Value postincr(int incr)
192   {
193     return new DoubleValue(_value + incr);
194   }
195
196   /**
197    * Multiplies to the following value.
198    */

199   public Value mul(Value rValue)
200   {
201     return new DoubleValue(_value * rValue.toDouble());
202   }
203
204   /**
205    * Multiplies to the following value.
206    */

207   public Value mul(long lValue)
208   {
209     return new DoubleValue(lValue * _value);
210   }
211
212   /**
213    * Returns true for equality
214    */

215   public boolean eql(Value rValue)
216   {
217     rValue = rValue.toValue();
218
219     if (! (rValue instanceof DoubleValue))
220       return false;
221
222     double rDouble = ((DoubleValue) rValue)._value;
223
224     return _value == rDouble;
225   }
226
227   /**
228    * Converts to a string.
229    * @param env
230    */

231   public String JavaDoc toString()
232   {
233     long longValue = (long) _value;
234
235     if (longValue == _value)
236       return String.valueOf(longValue);
237     else
238       return String.valueOf(_value);
239   }
240
241   /**
242    * Converts to an object.
243    */

244   public Object JavaDoc toObject()
245   {
246     return toString();
247   }
248
249   /**
250    * Prints the value.
251    * @param env
252    */

253   public void print(Env env)
254   {
255     env.print(toString());
256   }
257
258   /**
259    * Append to a string builder.
260    */

261   @Override JavaDoc
262   public void appendTo(StringBuilderValue sb)
263   {
264     sb.append(toString());
265   }
266
267   /**
268    * Append to a binary builder.
269    */

270   @Override JavaDoc
271   public void appendTo(BinaryBuilderValue sb)
272   {
273     sb.append(toString());
274   }
275
276   /**
277    * Serializes the value.
278    */

279   public void serialize(StringBuilder JavaDoc sb)
280   {
281     sb.append("d:");
282     sb.append(_value);
283     sb.append(";");
284   }
285
286   /**
287    * Exports the value.
288    */

289   public void varExport(StringBuilder JavaDoc sb)
290   {
291     sb.append(toString());
292   }
293
294   //
295
// Java generator code
296
//
297

298   /**
299    * Generates code to recreate the expression.
300    *
301    * @param out the writer to the Java source code.
302    */

303   public void generate(PrintWriter JavaDoc out)
304     throws IOException JavaDoc
305   {
306     if (_value == 0)
307       out.print("DoubleValue.ZERO");
308     else
309       out.print("new DoubleValue(" + _value + ")");
310   }
311
312   /**
313    * Returns the hash code
314    */

315   public int hashCode()
316   {
317     return (int) (37 + 65521 * _value);
318   }
319
320   /**
321    * Compare for equality.
322    */

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

348   private Object JavaDoc readResolve()
349   {
350     if (_value == 0)
351       return ZERO;
352     else
353       return this;
354   }
355
356 }
357
358
Popular Tags