KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.cayenne.exp.parser;
21
22 import java.math.BigDecimal JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.cayenne.exp.Expression;
27 import org.apache.cayenne.util.ConversionUtil;
28
29 /**
30  * "Add" Expression.
31  *
32  * @author Andrus Adamchik
33  */

34 public class ASTAdd extends SimpleNode {
35     ASTAdd(int id) {
36         super(id);
37     }
38
39     public ASTAdd() {
40         super(ExpressionParserTreeConstants.JJTADD);
41     }
42
43     public ASTAdd(Object JavaDoc[] nodes) {
44         super(ExpressionParserTreeConstants.JJTADD);
45         int len = nodes.length;
46         for (int i = 0; i < len; i++) {
47             jjtAddChild(wrapChild(nodes[i]), i);
48         }
49     }
50
51     public ASTAdd(Collection JavaDoc nodes) {
52         super(ExpressionParserTreeConstants.JJTADD);
53         int len = nodes.size();
54         Iterator JavaDoc it = nodes.iterator();
55         for (int i = 0; i < len; i++) {
56             jjtAddChild(wrapChild(it.next()), i);
57         }
58     }
59
60     protected Object JavaDoc evaluateNode(Object JavaDoc o) throws Exception JavaDoc {
61         int len = jjtGetNumChildren();
62         if (len == 0) {
63             return null;
64         }
65
66         BigDecimal JavaDoc result = null;
67         for (int i = 0; i < len; i++) {
68             BigDecimal JavaDoc value = ConversionUtil.toBigDecimal(evaluateChild(i, o));
69
70             if (value == null) {
71                 return null;
72             }
73
74             result = (i == 0) ? value : result.add(value);
75         }
76
77         return result;
78     }
79
80     /**
81      * Creates a copy of this expression node, without copying children.
82      */

83     public Expression shallowCopy() {
84         return new ASTAdd(id);
85     }
86
87     protected String JavaDoc getExpressionOperator(int index) {
88         return "+";
89     }
90
91     public int getType() {
92         return Expression.ADD;
93     }
94
95     public void jjtClose() {
96         super.jjtClose();
97         flattenTree();
98     }
99 }
100
Popular Tags