KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > exp > parser > ASTNegate


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

19
20
21 package org.apache.cayenne.exp.parser;
22
23 import java.io.PrintWriter JavaDoc;
24 import java.math.BigDecimal JavaDoc;
25
26 import org.apache.cayenne.exp.Expression;
27 import org.apache.cayenne.util.ConversionUtil;
28
29 /**
30  * "Negate" expression.
31  *
32  * @since 1.1
33  * @author Andrus Adamchik
34  */

35 public class ASTNegate extends SimpleNode {
36     ASTNegate(int id) {
37         super(id);
38     }
39
40     public ASTNegate() {
41         super(ExpressionParserTreeConstants.JJTNEGATE);
42     }
43
44     public ASTNegate(Object JavaDoc node) {
45         super(ExpressionParserTreeConstants.JJTNEGATE);
46         jjtAddChild(wrapChild(node), 0);
47     }
48
49     /**
50      * Creates a copy of this expression node, without copying children.
51      */

52     public Expression shallowCopy() {
53         return new ASTNegate(id);
54     }
55
56     protected Object JavaDoc evaluateNode(Object JavaDoc o) throws Exception JavaDoc {
57         int len = jjtGetNumChildren();
58         if (len == 0) {
59             return null;
60         }
61
62         BigDecimal JavaDoc result = ConversionUtil.toBigDecimal(evaluateChild(0, o));
63         return result != null ? result.negate() : null;
64     }
65
66     public void encodeAsString(PrintWriter JavaDoc pw) {
67         if ((children != null) && (children.length > 0)) {
68             pw.print("-");
69
70             SimpleNode child = (SimpleNode) children[0];
71
72             // don't call super - we have our own parenthesis policy
73
boolean useParen =
74                 parent != null
75                     && !((child instanceof ASTScalar) || (child instanceof ASTPath));
76             if (useParen) {
77                 pw.print("(");
78             }
79
80             child.encodeAsString(pw);
81
82             if (useParen) {
83                 pw.print(')');
84             }
85         }
86     }
87
88     protected String JavaDoc getExpressionOperator(int index) {
89         throw new UnsupportedOperationException JavaDoc(
90             "No operator for '" + ExpressionParserTreeConstants.jjtNodeName[id] + "'");
91     }
92
93     public int getType() {
94         return Expression.NEGATIVE;
95     }
96
97     public int getOperandCount() {
98         return 1;
99     }
100 }
101
Popular Tags