001: package org.apache.ojb.jdo.jdoql;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * A binary expression (arithmetic or logical).
020: *
021: * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
022: */
023: public class BinaryExpression extends Expression {
024: public static final int OPERATOR_MULTIPLY = 0;
025: public static final int OPERATOR_DIVIDE = 1;
026: public static final int OPERATOR_PLUS = 2;
027: public static final int OPERATOR_MINUS = 3;
028: public static final int OPERATOR_LOWER = 4;
029: public static final int OPERATOR_GREATER = 5;
030: public static final int OPERATOR_LOWER_OR_EQUAL = 6;
031: public static final int OPERATOR_GREATER_OR_EQUAL = 7;
032: public static final int OPERATOR_EQUAL = 8;
033: public static final int OPERATOR_NOT_EQUAL = 9;
034: public static final int OPERATOR_BITWISE_AND = 10;
035: public static final int OPERATOR_BITWISE_XOR = 11;
036: public static final int OPERATOR_BITWISE_OR = 12;
037: public static final int OPERATOR_AND = 13;
038: public static final int OPERATOR_OR = 14;
039:
040: /** The left side expression */
041: private Expression _left;
042: /** The binary operator (one of the above constants) */
043: private int _operator;
044: /** The right side expression */
045: private Expression _right;
046: /** The type of this expression */
047: private Class _type;
048:
049: /**
050: * Creates a new binary expression object.
051: *
052: * @param left The left side of the expression
053: * @param operator The binary operator
054: * @param right The right side of the expression
055: */
056: public BinaryExpression(Expression left, int operator,
057: Expression right) {
058: _left = left;
059: _operator = operator;
060: _right = right;
061: }
062:
063: /**
064: * Returns the left side expression.
065: *
066: * @return The left expression
067: */
068: public Expression getLeftSide() {
069: return _left;
070: }
071:
072: /**
073: * Returns the operator.
074: *
075: * @return The operator
076: */
077: public int getOperator() {
078: return _operator;
079: }
080:
081: /**
082: * Returns the right side expression.
083: *
084: * @return The right expression
085: */
086: public Expression getRightSide() {
087: return _right;
088: }
089:
090: /* (non-Javadoc)
091: * @see org.apache.ojb.jdo.jdoql.Expression#replaceChild(org.apache.ojb.jdo.jdoql.Expression, org.apache.ojb.jdo.jdoql.Expression)
092: */
093: public void replaceChild(Expression oldChild, Expression newChild) {
094: if (oldChild == _left) {
095: _left.setParent(null);
096: _left = newChild;
097: _left.setParent(this );
098: } else if (oldChild == _right) {
099: _right.setParent(null);
100: _right = newChild;
101: _right.setParent(this );
102: }
103: }
104:
105: /* (non-Javadoc)
106: * @see org.apache.ojb.jdo.jdoql.Acceptor#accept(org.apache.ojb.jdo.jdoql.Visitor)
107: */
108: public void accept(Visitor visitor) {
109: visitor.visit(this );
110: }
111:
112: /* (non-Javadoc)
113: * @see java.lang.Object#toString()
114: */
115: public String toString() {
116: StringBuffer result = new StringBuffer();
117:
118: result.append("(");
119: result.append(_left.toString());
120: result.append(" ");
121: switch (_operator) {
122: case OPERATOR_MULTIPLY:
123: result.append("* ");
124: break;
125: case OPERATOR_DIVIDE:
126: result.append("/ ");
127: break;
128: case OPERATOR_PLUS:
129: result.append("+ ");
130: break;
131: case OPERATOR_MINUS:
132: result.append("- ");
133: break;
134: case OPERATOR_LOWER:
135: result.append("< ");
136: break;
137: case OPERATOR_GREATER:
138: result.append("> ");
139: break;
140: case OPERATOR_LOWER_OR_EQUAL:
141: result.append("<= ");
142: break;
143: case OPERATOR_GREATER_OR_EQUAL:
144: result.append(">= ");
145: break;
146: case OPERATOR_EQUAL:
147: result.append("== ");
148: break;
149: case OPERATOR_NOT_EQUAL:
150: result.append("!= ");
151: break;
152: case OPERATOR_BITWISE_AND:
153: result.append("& ");
154: break;
155: case OPERATOR_BITWISE_XOR:
156: result.append("^ ");
157: break;
158: case OPERATOR_BITWISE_OR:
159: result.append("| ");
160: break;
161: case OPERATOR_AND:
162: result.append("&& ");
163: break;
164: case OPERATOR_OR:
165: result.append("|| ");
166: break;
167: }
168: result.append(_right.toString());
169: result.append(")");
170: return result.toString();
171: }
172:
173: /**
174: * Sets the type of this binary expression.
175: *
176: * @param type The type
177: */
178: public void setType(Class type) {
179: _type = type;
180: }
181:
182: /* (non-Javadoc)
183: * @see org.apache.ojb.jdo.jdoql.Expression#getType()
184: */
185: public Class getType() {
186: return _type;
187: }
188: }
|