KickJava   Java API By Example, From Geeks To Geeks.

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


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  * "Divide" expression.
31  *
32  * @since 1.1
33  * @author Andrus Adamchik
34  */

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

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