KickJava   Java API By Example, From Geeks To Geeks.

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


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: ne
42  */

43 public class NeExpr 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 NeExpr(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 == bObj)
82       return false;
83
84     if (aObj == null || bObj == null)
85       return true;
86
87     Class JavaDoc aType = aObj.getClass();
88     Class JavaDoc bType = bObj.getClass();
89     
90     if (aObj instanceof BigDecimal JavaDoc || bObj instanceof BigDecimal JavaDoc) {
91       BigDecimal JavaDoc a = toBigDecimal(aObj, env);
92       BigDecimal JavaDoc b = toBigDecimal(bObj, env);
93
94       return ! a.equals(b);
95     }
96     
97     if (aType == Double JavaDoc.class || aType == Float JavaDoc.class ||
98         bType == Double JavaDoc.class || bType == Float JavaDoc.class) {
99       double a = toDouble(aObj, env);
100       double b = toDouble(bObj, env);
101
102       return a != b;
103     }
104     
105     if (aType == BigInteger JavaDoc.class || bType == BigInteger JavaDoc.class) {
106       BigInteger JavaDoc a = toBigInteger(aObj, env);
107       BigInteger JavaDoc b = toBigInteger(bObj, env);
108
109       return ! a.equals(b);
110     }
111     
112     if (aObj instanceof Number JavaDoc || bObj instanceof Number JavaDoc) {
113       long a = toLong(aObj, env);
114       long b = toLong(bObj, env);
115
116       return a != b;
117     }
118
119     if (aType == Boolean JavaDoc.class || bType == Boolean JavaDoc.class) {
120       boolean a = toBoolean(aObj, env);
121       boolean b = toBoolean(bObj, env);
122
123       return a != b;
124     }
125
126     // XXX: enum
127

128     if (aObj instanceof String JavaDoc || bObj instanceof String JavaDoc) {
129       String JavaDoc a = toString(aObj, env);
130       String JavaDoc b = toString(bObj, env);
131
132       return ! a.equals(b);
133     }
134
135     return ! aObj.equals(bObj);
136   }
137
138   /**
139    * Prints the code to create an LongLiteral.
140    */

141   @Override JavaDoc
142   public void printCreate(WriteStream os)
143     throws IOException JavaDoc
144   {
145     os.print("new com.caucho.el.NeExpr(");
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 NeExpr))
158       return false;
159
160     NeExpr expr = (NeExpr) 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 + " ne " + _right + ")";
172   }
173 }
174
Popular Tags