001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.env;
031:
032: import com.caucho.vfs.WriteStream;
033:
034: import java.io.IOException;
035: import java.io.PrintWriter;
036: import java.io.Serializable;
037: import java.util.IdentityHashMap;
038:
039: /**
040: * Represents a PHP double value.
041: */
042: public class DoubleValue extends NumberValue implements Serializable {
043: public static final DoubleValue ZERO = new DoubleValue(0);
044:
045: private final double _value;
046:
047: public DoubleValue(double value) {
048: _value = value;
049: }
050:
051: public static DoubleValue create(double value) {
052: return new DoubleValue(value);
053: }
054:
055: public static Value create(Number value) {
056: if (value == null) {
057: // php/3c2d
058: return NullValue.NULL;
059: } else
060: return new DoubleValue(value.doubleValue());
061: }
062:
063: /**
064: * Returns the type.
065: */
066: public String getType() {
067: return "float";
068: }
069:
070: /**
071: * Returns the ValueType.
072: */
073: @Override
074: public ValueType getValueType() {
075: return ValueType.DOUBLE;
076: }
077:
078: /**
079: * Returns true for a double.
080: */
081: public boolean isDoubleConvertible() {
082: return true;
083: }
084:
085: /**
086: * Returns true for integer looking doubles.
087: */
088: public boolean isLongConvertible() {
089: return _value == (double) ((long) _value);
090: }
091:
092: /**
093: * Returns true for is_numeric
094: */
095: @Override
096: public boolean isNumeric() {
097: return true;
098: }
099:
100: /**
101: * Returns true for a scalar
102: */
103: public boolean isScalar() {
104: return true;
105: }
106:
107: /**
108: * Converts to a boolean.
109: */
110: public boolean toBoolean() {
111: return _value != 0;
112: }
113:
114: /**
115: * Converts to a long.
116: */
117: public long toLong() {
118: return (long) _value;
119: }
120:
121: /**
122: * Converts to a double.
123: */
124: public double toDouble() {
125: return _value;
126: }
127:
128: /**
129: * Converts to a double.
130: */
131: public DoubleValue toDoubleValue() {
132: return this ;
133: }
134:
135: /**
136: * Converts to a string builder
137: */
138: @Override
139: public StringValue toStringBuilder(Env env) {
140: return env.createUnicodeBuilder().append(_value);
141: }
142:
143: /**
144: * Converts to a key.
145: */
146: public Value toKey() {
147: return LongValue.create((long) _value);
148: }
149:
150: /**
151: * Converts to a java object.
152: */
153: public Object toJavaObject() {
154: return new Double(_value);
155: }
156:
157: /**
158: * Negates the value.
159: */
160: public Value neg() {
161: return new DoubleValue(-_value);
162: }
163:
164: /**
165: * Returns the value
166: */
167: public Value pos() {
168: return this ;
169: }
170:
171: /**
172: * Multiplies to the following value.
173: */
174: public Value add(Value rValue) {
175: return new DoubleValue(_value + rValue.toDouble());
176: }
177:
178: /**
179: * Multiplies to the following value.
180: */
181: public Value add(long lValue) {
182: return new DoubleValue(lValue + _value);
183: }
184:
185: /**
186: * Pre-increment the following value.
187: */
188: public Value preincr(int incr) {
189: return new DoubleValue(_value + incr);
190: }
191:
192: /**
193: * Post-increment the following value.
194: */
195: public Value postincr(int incr) {
196: return new DoubleValue(_value + incr);
197: }
198:
199: /**
200: * Multiplies to the following value.
201: */
202: public Value mul(Value rValue) {
203: return new DoubleValue(_value * rValue.toDouble());
204: }
205:
206: /**
207: * Multiplies to the following value.
208: */
209: public Value mul(long lValue) {
210: return new DoubleValue(lValue * _value);
211: }
212:
213: /**
214: * Returns true for equality
215: */
216: public boolean eql(Value rValue) {
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 toString() {
232: long longValue = (long) _value;
233:
234: if (longValue == _value)
235: return String.valueOf(longValue);
236: else
237: return String.valueOf(_value);
238: }
239:
240: /**
241: * Converts to an object.
242: */
243: public Object toObject() {
244: return toString();
245: }
246:
247: /**
248: * Prints the value.
249: * @param env
250: */
251: public void print(Env env) {
252: env.print(toString());
253: }
254:
255: /**
256: * Serializes the value.
257: */
258: public void serialize(StringBuilder sb) {
259: sb.append("d:");
260: sb.append(_value);
261: sb.append(";");
262: }
263:
264: /**
265: * Exports the value.
266: */
267: public void varExport(StringBuilder sb) {
268: sb.append(toString());
269: }
270:
271: //
272: // Java generator code
273: //
274:
275: /**
276: * Generates code to recreate the expression.
277: *
278: * @param out the writer to the Java source code.
279: */
280: public void generate(PrintWriter out) throws IOException {
281: if (_value == 0)
282: out.print("DoubleValue.ZERO");
283: else
284: out.print("new DoubleValue(" + _value + ")");
285: }
286:
287: /**
288: * Returns the hash code
289: */
290: public int hashCode() {
291: return (int) (37 + 65521 * _value);
292: }
293:
294: /**
295: * Compare for equality.
296: */
297: public boolean equals(Object o) {
298: if (this == o)
299: return true;
300: else if (!(o instanceof DoubleValue))
301: return false;
302:
303: DoubleValue value = (DoubleValue) o;
304:
305: return _value == value._value;
306: }
307:
308: public void varDumpImpl(Env env, WriteStream out, int depth,
309: IdentityHashMap<Value, String> valueSet) throws IOException {
310: out.print("float(" + toString() + ")");
311: }
312:
313: //
314: // Java Serialization
315: //
316:
317: private Object readResolve() {
318: if (_value == 0)
319: return ZERO;
320: else
321: return this;
322: }
323:
324: }
|