KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > query > model > functions > ModFunction


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.query.model.functions;
17
18 import org.outerj.daisy.query.model.SqlGenerationContext;
19 import org.outerj.daisy.repository.query.QueryException;
20
21 import java.math.BigDecimal JavaDoc;
22
23 public class ModFunction extends AbstractBinaryMathFunction {
24     public static final String JavaDoc NAME = "Mod";
25
26     public String JavaDoc getFunctionName() {
27         return NAME;
28     }
29
30     protected Object JavaDoc performCalculation(BigDecimal JavaDoc value1, BigDecimal JavaDoc value2) {
31         // TODO once we move to Java 1.5, use the build-in remainder function
32
// this is just a quick hack to have something
33
value1 = value1.abs();
34         value2 = value2.abs();
35         BigDecimal JavaDoc quotient = value1.divide(value2, BigDecimal.ROUND_HALF_DOWN);
36         quotient = quotient.setScale(0, BigDecimal.ROUND_DOWN);
37         return value1.subtract(quotient.multiply(value2));
38     }
39
40     public void generateSqlValueExpr(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
41         sql.append(" MOD( ");
42         getParam(0).generateSqlValueExpr(sql, context);
43         sql.append(", ");
44         getParam(1).generateSqlValueExpr(sql, context);
45         sql.append(") ");
46     }
47
48     public String JavaDoc getExpression() {
49         return getFunctionName() + "(" + getParam(0).getExpression() + ", " + getParam(1).getExpression() + ")";
50     }
51
52     protected String JavaDoc getMathSymbol() {
53         throw new RuntimeException JavaDoc("This method should not be called.");
54     }
55 }
56
57
Popular Tags