KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigDecimal JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.cayenne.exp.Expression;
28 import org.apache.cayenne.util.ConversionUtil;
29
30 /**
31  * "Multiply" expression.
32  *
33  * @since 1.1
34  * @author Andrus Adamchik
35  */

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

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