KickJava   Java API By Example, From Geeks To Geeks.

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


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 mod numeric operation
42  */

43 public class ModExpr 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 ModExpr(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     || isDouble(aObj)
84     || bObj instanceof BigDecimal JavaDoc
85     || isDouble(bObj)) {
86       double a = toDouble(aObj, env);
87       double b = toDouble(bObj, env);
88       
89       return new Double JavaDoc(a % b);
90     }
91     else if (aObj instanceof BigInteger JavaDoc
92          || bObj instanceof BigInteger JavaDoc) {
93       BigInteger JavaDoc a = toBigInteger(aObj, env);
94       BigInteger JavaDoc b = toBigInteger(bObj, env);
95       
96       return a.remainder(b);
97     }
98     else if (aObj == null && bObj == null)
99       return new Long JavaDoc(0);
100     else {
101       long a = toLong(aObj, env);
102       long b = toLong(bObj, env);
103
104       return new Long JavaDoc(a % b);
105     }
106   }
107   
108   /**
109    * Evaluate the expression as a long
110    *
111    * @param env the variable environment
112    *
113    * @return the result as an long
114    */

115   @Override JavaDoc
116   public long evalLong(ELContext env)
117     throws ELException
118   {
119     long a = _left.evalLong(env);
120     long b = _right.evalLong(env);
121
122     return a % b;
123   }
124   
125   /**
126    * Evaluate the expression as a double
127    *
128    * @param env the variable environment
129    *
130    * @return the result as an double
131    */

132   @Override JavaDoc
133   public double evalDouble(ELContext env)
134     throws ELException
135   {
136     double a = _left.evalDouble(env);
137     double b = _right.evalDouble(env);
138
139     return a % b;
140   }
141
142   /**
143    * Prints the Java code to recreate an LongLiteral.
144    *
145    * @param os the output stream to the *.java file
146    */

147   @Override JavaDoc
148   public void printCreate(WriteStream os)
149     throws IOException JavaDoc
150   {
151     os.print("new com.caucho.el.ModExpr(");
152     _left.printCreate(os);
153     os.print(", ");
154     _right.printCreate(os);
155     os.print(")");
156   }
157
158   /**
159    * Returns true for equal strings.
160    */

161   public boolean equals(Object JavaDoc o)
162   {
163     if (! (o instanceof ModExpr))
164       return false;
165
166     ModExpr expr = (ModExpr) o;
167
168     return (_left.equals(expr._left) &&
169             _right.equals(expr._right));
170   }
171   
172   /**
173    * Returns a readable representation of the expr.
174    */

175   public String JavaDoc toString()
176   {
177     return "(" + _left + " + " + _right + ")";
178   }
179 }
180
Popular Tags