KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractFunction;
19 import org.outerj.daisy.query.model.QValueType;
20 import org.outerj.daisy.query.model.SqlGenerationContext;
21 import org.outerj.daisy.query.QueryContext;
22 import org.outerj.daisy.repository.query.QueryException;
23 import org.outerj.daisy.repository.query.EvaluationContext;
24 import org.outerj.daisy.repository.Document;
25 import org.outerj.daisy.repository.Version;
26
27 import java.util.Locale JavaDoc;
28 import java.math.BigDecimal JavaDoc;
29
30 public abstract class AbstractUnaryMathFunction extends AbstractFunction {
31     public void prepare(QueryContext context) throws QueryException {
32         super.prepare(context);
33         if (params.size() != 1)
34             throw new QueryException("\"" + getFunctionName() + "\" requires exactly one parameter.");
35
36         if (!AbstractBinaryMathFunction.isNumeric(getParam(0)))
37             throw new QueryException("\"" + getFunctionName() + "\" can only be performed on numeric expressions, which this is not: " + getParam(0).getExpression());
38     }
39
40     public Object JavaDoc evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException {
41         BigDecimal JavaDoc value = AbstractBinaryMathFunction.toBigDecimal(getParam(0).evaluate(QValueType.DECIMAL, evaluationContext));
42         return evaluate(value);
43     }
44
45     public Object JavaDoc evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
46         BigDecimal JavaDoc value = AbstractBinaryMathFunction.toBigDecimal(getParam(0).evaluate(QValueType.DECIMAL, document, version, evaluationContext));
47         return evaluate(value);
48     }
49
50     private Object JavaDoc evaluate(BigDecimal JavaDoc value) {
51         return value == null ? null : performCalculation(value);
52     }
53
54     protected abstract Object JavaDoc performCalculation(BigDecimal JavaDoc value);
55     protected abstract String JavaDoc[] getSqlFunction(SqlGenerationContext context);
56
57     public void generateSqlValueExpr(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
58         String JavaDoc[] sqlFunction = getSqlFunction(context);
59         sql.append(' ').append(sqlFunction[0]);
60         super.generateSqlValueExpr(sql, context);
61         sql.append(sqlFunction[1]).append(' ');
62     }
63
64     public QValueType getValueType() {
65         return QValueType.DECIMAL;
66     }
67
68     public String JavaDoc getTitle(Locale JavaDoc locale) {
69         return getExpression();
70     }
71
72     public QValueType getOutputValueType() {
73         return QValueType.DECIMAL;
74     }
75
76     public Object JavaDoc getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
77         return evaluate(null, document, version, evaluationContext);
78     }
79 }
80
Popular Tags