KickJava   Java API By Example, From Geeks To Geeks.

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


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 the numeric comparison operation: gt
42  */

43 public class GtExpr extends AbstractBooleanExpr {
44   private final Expr _left;
45   private final Expr _right;
46
47   /**
48    * Creates a comparison expression
49    *
50    * @param op the lexical code for the operation
51    * @param left the left subexpression
52    * @param right the right subexpression
53    */

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

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

74   @Override JavaDoc
75   public boolean evalBoolean(ELContext env)
76     throws ELException
77   {
78     Object JavaDoc aObj = _left.getValue(env);
79     Object JavaDoc bObj = _right.getValue(env);
80
81     if (aObj == null || bObj == null)
82       return false;
83
84     Class JavaDoc aType = aObj.getClass();
85     Class JavaDoc bType = bObj.getClass();
86     
87     if (aObj instanceof BigDecimal JavaDoc || bObj instanceof BigDecimal JavaDoc) {
88       BigDecimal JavaDoc a = toBigDecimal(aObj, env);
89       BigDecimal JavaDoc b = toBigDecimal(bObj, env);
90
91       return a.compareTo(b) > 0;
92     }
93     
94     if (aType == Double JavaDoc.class || aType == Float JavaDoc.class ||
95         bType == Double JavaDoc.class || bType == Float JavaDoc.class) {
96       double a = toDouble(aObj, env);
97       double b = toDouble(bObj, env);
98
99       return a > b;
100     }
101     
102     if (aType == BigInteger JavaDoc.class || bType == BigInteger JavaDoc.class) {
103       BigInteger JavaDoc a = toBigInteger(aObj, env);
104       BigInteger JavaDoc b = toBigInteger(bObj, env);
105
106       return a.compareTo(b) > 0;
107     }
108     
109     if (aObj instanceof Number JavaDoc || bObj instanceof Number JavaDoc) {
110       long a = toLong(aObj, env);
111       long b = toLong(bObj, env);
112
113       return a > b;
114     }
115
116     if (aObj instanceof String JavaDoc || bObj instanceof String JavaDoc) {
117       String JavaDoc a = toString(aObj, env);
118       String JavaDoc b = toString(bObj, env);
119
120       return a.compareTo(b) > 0;
121     }
122
123     if (aObj instanceof Comparable JavaDoc) {
124       int cmp = ((Comparable JavaDoc) aObj).compareTo(bObj);
125
126       return cmp > 0;
127     }
128
129     if (bObj instanceof Comparable JavaDoc) {
130       int cmp = ((Comparable JavaDoc) bObj).compareTo(aObj);
131
132       return cmp < 0;
133     }
134
135     ELException e = new ELException(L.l("can't compare {0} and {1}.",
136                                         aObj, bObj));
137
138     error(e, env);
139
140     return false;
141   }
142
143   /**
144    * Prints the code to create an LongLiteral.
145    */

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

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

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