Search in sources :

Example 1 with AbstractStringCalc

use of mondrian.calc.impl.AbstractStringCalc in project mondrian by pentaho.

the class FormatFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final Exp[] args = call.getArgs();
    final Calc calc = compiler.compileScalar(call.getArg(0), true);
    final Locale locale = compiler.getEvaluator().getConnectionLocale();
    if (args[1] instanceof Literal) {
        // Constant string expression: optimize by
        // compiling format string.
        String formatString = (String) ((Literal) args[1]).getValue();
        final Format format = new Format(formatString, locale);
        return new AbstractStringCalc(call, new Calc[] { calc }) {

            public String evaluateString(Evaluator evaluator) {
                final Object o = calc.evaluate(evaluator);
                return format.format(o);
            }
        };
    } else {
        // Variable string expression
        final StringCalc stringCalc = compiler.compileString(call.getArg(1));
        return new AbstractStringCalc(call, new Calc[] { calc, stringCalc }) {

            public String evaluateString(Evaluator evaluator) {
                final Object o = calc.evaluate(evaluator);
                final String formatString = stringCalc.evaluateString(evaluator);
                final Format format = new Format(formatString, locale);
                return format.format(o);
            }
        };
    }
}
Also used : Locale(java.util.Locale) AbstractStringCalc(mondrian.calc.impl.AbstractStringCalc) AbstractStringCalc(mondrian.calc.impl.AbstractStringCalc) Format(mondrian.util.Format) AbstractStringCalc(mondrian.calc.impl.AbstractStringCalc)

Example 2 with AbstractStringCalc

use of mondrian.calc.impl.AbstractStringCalc in project mondrian by pentaho.

the class TupleToStrFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    if (TypeUtil.couldBeMember(call.getArg(0).getType())) {
        final MemberCalc memberCalc = compiler.compileMember(call.getArg(0));
        return new AbstractStringCalc(call, new Calc[] { memberCalc }) {

            public String evaluateString(Evaluator evaluator) {
                final Member member = memberCalc.evaluateMember(evaluator);
                if (member.isNull()) {
                    return "";
                }
                StringBuilder buf = new StringBuilder();
                buf.append(member.getUniqueName());
                return buf.toString();
            }
        };
    } else {
        final TupleCalc tupleCalc = compiler.compileTuple(call.getArg(0));
        return new AbstractStringCalc(call, new Calc[] { tupleCalc }) {

            public String evaluateString(Evaluator evaluator) {
                final Member[] members = tupleCalc.evaluateTuple(evaluator);
                if (members == null) {
                    return "";
                }
                StringBuilder buf = new StringBuilder();
                SetToStrFunDef.appendTuple(buf, members);
                return buf.toString();
            }
        };
    }
}
Also used : AbstractStringCalc(mondrian.calc.impl.AbstractStringCalc) Evaluator(mondrian.olap.Evaluator) Member(mondrian.olap.Member)

Aggregations

AbstractStringCalc (mondrian.calc.impl.AbstractStringCalc)2 Locale (java.util.Locale)1 Evaluator (mondrian.olap.Evaluator)1 Member (mondrian.olap.Member)1 Format (mondrian.util.Format)1