001: /*
002: * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*
026: * COMPONENT_NAME: idl.parser
027: *
028: * ORIGINS: 27
029: *
030: * Licensed Materials - Property of IBM
031: * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
032: * RMI-IIOP v1.0
033: *
034: * @(#)Divide.java 1.20 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.constExpr;
038:
039: // NOTES:
040: // -D52042<daz> Added protypical code for computing mixed-operand binary
041: // expressions, which promotes result to Double only when the target type
042: // is floating-point. Code violates spec, but may be usable at some future
043: // time.
044:
045: import com.sun.tools.corba.se.idl.Util;
046: import java.math.BigInteger;
047:
048: /**
049: *
050: **/
051: public class Divide extends BinaryExpr {
052: /**
053: * Constructor: set operation and operands.
054: **/
055: protected Divide(Expression leftOperand, Expression rightOperand) {
056: super ("/", leftOperand, rightOperand);
057: } // ctor
058:
059: /**
060: *
061: **/
062: public Object evaluate() throws EvaluationException {
063: try {
064: Number l = (Number) left().evaluate();
065: Number r = (Number) right().evaluate();
066:
067: boolean lIsNonInteger = l instanceof Float
068: || l instanceof Double;
069: boolean rIsNonInteger = r instanceof Float
070: || r instanceof Double;
071:
072: if (lIsNonInteger && rIsNonInteger)
073: value(new Double(l.doubleValue() / r.doubleValue()));
074: else if (lIsNonInteger || rIsNonInteger) {
075: String[] parameters = {
076: Util.getMessage("EvaluationException.divide"),
077: left().value().getClass().getName(),
078: right().value().getClass().getName() };
079: throw new EvaluationException(Util.getMessage(
080: "EvaluationException.1", parameters));
081: } else {
082: BigInteger tmpL = (BigInteger) l, tmpR = (BigInteger) r;
083: value(tmpL.divide(tmpR));
084: }
085: // <d52042> Allow evaluation over mixed operands. Supplant code above.
086: /*
087: Number l = (Number)left ().evaluate ();
088: Number r = (Number)right ().evaluate ();
089:
090: boolean lIsNonInteger = l instanceof Float || l instanceof Double;
091: boolean rIsNonInteger = r instanceof Float || r instanceof Double;
092:
093: // Floating-point operands.
094: if (lIsNonInteger && rIsNonInteger)
095: {
096: value (new Double (l.doubleValue () / r.doubleValue ()));
097: }
098: // Integral operands.
099: else if (!(lIsNonInteger || rIsNonInteger))
100: {
101: BigInteger tmpL = (BigInteger)l, tmpR = (BigInteger)r;
102: value (tmpL.divide (tmpR));
103: }
104: // Mixed operands: one operand is floating-point, the other is integral.
105: else
106: {
107: // Legal over floating-point types only.
108: if (type ().equals ("float") ||
109: type ().equals ("double"))
110: {
111: value (new Double (l.doubleValue () / r.doubleValue ()));
112: }
113: else
114: {
115: String[] parameters = {Util.getMessage ("EvaluationException.divide"),
116: left ().value ().getClass ().getName (),
117: right ().value ().getClass ().getName ()};
118: throw new EvaluationException (Util.getMessage ("EvaluationException.1", parameters));
119: }
120: }
121: */
122: } catch (ClassCastException e) {
123: String[] parameters = {
124: Util.getMessage("EvaluationException.divide"),
125: left().value().getClass().getName(),
126: right().value().getClass().getName() };
127: throw new EvaluationException(Util.getMessage(
128: "EvaluationException.1", parameters));
129: }
130: return value();
131: } // evaluate
132: } // class Divide
|