001: /*=============================================================================
002: * Copyright Texas Instruments 2000-2004. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.data;
022:
023: import oscript.exceptions.*;
024:
025: /**
026: * An inexact number is a non-integer number. An exact number can be
027: * converted to an inexact-number with no loss of precision, but not
028: * visa-versa. An <code>OInxactNumber</code> is immutable, meaning once
029: * the instance is constructed, it won't change.
030: *
031: * @author Rob Clark (rob@ti.com)
032: */
033: public class OInexactNumber extends OObject implements
034: java.io.Externalizable {
035: /**
036: * The type object for an instance of InexactNumber.
037: */
038: public final static Value TYPE = BuiltinType
039: .makeBuiltinType("oscript.data.OInexactNumber");
040: public final static String PARENT_TYPE_NAME = "oscript.data.OObject";
041: public final static String TYPE_NAME = "InexactNumber";
042: public final static String[] MEMBER_NAMES = new String[] {
043: "castToString", "castToExactNumber", "castToInexactNumber",
044: "castToJavaObject", "intValue", "floatValue", "shortValue",
045: "charValue", "byteValue", "bopEquals", "bopEqualsR",
046: "bopNotEquals", "bopNotEqualsR", "bopLessThan",
047: "bopLessThanR", "bopGreaterThan", "bopGreaterThanR",
048: "bopLessThanOrEquals", "bopLessThanOrEqualsR",
049: "bopGreaterThanOrEquals", "bopGreaterThanOrEqualsR",
050: "bopPlus", "bopPlusR", "bopMinus", "bopMinusR",
051: "bopMultiply", "bopMultiplyR", "bopDivide", "bopDivideR",
052: "uopIncrement", "uopDecrement", "uopPlus", "uopMinus", };
053:
054: /*=======================================================================*/
055: /**
056: */
057: public static final OInexactNumber makeInexactNumber(
058: double doubleVal) {
059: return new OInexactNumber(doubleVal);
060: }
061:
062: /*=======================================================================*/
063: // members:
064: private double doubleVal;
065:
066: // Externalizable support:
067: public OInexactNumber() {
068: }
069:
070: /**
071: * Derived class that implements {@link java.io.Externalizable} must
072: * call this if it overrides it. It should override it to save/restore
073: * it's own state.
074: */
075: public void readExternal(java.io.ObjectInput in)
076: throws java.io.IOException {
077: doubleVal = in.readDouble();
078: }
079:
080: /**
081: * Derived class that implements {@link java.io.Externalizable} must
082: * call this if it overrides it. It should override it to save/restore
083: * it's own state.
084: */
085: public void writeExternal(java.io.ObjectOutput out)
086: throws java.io.IOException {
087: out.writeDouble(doubleVal);
088: }
089:
090: /*=======================================================================*/
091:
092: /*=======================================================================*/
093: /**
094: * Class Constructor.
095: *
096: * @param doubleVal the value of this inexact number
097: */
098: public OInexactNumber(double doubleVal) {
099: super ();
100: this .doubleVal = doubleVal;
101: }
102:
103: /*=======================================================================*/
104: /**
105: * Class Constructor. This is the constructor that is called via a
106: * <code>BuiltinType</code> instance.
107: *
108: * @param args arguments to this constructor
109: * @throws PackagedScriptObjectException(Exception) if wrong number of args
110: */
111: public OInexactNumber(oscript.util.MemberTable args) {
112: super ();
113:
114: if (args.length() != 1) {
115: throw PackagedScriptObjectException
116: .makeExceptionWrapper(new OIllegalArgumentException(
117: "wrong number of args!"));
118: } else {
119: doubleVal = args.referenceAt(0).castToInexactNumber();
120: }
121: }
122:
123: /*=======================================================================*/
124: /**
125: * Return a hash code value for this object.
126: *
127: * @return a hash code value
128: * @see java.lang.Object#hashCode()
129: */
130: public int hashCode() {
131: return castToJavaObject().hashCode();
132: }
133:
134: /*=======================================================================*/
135: /**
136: * Compare two objects for equality.
137: *
138: * @param obj the object to compare to this object
139: * @return <code>true</code> if equals, else <code>false</code>
140: * @see java.lang.Object#equals(java.lang.Object)
141: */
142: public boolean equals(Object obj) {
143: if ((obj instanceof OInexactNumber)
144: && (((OInexactNumber) obj).doubleVal == doubleVal)) {
145: return true;
146: } else if ((obj instanceof Number)
147: && (((Number) obj).doubleValue() == doubleVal)) {
148: return true;
149: } else {
150: return false;
151: }
152: }
153:
154: /*=======================================================================*/
155: /**
156: * Get the type of this object. The returned type doesn't have to take
157: * into account the possibility of a script type extending a built-in
158: * type, since that is handled by {@link #getType}.
159: *
160: * @return the object's type
161: */
162: protected Value getTypeImpl() {
163: return TYPE;
164: }
165:
166: /*=======================================================================*/
167: /**
168: * Convert this object to a native java <code>String</code> value.
169: *
170: * @return a String value
171: * @throws PackagedScriptObjectException(NoSuchMethodException)
172: */
173: public String castToString() throws PackagedScriptObjectException {
174: return String.valueOf(doubleVal);
175: }
176:
177: /*=======================================================================*/
178: /**
179: * Convert this object to a native java <code>long</code> value.
180: *
181: * @return a long value
182: * @throws PackagedScriptObjectException(NoSuchMemberException)
183: */
184: public long castToExactNumber()
185: throws PackagedScriptObjectException {
186: return (long) doubleVal;
187: }
188:
189: /*=======================================================================*/
190: /**
191: * Convert this object to a native java <code>double</code> value.
192: *
193: * @return a double value
194: * @throws PackagedScriptObjectException(NoSuchMethodException)
195: */
196: public double castToInexactNumber()
197: throws PackagedScriptObjectException {
198: return doubleVal;
199: }
200:
201: public static Value _bopCast(Value val) {
202: if (val instanceof OInexactNumber)
203: return val;
204: return makeInexactNumber(val.castToInexactNumber());
205: }
206:
207: /*=======================================================================*/
208: /**
209: * Convert this object to a native java <code>Object</code> value.
210: *
211: * @return a java object
212: * @throws PackagedScriptObjectException(NoSuchMemberException)
213: */
214: public Object castToJavaObject()
215: throws PackagedScriptObjectException {
216: return new Double(doubleVal);
217: }
218:
219: public int intValue() {
220: return (int) doubleVal;
221: }
222:
223: public float floatValue() {
224: return (float) doubleVal;
225: }
226:
227: public short shortValue() {
228: return (short) doubleVal;
229: }
230:
231: public char charValue() {
232: return (char) doubleVal;
233: }
234:
235: public byte byteValue() {
236: return (byte) doubleVal;
237: }
238:
239: /*=======================================================================*/
240: /* The binary operators:
241: */
242:
243: /*=======================================================================*/
244: /**
245: * Perform the "==" operation.
246: *
247: * @param val the other value
248: * @return the result
249: * @throws PackagedScriptObjectException(NoSuchMethodException)
250: */
251: public Value bopEquals(Value val)
252: throws PackagedScriptObjectException {
253: try {
254: return OBoolean.makeBoolean(doubleVal == val
255: .castToInexactNumber());
256: } catch (PackagedScriptObjectException e) {
257: return val.bopEqualsR(this , e);
258: }
259: }
260:
261: public Value bopEqualsR(Value val, PackagedScriptObjectException e)
262: throws PackagedScriptObjectException {
263: return OBoolean
264: .makeBoolean(val.castToInexactNumber() == doubleVal);
265: }
266:
267: /*=======================================================================*/
268: /**
269: * Perform the "!=" operation.
270: *
271: * @param val the other value
272: * @return the result
273: * @throws PackagedScriptObjectException(NoSuchMethodException)
274: */
275: public Value bopNotEquals(Value val)
276: throws PackagedScriptObjectException {
277: try {
278: return OBoolean.makeBoolean(doubleVal != val
279: .castToInexactNumber());
280: } catch (PackagedScriptObjectException e) {
281: return val.bopNotEqualsR(this , e);
282: }
283: }
284:
285: public Value bopNotEqualsR(Value val,
286: PackagedScriptObjectException e)
287: throws PackagedScriptObjectException {
288: return OBoolean
289: .makeBoolean(val.castToInexactNumber() != doubleVal);
290: }
291:
292: /*=======================================================================*/
293: /**
294: * Perform the "<" operation.
295: *
296: * @param val the other value
297: * @return the result
298: * @throws PackagedScriptObjectException(NoSuchMethodException)
299: */
300: public Value bopLessThan(Value val)
301: throws PackagedScriptObjectException {
302: try {
303: return OBoolean.makeBoolean(doubleVal < val
304: .castToInexactNumber());
305: } catch (PackagedScriptObjectException e) {
306: return val.bopLessThanR(this , e);
307: }
308: }
309:
310: public Value bopLessThanR(Value val, PackagedScriptObjectException e)
311: throws PackagedScriptObjectException {
312: return OBoolean
313: .makeBoolean(val.castToInexactNumber() < doubleVal);
314: }
315:
316: /*=======================================================================*/
317: /**
318: * Perform the ">" operation.
319: *
320: * @param val the other value
321: * @return the result
322: * @throws PackagedScriptObjectException(NoSuchMethodException)
323: */
324: public Value bopGreaterThan(Value val)
325: throws PackagedScriptObjectException {
326: try {
327: return OBoolean.makeBoolean(doubleVal > val
328: .castToInexactNumber());
329: } catch (PackagedScriptObjectException e) {
330: return val.bopGreaterThanR(this , e);
331: }
332: }
333:
334: public Value bopGreaterThanR(Value val,
335: PackagedScriptObjectException e)
336: throws PackagedScriptObjectException {
337: return OBoolean
338: .makeBoolean(val.castToInexactNumber() > doubleVal);
339: }
340:
341: /*=======================================================================*/
342: /**
343: * Perform the "<=" operation.
344: *
345: * @param val the other value
346: * @return the result
347: * @throws PackagedScriptObjectException(NoSuchMethodException)
348: */
349: public Value bopLessThanOrEquals(Value val)
350: throws PackagedScriptObjectException {
351: try {
352: return OBoolean.makeBoolean(doubleVal <= val
353: .castToInexactNumber());
354: } catch (PackagedScriptObjectException e) {
355: return val.bopLessThanOrEqualsR(this , e);
356: }
357: }
358:
359: public Value bopLessThanOrEqualsR(Value val,
360: PackagedScriptObjectException e)
361: throws PackagedScriptObjectException {
362: return OBoolean
363: .makeBoolean(val.castToInexactNumber() <= doubleVal);
364: }
365:
366: /*=======================================================================*/
367: /**
368: * Perform the ">=" operation.
369: *
370: * @param val the other value
371: * @return the result
372: * @throws PackagedScriptObjectException(NoSuchMethodException)
373: */
374: public Value bopGreaterThanOrEquals(Value val)
375: throws PackagedScriptObjectException {
376: try {
377: return OBoolean.makeBoolean(doubleVal >= val
378: .castToInexactNumber());
379: } catch (PackagedScriptObjectException e) {
380: return val.bopGreaterThanOrEqualsR(this , e);
381: }
382: }
383:
384: public Value bopGreaterThanOrEqualsR(Value val,
385: PackagedScriptObjectException e)
386: throws PackagedScriptObjectException {
387: return OBoolean
388: .makeBoolean(val.castToInexactNumber() >= doubleVal);
389: }
390:
391: /*=======================================================================*/
392: /**
393: * Perform the "+" operation.
394: *
395: * @param val the other value
396: * @return the result
397: * @throws PackagedScriptObjectException(NoSuchMethodException)
398: */
399: public Value bopPlus(Value val)
400: throws PackagedScriptObjectException {
401: try {
402: return OInexactNumber.makeInexactNumber(doubleVal
403: + val.castToInexactNumber());
404: } catch (PackagedScriptObjectException e) {
405: return val.bopPlusR(this , e);
406: }
407: }
408:
409: public Value bopPlusR(Value val, PackagedScriptObjectException e)
410: throws PackagedScriptObjectException {
411: return OInexactNumber.makeInexactNumber(val
412: .castToInexactNumber()
413: + doubleVal);
414: }
415:
416: /*=======================================================================*/
417: /**
418: * Perform the "-" operation.
419: *
420: * @param val the other value
421: * @return the result
422: * @throws PackagedScriptObjectException(NoSuchMethodException)
423: */
424: public Value bopMinus(Value val)
425: throws PackagedScriptObjectException {
426: try {
427: return OInexactNumber.makeInexactNumber(doubleVal
428: - val.castToInexactNumber());
429: } catch (PackagedScriptObjectException e) {
430: return val.bopMinusR(this , e);
431: }
432: }
433:
434: public Value bopMinusR(Value val, PackagedScriptObjectException e)
435: throws PackagedScriptObjectException {
436: return OInexactNumber.makeInexactNumber(val
437: .castToInexactNumber()
438: - doubleVal);
439: }
440:
441: /*=======================================================================*/
442: /**
443: * Perform the "*" operation.
444: *
445: * @param val the other value
446: * @return the result
447: * @throws PackagedScriptObjectException(NoSuchMethodException)
448: */
449: public Value bopMultiply(Value val)
450: throws PackagedScriptObjectException {
451: try {
452: return OInexactNumber.makeInexactNumber(doubleVal
453: * val.castToInexactNumber());
454: } catch (PackagedScriptObjectException e) {
455: return val.bopMultiplyR(this , e);
456: }
457: }
458:
459: public Value bopMultiplyR(Value val, PackagedScriptObjectException e)
460: throws PackagedScriptObjectException {
461: return OInexactNumber.makeInexactNumber(val
462: .castToInexactNumber()
463: * doubleVal);
464: }
465:
466: /*=======================================================================*/
467: /**
468: * Perform the "/" operation.
469: *
470: * @param val the other value
471: * @return the result
472: * @throws PackagedScriptObjectException(NoSuchMethodException)
473: */
474: public Value bopDivide(Value val)
475: throws PackagedScriptObjectException {
476: try {
477: return OInexactNumber.makeInexactNumber(doubleVal
478: / val.castToInexactNumber());
479: } catch (PackagedScriptObjectException e) {
480: return val.bopDivideR(this , e);
481: }
482: }
483:
484: public Value bopDivideR(Value val, PackagedScriptObjectException e)
485: throws PackagedScriptObjectException {
486: return OInexactNumber.makeInexactNumber(val
487: .castToInexactNumber()
488: / doubleVal);
489: }
490:
491: /*=======================================================================*/
492: /* The unary operators:
493: */
494:
495: /*=======================================================================*/
496: /**
497: * Perform the "++" operation.
498: *
499: * @param val the other value
500: * @return the result
501: * @throws PackagedScriptObjectException(NoSuchMethodException)
502: */
503: public Value uopIncrement() throws PackagedScriptObjectException {
504: return OInexactNumber.makeInexactNumber(doubleVal + 1.0);
505: }
506:
507: /*=======================================================================*/
508: /**
509: * Perform the "--" operation.
510: *
511: * @param val the other value
512: * @return the result
513: * @throws PackagedScriptObjectException(NoSuchMethodException)
514: */
515: public Value uopDecrement() throws PackagedScriptObjectException {
516: return OInexactNumber.makeInexactNumber(doubleVal - 1.0);
517: }
518:
519: /*=======================================================================*/
520: /**
521: * Perform the "+" operation.
522: *
523: * @param val the other value
524: * @return the result
525: * @throws PackagedScriptObjectException(NoSuchMethodException)
526: */
527: public Value uopPlus() throws PackagedScriptObjectException {
528: return OInexactNumber.makeInexactNumber(+doubleVal);
529: }
530:
531: /*=======================================================================*/
532: /**
533: * Perform the "-" operation.
534: *
535: * @param val the other value
536: * @return the result
537: * @throws PackagedScriptObjectException(NoSuchMethodException)
538: */
539: public Value uopMinus() throws PackagedScriptObjectException {
540: return OInexactNumber.makeInexactNumber(-doubleVal);
541: }
542: }
543:
544: /*
545: * Local Variables:
546: * tab-width: 2
547: * indent-tabs-mode: nil
548: * mode: java
549: * c-indentation-style: java
550: * c-basic-offset: 2
551: * eval: (c-set-offset 'substatement-open '0)
552: * eval: (c-set-offset 'case-label '+)
553: * eval: (c-set-offset 'inclass '+)
554: * eval: (c-set-offset 'inline-open '0)
555: * End:
556: */
|