KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > DivExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.el;
31
32 import com.caucho.vfs.WriteStream;
33
34 import javax.el.ELContext;
35 import javax.el.ELException;
36 import java.io.IOException JavaDoc;
37 import java.math.BigDecimal JavaDoc;
38 import java.math.BigInteger JavaDoc;
39
40 /**
41  * Represents a binary divtiplication numeric operation
42  */

43 public class DivExpr extends Expr {
44   private final Expr _left;
45   private final Expr _right;
46   
47   /**
48    * Creates the multiplication expression.
49    *
50    * @param left the left sub-expression
51    * @param right the right sub-expression
52    */

53   public DivExpr(Expr left, Expr right)
54   {
55     _left = left;
56     _right = right;
57   }
58
59   /**
60    * Returns true if this is a constant expression.
61    */

62   @Override JavaDoc
63   public boolean isConstant()
64   {
65     return _left.isConstant() && _right.isConstant();
66   }
67   
68   /**
69    * Evaluate the expression as an object.
70    *
71    * @param env the variable environment
72    *
73    * @return the result as an object
74    */

75   @Override JavaDoc
76   public Object JavaDoc getValue(ELContext env)
77     throws ELException
78   {
79     Object JavaDoc aObj = _left.getValue(env);
80     Object JavaDoc bObj = _right.getValue(env);
81
82     if (aObj instanceof BigDecimal JavaDoc
83     || bObj instanceof BigDecimal JavaDoc
84     || aObj instanceof BigInteger JavaDoc
85     || bObj instanceof BigInteger JavaDoc) {
86       BigDecimal JavaDoc a = toBigDecimal(aObj, env);
87       BigDecimal JavaDoc b = toBigDecimal(bObj, env);
88       
89       return a.divide(b, BigDecimal.ROUND_HALF_UP);
90     }
91     else if (aObj == null && bObj == null)
92       return new Long JavaDoc(0);
93     else {
94       double a = toDouble(aObj, env);
95       double b = toDouble(bObj, env);
96       double dValue = a / b;
97
98       return new Double JavaDoc(dValue);
99     }
100   }
101   
102   /**
103    * Evaluate the expression as a long
104    *
105    * @param env the variable environment
106    *
107    * @return the result as an long
108    */

109   @Override JavaDoc
110   public long evalLong(ELContext env)
111     throws ELException
112   {
113     double a = _left.evalDouble(env);
114     double b = _right.evalDouble(env);
115
116     return (long) (a / b + 0.5);
117   }
118   
119   /**
120    * Evaluate the expression as a double
121    *
122    * @param env the variable environment
123    *
124    * @return the result as an double
125    */

126   @Override JavaDoc
127   public double evalDouble(ELContext env)
128     throws ELException
129   {
130     double a = _left.evalDouble(env);
131     double b = _right.evalDouble(env);
132
133     return a / b;
134   }
135
136   /**
137    * Prints the Java code to recreate an LongLiteral.
138    *
139    * @param os the output stream to the *.java file
140    */

141   @Override JavaDoc
142   public void printCreate(WriteStream os)
143     throws IOException JavaDoc
144   {
145     os.print("new com.caucho.el.DivExpr(");
146     _left.printCreate(os);
147     os.print(", ");
148     _right.printCreate(os);
149     os.print(")");
150   }
151
152   /**
153    * Returns true for equal strings.
154    */

155   public boolean equals(Object JavaDoc o)
156   {
157     if (! (o instanceof DivExpr))
158       return false;
159
160     DivExpr expr = (DivExpr) o;
161
162     return (_left.equals(expr._left) &&
163             _right.equals(expr._right));
164   }
165   
166   /**
167    * Returns a readable representation of the expr.
168    */

169   public String JavaDoc toString()
170   {
171     return "(" + _left + " + " + _right + ")";
172   }
173 }
174
Popular Tags