KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > fun > FormatFunDef


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/FormatFunDef.java#2 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2006-2006 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.olap.fun;
11
12 import mondrian.olap.FunDef;
13 import mondrian.olap.Exp;
14 import mondrian.olap.Literal;
15 import mondrian.olap.Evaluator;
16 import mondrian.calc.Calc;
17 import mondrian.calc.ExpCompiler;
18 import mondrian.calc.StringCalc;
19 import mondrian.calc.impl.AbstractStringCalc;
20 import mondrian.mdx.ResolvedFunCall;
21 import mondrian.util.Format;
22
23 import java.util.Locale JavaDoc;
24
25 /**
26  * Definition of the <code>Format</code> MDX function.
27  *
28  * @author jhyde
29  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/FormatFunDef.java#2 $
30  * @since Mar 23, 2006
31  */

32 class FormatFunDef extends FunDefBase {
33     static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver(
34             "Format",
35             "Format(<Numeric Expression>, <String Expression>)",
36             "Formats a number to string.",
37             new String JavaDoc[] { "fSmS", "fSnS" },
38             FormatFunDef.class);
39
40     public FormatFunDef(FunDef dummyFunDef) {
41         super(dummyFunDef);
42     }
43
44     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
45         final Exp[] args = call.getArgs();
46         final Calc calc = compiler.compileScalar(call.getArg(0), true);
47         final Locale JavaDoc locale = compiler.getEvaluator().getConnectionLocale();
48         if (args[1] instanceof Literal) {
49             // Constant string expression: optimize by
50
// compiling format string.
51
String JavaDoc formatString = (String JavaDoc) ((Literal) args[1]).getValue();
52             final Format format = new Format(formatString, locale);
53             return new AbstractStringCalc(call, new Calc[] {calc}) {
54                 public String JavaDoc evaluateString(Evaluator evaluator) {
55                     final Object JavaDoc o = calc.evaluate(evaluator);
56                     return format.format(o);
57                 }
58             };
59         } else {
60             // Variable string expression
61
final StringCalc stringCalc =
62                     compiler.compileString(call.getArg(1));
63             return new AbstractStringCalc(call, new Calc[] {calc, stringCalc}) {
64                 public String JavaDoc evaluateString(Evaluator evaluator) {
65                     final Object JavaDoc o = calc.evaluate(evaluator);
66                     final String JavaDoc formatString =
67                             stringCalc.evaluateString(evaluator);
68                     final Format format =
69                             new Format(formatString, locale);
70                     return format.format(o);
71                 }
72             };
73
74         }
75     };
76 }
77
78 // End FormatFunDef.java
79
Popular Tags