001 /* 002 // $Id: //open/mondrian/src/main/mondrian/olap/fun/FormatFunDef.java#3 $ 003 // This software is subject to the terms of the Common Public License 004 // Agreement, available at the following URL: 005 // http://www.opensource.org/licenses/cpl.html. 006 // Copyright (C) 2006-2006 Julian Hyde 007 // All Rights Reserved. 008 // You must accept the terms of that agreement to use this software. 009 */ 010 package mondrian.olap.fun; 011 012 import mondrian.olap.FunDef; 013 import mondrian.olap.Exp; 014 import mondrian.olap.Literal; 015 import mondrian.olap.Evaluator; 016 import mondrian.calc.*; 017 import mondrian.calc.impl.AbstractStringCalc; 018 import mondrian.mdx.ResolvedFunCall; 019 import mondrian.util.Format; 020 021 import java.util.Locale; 022 023 /** 024 * Definition of the <code>Format</code> MDX function. 025 * 026 * @author jhyde 027 * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/FormatFunDef.java#3 $ 028 * @since Mar 23, 2006 029 */ 030 class FormatFunDef extends FunDefBase { 031 static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver( 032 "Format", 033 "Format(<Expression>, <String Expression>)", 034 "Formats a number or date to a string.", 035 new String[] { "fSmS", "fSnS", "fSDS" }, 036 FormatFunDef.class); 037 038 public FormatFunDef(FunDef dummyFunDef) { 039 super(dummyFunDef); 040 } 041 042 public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) { 043 final Exp[] args = call.getArgs(); 044 final Calc calc = compiler.compileScalar(call.getArg(0), true); 045 final Locale locale = compiler.getEvaluator().getConnectionLocale(); 046 if (args[1] instanceof Literal) { 047 // Constant string expression: optimize by 048 // compiling format string. 049 String formatString = (String) ((Literal) args[1]).getValue(); 050 final Format format = new Format(formatString, locale); 051 return new AbstractStringCalc(call, new Calc[] {calc}) { 052 public String evaluateString(Evaluator evaluator) { 053 final Object o = calc.evaluate(evaluator); 054 return format.format(o); 055 } 056 }; 057 } else { 058 // Variable string expression 059 final StringCalc stringCalc = 060 compiler.compileString(call.getArg(1)); 061 return new AbstractStringCalc(call, new Calc[] {calc, stringCalc}) { 062 public String evaluateString(Evaluator evaluator) { 063 final Object o = calc.evaluate(evaluator); 064 final String formatString = 065 stringCalc.evaluateString(evaluator); 066 final Format format = 067 new Format(formatString, locale); 068 return format.format(o); 069 } 070 }; 071 } 072 } 073 } 074 075 // End FormatFunDef.java