001: /*
002: * Copyright (c) 1998-2006 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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: import com.caucho.vfs.VfsWriteObject;
032: import com.caucho.vfs.WriteStream;
033:
034: import java.io.Externalizable;
035: import java.io.IOException;
036: import java.io.ObjectInput;
037: import java.io.ObjectOutput;
038:
039: /**
040: * Implementation class for JavaScript numbers. Essentially, these are
041: * equivalent to Java doubles.
042: */
043: public class ESNumber extends ESBase implements VfsWriteObject,
044: Externalizable {
045: public static ESNumber ZERO = new ESNumber(0.0);
046: public static ESNumber ONE = new ESNumber(1.0);
047: public static ESNumber NaN = new ESNumber(0.0 / 0.0);
048: static ESNumber ints[];
049: static {
050: ints = new ESNumber[128];
051: for (int i = 0; i < ints.length; i++)
052: ints[i] = new ESNumber(i);
053: }
054:
055: private double value;
056:
057: /**
058: * Null-arg constructor for serialization.
059: */
060: public ESNumber() {
061: prototype = esNull;
062: }
063:
064: /**
065: * Create a new object based on a prototype
066: */
067: private ESNumber(double value) {
068: prototype = esNull;
069: this .value = value;
070: }
071:
072: public static ESNumber create(double value) {
073: try {
074: // Can't use 0 because of -0
075: if (value >= 128 || value <= 0)
076: return new ESNumber(value);
077:
078: int intValue = (int) value;
079: if (intValue == value)
080: return ints[intValue];
081: else
082: return new ESNumber(value);
083: } catch (Exception e) {
084: return new ESNumber(value);
085: }
086: }
087:
088: /**
089: * Any non-zero number is true.
090: *
091: * XXX: NaN and inf?
092: */
093: public boolean toBoolean() {
094: return !Double.isNaN(value) && value != 0.0;
095: }
096:
097: public boolean isNum() {
098: return true;
099: }
100:
101: public double toNum() {
102: return value;
103: }
104:
105: public ESObject toObject() throws ESException {
106: return new ESWrapper("Number",
107: Global.getGlobalProto().numProto, this );
108: }
109:
110: public Object toJavaObject() {
111: return new Double(value);
112: }
113:
114: public ESBase typeof() throws ESException {
115: return ESString.create("number");
116: }
117:
118: public Class getJavaType() {
119: if ((int) value == value)
120: return int.class;
121: else
122: return double.class;
123: }
124:
125: public ESBase getProperty(ESString key) throws Throwable {
126: return Global.getGlobalProto().numProto.getProperty(key);
127: }
128:
129: public ESString toStr() {
130: int intValue = (int) value;
131:
132: if (intValue == value)
133: return ESString.create(intValue);
134: else
135: return ESString.create(toString());
136: }
137:
138: /**
139: * Returns the string representation of the number.
140: *
141: * Notes: the spec says
142: * 1) -0 should be printed at 0.
143: * 2) 20 decimal digit integers should be printed as integers.
144: * This is insane since the double can only almost a 16 digit decimal.
145: * 3) The exponent should be lower case.
146: */
147: public String toString() {
148: int intValue = (int) value;
149:
150: if (intValue == value)
151: return String.valueOf(intValue);
152: else if ((long) value == value)
153: return String.valueOf((long) value);
154: else if (Double.isNaN(value))
155: return "NaN";
156: else if (Double.isInfinite(value))
157: return (value < 0 ? "-Infinity" : "Infinity");
158:
159: return String.valueOf(value).toLowerCase();
160: }
161:
162: public void print(WriteStream os) throws IOException {
163: int intValue = (int) value;
164:
165: if (intValue == value)
166: os.print(intValue);
167: else if ((long) value == value)
168: os.print((long) value);
169: else if (Double.isNaN(value))
170: os.print("NaN");
171: else if (Double.isInfinite(value))
172: os.print(value < 0 ? "-Infinity" : "Infinity");
173: else
174: os.print(value);
175: }
176:
177: public int hashCode() {
178: long bits = Double.doubleToLongBits(value);
179:
180: return (int) bits + 65517 * (int) (bits >> 32);
181: }
182:
183: public boolean equals(Object b) {
184: return (b instanceof ESNumber) && value == ((ESNumber) b).value;
185: }
186:
187: public boolean ecmaEquals(ESBase b) throws Throwable {
188: return b != esNull && value == b.toNum();
189: }
190:
191: public boolean lessThan(ESBase b, boolean neg) throws Throwable {
192: double db = b.toNum();
193:
194: if (Double.isNaN(value) || Double.isNaN(db))
195: return false;
196: else
197: return (value < db) != neg;
198: }
199:
200: public ESBase plus(ESBase b) throws Throwable {
201: if (b instanceof ESNumber)
202: return create(value + ((ESNumber) b).value);
203: else {
204: ESBase primB = b.toPrimitive(NONE);
205:
206: if (primB instanceof ESString)
207: return ESString.create(toString() + primB.toString());
208: else
209: return create(value + primB.toNum());
210: }
211: }
212:
213: /**
214: * Save the external representation.
215: */
216: public void writeExternal(ObjectOutput os) throws IOException {
217: os.writeDouble(value);
218: }
219:
220: /**
221: * Restore the external representation.
222: */
223: public void readExternal(ObjectInput is) throws IOException {
224: value = is.readDouble();
225: }
226: }
|