001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2006-2011 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.olap.fun;
011
012import mondrian.calc.*;
013import mondrian.calc.impl.AbstractListCalc;
014import mondrian.mdx.ResolvedFunCall;
015import mondrian.olap.Evaluator;
016import mondrian.olap.FunDef;
017
018/**
019 * Definition of the <code>Hierarchize</code> MDX function.
020 *
021 * @author jhyde
022 * @since Mar 23, 2006
023 */
024class HierarchizeFunDef extends FunDefBase {
025    static final String[] prePost = {"PRE", "POST"};
026    static final ReflectiveMultiResolver Resolver =
027        new ReflectiveMultiResolver(
028            "Hierarchize",
029            "Hierarchize(<Set>[, POST])",
030            "Orders the members of a set in a hierarchy.",
031            new String[] {"fxx", "fxxy"},
032            HierarchizeFunDef.class,
033            prePost);
034
035    public HierarchizeFunDef(FunDef dummyFunDef) {
036        super(dummyFunDef);
037    }
038
039    public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
040        final ListCalc listCalc =
041            compiler.compileList(call.getArg(0), true);
042        String order = getLiteralArg(call, 1, "PRE", prePost);
043        final boolean post = order.equals("POST");
044        return new AbstractListCalc(call, new Calc[] {listCalc}) {
045            public TupleList evaluateList(Evaluator evaluator) {
046                TupleList list = listCalc.evaluateList(evaluator);
047                return hierarchizeTupleList(list, post);
048            }
049        };
050    }
051}
052
053// End HierarchizeFunDef.java