KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > parsing > ModNode


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.parsing;
23
24 /**
25  * INTERNAL
26  * <p><b>Purpose</b>: Represent a MOD
27  * <p><b>Responsibilities</b>:<ul>
28  * <li> Answer the correct expression for a MOD
29  * <li> Maintain the parts of a MOD statement
30  *
31  * e.g.
32  * SELECT ... FROM ... WHERE MOD(emp.salary, 2) > 1000
33  * </ul>
34  * @author Jon Driscoll and Joel Lucuik
35  * @since TopLink 4.0
36  */

37 import oracle.toplink.essentials.expressions.*;
38 import oracle.toplink.essentials.exceptions.EJBQLException;
39
40 public class ModNode extends ArithmeticFunctionNode {
41
42     private Node denominator = null;
43
44     /**
45      * INTERNAL
46      * Validate node and calculate its type.
47      */

48     public void validate(ParseTreeContext context) {
49         TypeHelper typeHelper = context.getTypeHelper();
50         if (left != null) {
51             left.validate(context);
52             left.validateParameter(context, typeHelper.getIntType());
53
54             Object JavaDoc type = left.getType();
55             if (!typeHelper.isIntegralType(type))
56                 throw EJBQLException.invalidFunctionArgument("MOD", left.getAsString(), "integral type");
57         }
58
59         if (denominator != null) {
60             denominator.validate(context);
61             denominator.validateParameter(context, typeHelper.getIntType());
62
63             Object JavaDoc denominatorType = denominator.getType();
64             if (!typeHelper.isIntegralType(denominatorType))
65                 throw EJBQLException.invalidFunctionArgument("MOD", denominator.getAsString(), "integral type");
66         }
67
68         setType(typeHelper.getIntType());
69     }
70     
71     /** */
72     public Expression generateExpression(GenerationContext context) {
73         Expression whereClause;
74         Integer JavaDoc i = new Integer JavaDoc(((LiteralNode)getDenominator()).getLiteral().toString());
75
76         // whereClause = ExpressionMath.mod(getLeft().generateExpression(context), i.intValue());
77
if (getLeft().isLiteralNode()) {
78             ExpressionBuilder builder = new ExpressionBuilder();
79             Integer JavaDoc leftInt = new Integer JavaDoc(((IntegerLiteralNode)getLeft()).getLiteral().toString());
80             whereClause = ExpressionMath.mod(builder.value(leftInt.intValue()), builder.value(i.intValue()));
81         } else {
82             whereClause = ExpressionMath.mod(getLeft().generateExpression(context), i.intValue());
83         }
84         return whereClause;
85     }
86
87     // Accessors
88
public Node getDenominator() {
89         return denominator;
90     }
91
92     public void setDenominator(Node denominator) {
93         this.denominator = denominator;
94     }
95 }
96
Popular Tags