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 unary expression (arithmetic, logical, or a cast).
020: *
021: * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
022: */
023: public class UnaryExpression extends Expression {
024: public static final int OPERATOR_MINUS = 0;
025: public static final int OPERATOR_PLUS = 1;
026: public static final int OPERATOR_BITWISE_COMPLEMENT = 2;
027: public static final int OPERATOR_NOT = 3;
028: public static final int OPERATOR_CAST = 4;
029:
030: /** The unary operator (one of the above constants) */
031: private int _operator;
032: /** The inner expression */
033: private Expression _inner;
034: /** The type that the inner expression is cast to */
035: private Type _castType;
036: /** The type of this expression */
037: private Class _type;
038:
039: /**
040: * Creates a new unary expression object.
041: *
042: * @param operator The unary operator
043: * @param inner The inner expression
044: */
045: public UnaryExpression(int operator, Expression inner) {
046: _operator = operator;
047: _inner = inner;
048: }
049:
050: /**
051: * Creates a new cast expression object.
052: *
053: * @param castType The cast-to type
054: * @param inner The inner expression
055: */
056: public UnaryExpression(Type castType, Expression inner) {
057: _operator = OPERATOR_CAST;
058: _castType = castType;
059: _inner = inner;
060: }
061:
062: /**
063: * Returns the operator.
064: *
065: * @return The operator
066: */
067: public int getOperator() {
068: return _operator;
069: }
070:
071: /**
072: * Returns the inner expression.
073: *
074: * @return The inner expression
075: */
076: public Expression getInnerExpression() {
077: return _inner;
078: }
079:
080: /* (non-Javadoc)
081: * @see org.apache.ojb.jdo.jdoql.Expression#replaceChild(org.apache.ojb.jdo.jdoql.Expression, org.apache.ojb.jdo.jdoql.Expression)
082: */
083: public void replaceChild(Expression oldChild, Expression newChild) {
084: _inner.setParent(null);
085: _inner = newChild;
086: _inner.setParent(this );
087: }
088:
089: /**
090: * Returns the target type of this cast expression.
091: *
092: * @return The type name of the cast type, or <code>null</code> if this is no
093: * cast expression
094: */
095: public Type getCastType() {
096: return _castType;
097: }
098:
099: /* (non-Javadoc)
100: * @see org.apache.ojb.jdo.jdoql.Acceptor#accept(org.apache.ojb.jdo.jdoql.Visitor)
101: */
102: public void accept(Visitor visitor) {
103: visitor.visit(this );
104: }
105:
106: /* (non-Javadoc)
107: * @see java.lang.Object#toString()
108: */
109: public String toString() {
110: StringBuffer result = new StringBuffer();
111:
112: result.append("(");
113: switch (_operator) {
114: case OPERATOR_PLUS:
115: result.append("+");
116: break;
117: case OPERATOR_MINUS:
118: result.append("-");
119: break;
120: case OPERATOR_BITWISE_COMPLEMENT:
121: result.append("~");
122: break;
123: case OPERATOR_NOT:
124: result.append("!");
125: break;
126: case OPERATOR_CAST:
127: result.append("(");
128: result.append(_castType.toString());
129: result.append(")");
130: break;
131: }
132: result.append(_inner.toString());
133: result.append(")");
134: return result.toString();
135: }
136:
137: /**
138: * Sets the type of this unary expression.
139: *
140: * @param type The type
141: */
142: public void setType(Class type) {
143: _type = type;
144: }
145:
146: /* (non-Javadoc)
147: * @see org.apache.ojb.jdo.jdoql.Expression#getType()
148: */
149: public Class getType() {
150: return _type;
151: }
152: }
|