KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.logging.Level JavaDoc;
38
39 /**
40  * Represents a binary boolean expression.
41  */

42 public class BooleanExpr extends AbstractBooleanExpr {
43   private int _op;
44   private Expr _left;
45   private Expr _right;
46
47   /**
48    * Constructs a new Boolean expression.
49    *
50    * @param op the lexeme code for the boolean operation.
51    * @param left the left expression
52    * @param right the right expression
53    */

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

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

77   @Override JavaDoc
78   public boolean evalBoolean(ELContext env)
79     throws ELException
80   {
81     if (_op == AND)
82       return _left.evalBoolean(env) && _right.evalBoolean(env);
83     else if (_op == OR)
84       return _left.evalBoolean(env) || _right.evalBoolean(env);
85
86     ELException e = new ELException(L.l("can't compare."));
87     log.log(Level.FINE, e.getMessage(), e);
88
89     return false;
90   }
91
92   /**
93    * Prints the Java code to recreate a BooleanExpr
94    */

95   @Override JavaDoc
96   public void printCreate(WriteStream os)
97     throws IOException JavaDoc
98   {
99     os.print("new com.caucho.el.BooleanExpr(");
100     os.print(_op + ", ");
101     _left.printCreate(os);
102     os.print(", ");
103     _right.printCreate(os);
104     os.print(")");
105   }
106
107   /**
108    * Returns true for equal strings.
109    */

110   public boolean equals(Object JavaDoc o)
111   {
112     if (! (o instanceof BooleanExpr))
113       return false;
114
115     BooleanExpr expr = (BooleanExpr) o;
116
117     return (_op == expr._op &&
118             _left.equals(expr._left) &&
119             _right.equals(expr._right));
120   }
121
122   /**
123    * Returns a readable representation of the expr.
124    */

125   public String JavaDoc toString()
126   {
127     String JavaDoc op;
128
129     switch (_op) {
130     case OR:
131       op = " or ";
132       break;
133     case AND:
134       op = " and ";
135       break;
136     default:
137       op = " unknown(" + _op + ") ";
138       break;
139     }
140         
141     return "(" + _left + op + _right + ")";
142   }
143 }
144
Popular Tags