KickJava   Java API By Example, From Geeks To Geeks.

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


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 unary expression (arithmetic, logical, or a cast).
20  *
21  * @author <a HREF="mailto:tomdz@apache.org">Thomas Dudziak</a>
22  */

23 public class UnaryExpression extends Expression
24 {
25     public static final int OPERATOR_MINUS = 0;
26     public static final int OPERATOR_PLUS = 1;
27     public static final int OPERATOR_BITWISE_COMPLEMENT = 2;
28     public static final int OPERATOR_NOT = 3;
29     public static final int OPERATOR_CAST = 4;
30
31     /** The unary operator (one of the above constants) */
32     private int _operator;
33     /** The inner expression */
34     private Expression _inner;
35     /** The type that the inner expression is cast to */
36     private Type _castType;
37     /** The type of this expression */
38     private Class JavaDoc _type;
39
40     /**
41      * Creates a new unary expression object.
42      *
43      * @param operator The unary operator
44      * @param inner The inner expression
45      */

46     public UnaryExpression(int operator, Expression inner)
47     {
48         _operator = operator;
49         _inner = inner;
50     }
51
52     /**
53      * Creates a new cast expression object.
54      *
55      * @param castType The cast-to type
56      * @param inner The inner expression
57      */

58     public UnaryExpression(Type castType, Expression inner)
59     {
60         _operator = OPERATOR_CAST;
61         _castType = castType;
62         _inner = inner;
63     }
64
65     /**
66      * Returns the operator.
67      *
68      * @return The operator
69      */

70     public int getOperator()
71     {
72         return _operator;
73     }
74
75     /**
76      * Returns the inner expression.
77      *
78      * @return The inner expression
79      */

80     public Expression getInnerExpression()
81     {
82         return _inner;
83     }
84
85     /* (non-Javadoc)
86      * @see org.apache.ojb.jdo.jdoql.Expression#replaceChild(org.apache.ojb.jdo.jdoql.Expression, org.apache.ojb.jdo.jdoql.Expression)
87      */

88     public void replaceChild(Expression oldChild, Expression newChild)
89     {
90         _inner.setParent(null);
91         _inner = newChild;
92         _inner.setParent(this);
93     }
94
95     /**
96      * Returns the target type of this cast expression.
97      *
98      * @return The type name of the cast type, or <code>null</code> if this is no
99      * cast expression
100      */

101     public Type getCastType()
102     {
103         return _castType;
104     }
105
106     /* (non-Javadoc)
107      * @see org.apache.ojb.jdo.jdoql.Acceptor#accept(org.apache.ojb.jdo.jdoql.Visitor)
108      */

109     public void accept(Visitor visitor)
110     {
111         visitor.visit(this);
112     }
113
114     /* (non-Javadoc)
115      * @see java.lang.Object#toString()
116      */

117     public String JavaDoc toString()
118     {
119         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
120
121         result.append("(");
122         switch (_operator)
123         {
124             case OPERATOR_PLUS :
125                 result.append("+");
126                 break;
127             case OPERATOR_MINUS :
128                 result.append("-");
129                 break;
130             case OPERATOR_BITWISE_COMPLEMENT :
131                 result.append("~");
132                 break;
133             case OPERATOR_NOT :
134                 result.append("!");
135                 break;
136             case OPERATOR_CAST :
137                 result.append("(");
138                 result.append(_castType.toString());
139                 result.append(")");
140                 break;
141         }
142         result.append(_inner.toString());
143         result.append(")");
144         return result.toString();
145     }
146
147     /**
148      * Sets the type of this unary expression.
149      *
150      * @param type The type
151      */

152     public void setType(Class JavaDoc type)
153     {
154         _type = type;
155     }
156
157     /* (non-Javadoc)
158      * @see org.apache.ojb.jdo.jdoql.Expression#getType()
159      */

160     public Class JavaDoc getType()
161     {
162         return _type;
163     }
164 }
165
Popular Tags