001    /*
002    // $Id: //open/mondrian-release/3.2/src/main/mondrian/olap/fun/NamedSetCurrentOrdinalFunDef.java#1 $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2006-2009 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.calc.Calc;
013    import mondrian.calc.ExpCompiler;
014    import mondrian.calc.impl.AbstractIntegerCalc;
015    import mondrian.mdx.ResolvedFunCall;
016    import mondrian.mdx.NamedSetExpr;
017    import mondrian.olap.*;
018    import mondrian.resource.MondrianResource;
019    
020    /**
021     * Definition of the <code>&lt;Named Set&gt;.CurrentOrdinal</code> MDX builtin
022     * function.
023     *
024     * @author jhyde
025     * @version $Id: //open/mondrian-release/3.2/src/main/mondrian/olap/fun/NamedSetCurrentOrdinalFunDef.java#1 $
026     * @since Oct 19, 2008
027     */
028    public class NamedSetCurrentOrdinalFunDef extends FunDefBase {
029        static final NamedSetCurrentOrdinalFunDef instance =
030            new NamedSetCurrentOrdinalFunDef();
031    
032        private NamedSetCurrentOrdinalFunDef() {
033            super(
034                "CurrentOrdinal",
035                "Returns the ordinal of the current iteration through a named set.",
036                "pix");
037        }
038    
039        public Exp createCall(Validator validator, Exp[] args) {
040            assert args.length == 1;
041            final Exp arg0 = args[0];
042            if (!(arg0 instanceof NamedSetExpr)) {
043                throw MondrianResource.instance().NotANamedSet.ex();
044            }
045            return super.createCall(validator, args);
046        }
047    
048        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
049            final Exp arg0 = call.getArg(0);
050            assert arg0 instanceof NamedSetExpr : "checked this in createCall";
051            final NamedSetExpr namedSetExpr = (NamedSetExpr) arg0;
052            return new AbstractIntegerCalc(call, new Calc[0]) {
053                public int evaluateInteger(Evaluator evaluator) {
054                    return namedSetExpr.getEval(evaluator).currentOrdinal();
055                }
056            };
057        }
058    }
059    
060    // End NamedSetCurrentOrdinalFunDef.java