Divide.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25/*
26 * COMPONENT_NAME: idl.parser
27 *
28 * ORIGINS: 27
29 *
30 * Licensed Materials - Property of IBM
31 * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
32 * RMI-IIOP v1.0
33 *
34 */
35
36package com.sun.tools.corba.se.idl.constExpr;
37
38// NOTES:
39// -D52042<daz> Added protypical code for computing mixed-operand binary
40//  expressions, which promotes result to Double only when the target type
41//  is floating-point. Code violates spec, but may be usable at some future
42//  time.
43
44import com.sun.tools.corba.se.idl.Util;
45import java.math.BigInteger;
46
47/**
48 *
49 **/
50public class Divide extends BinaryExpr
51{
52  /**
53   * Constructor: set operation and operands.
54   **/
55  protected Divide (Expression leftOperand, Expression rightOperand)
56  {
57    super ("/", leftOperand, rightOperand);
58  } // ctor
59
60  /**
61   *
62   **/
63  public Object evaluate () throws EvaluationException
64  {
65    try
66    {
67      Number l = (Number)left ().evaluate ();
68      Number r = (Number)right ().evaluate ();
69
70      boolean lIsNonInteger = l instanceof Float || l instanceof Double;
71      boolean rIsNonInteger = r instanceof Float || r instanceof Double;
72
73      if (lIsNonInteger && rIsNonInteger)
74        value (new Double (l.doubleValue () / r.doubleValue ()));
75      else if (lIsNonInteger || rIsNonInteger)
76      {
77        String[] parameters = {Util.getMessage ("EvaluationException.divide"),
78            left ().value ().getClass ().getName (),
79            right ().value ().getClass ().getName ()};
80        throw new EvaluationException (Util.getMessage ("EvaluationException.1", parameters));
81      }
82      else
83      {
84        BigInteger tmpL = (BigInteger)l,  tmpR = (BigInteger)r;
85        value (tmpL.divide (tmpR));
86      }
87      // <d52042> Allow evaluation over mixed operands.  Supplant code above.
88      /*
89      Number l = (Number)left ().evaluate ();
90      Number r = (Number)right ().evaluate ();
91
92      boolean lIsNonInteger = l instanceof Float || l instanceof Double;
93      boolean rIsNonInteger = r instanceof Float || r instanceof Double;
94
95      // Floating-point operands.
96      if (lIsNonInteger && rIsNonInteger)
97      {
98        value (new Double (l.doubleValue () / r.doubleValue ()));
99      }
100      // Integral operands.
101      else if (!(lIsNonInteger || rIsNonInteger))
102      {
103        BigInteger tmpL = (BigInteger)l,  tmpR = (BigInteger)r;
104        value (tmpL.divide (tmpR));
105      }
106      // Mixed operands: one operand is floating-point, the other is integral.
107      else
108      {
109        // Legal over floating-point types only.
110        if (type ().equals ("float") ||
111            type ().equals ("double"))
112        {
113          value (new Double (l.doubleValue () / r.doubleValue ()));
114        }
115        else
116        {
117          String[] parameters = {Util.getMessage ("EvaluationException.divide"),
118              left ().value ().getClass ().getName (),
119              right ().value ().getClass ().getName ()};
120        throw new EvaluationException (Util.getMessage ("EvaluationException.1", parameters));
121        }
122      }
123      */
124    }
125    catch (ClassCastException e)
126    {
127      String[] parameters = {Util.getMessage ("EvaluationException.divide"), left ().value ().getClass ().getName (), right ().value ().getClass ().getName ()};
128      throw new EvaluationException (Util.getMessage ("EvaluationException.1", parameters));
129    }
130    return value ();
131  } // evaluate
132} // class Divide
133