KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdo > jdoql > BinaryExpression


1 package org.apache.ojb.jdo.jdoql;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /**
19  * A binary expression (arithmetic or logical).
20  *
21  * @author <a HREF="mailto:tomdz@apache.org">Thomas Dudziak</a>
22  */

23 public class BinaryExpression extends Expression
24 {
25     public static final int OPERATOR_MULTIPLY = 0;
26     public static final int OPERATOR_DIVIDE = 1;
27     public static final int OPERATOR_PLUS = 2;
28     public static final int OPERATOR_MINUS = 3;
29     public static final int OPERATOR_LOWER = 4;
30     public static final int OPERATOR_GREATER = 5;
31     public static final int OPERATOR_LOWER_OR_EQUAL = 6;
32     public static final int OPERATOR_GREATER_OR_EQUAL = 7;
33     public static final int OPERATOR_EQUAL = 8;
34     public static final int OPERATOR_NOT_EQUAL = 9;
35     public static final int OPERATOR_BITWISE_AND = 10;
36     public static final int OPERATOR_BITWISE_XOR = 11;
37     public static final int OPERATOR_BITWISE_OR = 12;
38     public static final int OPERATOR_AND = 13;
39     public static final int OPERATOR_OR = 14;
40
41     /** The left side expression */
42     private Expression _left;
43     /** The binary operator (one of the above constants) */
44     private int _operator;
45     /** The right side expression */
46     private Expression _right;
47     /** The type of this expression */
48     private Class JavaDoc _type;
49
50     /**
51      * Creates a new binary expression object.
52      *
53      * @param left The left side of the expression
54      * @param operator The binary operator
55      * @param right The right side of the expression
56      */

57     public BinaryExpression(Expression left, int operator, Expression right)
58     {
59         _left = left;
60         _operator = operator;
61         _right = right;
62     }
63
64     /**
65      * Returns the left side expression.
66      *
67      * @return The left expression
68      */

69     public Expression getLeftSide()
70     {
71         return _left;
72     }
73
74     /**
75      * Returns the operator.
76      *
77      * @return The operator
78      */

79     public int getOperator()
80     {
81         return _operator;
82     }
83
84     /**
85      * Returns the right side expression.
86      *
87      * @return The right expression
88      */

89     public Expression getRightSide()
90     {
91         return _right;
92     }
93
94     /* (non-Javadoc)
95      * @see org.apache.ojb.jdo.jdoql.Expression#replaceChild(org.apache.ojb.jdo.jdoql.Expression, org.apache.ojb.jdo.jdoql.Expression)
96      */

97     public void replaceChild(Expression oldChild, Expression newChild)
98     {
99         if (oldChild == _left)
100         {
101             _left.setParent(null);
102             _left = newChild;
103             _left.setParent(this);
104         }
105         else if (oldChild == _right)
106         {
107             _right.setParent(null);
108             _right = newChild;
109             _right.setParent(this);
110         }
111     }
112
113     /* (non-Javadoc)
114      * @see org.apache.ojb.jdo.jdoql.Acceptor#accept(org.apache.ojb.jdo.jdoql.Visitor)
115      */

116     public void accept(Visitor visitor)
117     {
118         visitor.visit(this);
119     }
120
121     /* (non-Javadoc)
122      * @see java.lang.Object#toString()
123      */

124     public String JavaDoc toString()
125     {
126         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
127
128         result.append("(");
129         result.append(_left.toString());
130         result.append(" ");
131         switch (_operator)
132         {
133             case OPERATOR_MULTIPLY :
134                 result.append("* ");
135                 break;
136             case OPERATOR_DIVIDE :
137                 result.append("/ ");
138                 break;
139             case OPERATOR_PLUS :
140                 result.append("+ ");
141                 break;
142             case OPERATOR_MINUS :
143                 result.append("- ");
144                 break;
145             case OPERATOR_LOWER :
146                 result.append("< ");
147                 break;
148             case OPERATOR_GREATER :
149                 result.append("> ");
150                 break;
151             case OPERATOR_LOWER_OR_EQUAL :
152                 result.append("<= ");
153                 break;
154             case OPERATOR_GREATER_OR_EQUAL :
155                 result.append(">= ");
156                 break;
157             case OPERATOR_EQUAL :
158                 result.append("== ");
159                 break;
160             case OPERATOR_NOT_EQUAL :
161                 result.append("!= ");
162                 break;
163             case OPERATOR_BITWISE_AND :
164                 result.append("& ");
165                 break;
166             case OPERATOR_BITWISE_XOR :
167                 result.append("^ ");
168                 break;
169             case OPERATOR_BITWISE_OR :
170                 result.append("| ");
171                 break;
172             case OPERATOR_AND :
173                 result.append("&& ");
174                 break;
175             case OPERATOR_OR :
176                 result.append("|| ");
177                 break;
178         }
179         result.append(_right.toString());
180         result.append(")");
181         return result.toString();
182     }
183
184     /**
185      * Sets the type of this binary expression.
186      *
187      * @param type The type
188      */

189     public void setType(Class JavaDoc type)
190     {
191         _type = type;
192     }
193
194     /* (non-Javadoc)
195      * @see org.apache.ojb.jdo.jdoql.Expression#getType()
196      */

197     public Class JavaDoc getType()
198     {
199         return _type;
200     }
201 }
202
Popular Tags