001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/HierarchizeFunDef.java#5 $
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-2007 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.*;
013    import mondrian.calc.impl.AbstractListCalc;
014    import mondrian.mdx.ResolvedFunCall;
015    import mondrian.olap.Evaluator;
016    import mondrian.olap.FunDef;
017    
018    import java.util.List;
019    
020    /**
021     * Definition of the <code>Hierarchize</code> MDX function.
022     *
023     * @author jhyde
024     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/HierarchizeFunDef.java#5 $
025     * @since Mar 23, 2006
026     */
027    class HierarchizeFunDef extends FunDefBase {
028        static final String[] prePost = {"PRE","POST"};
029        static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver(
030                "Hierarchize",
031                "Hierarchize(<Set>[, POST])",
032                "Orders the members of a set in a hierarchy.",
033                new String[] {"fxx", "fxxy"},
034                HierarchizeFunDef.class,
035                prePost);
036    
037        public HierarchizeFunDef(FunDef dummyFunDef) {
038            super(dummyFunDef);
039        }
040    
041        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
042            final ListCalc listCalc =
043                compiler.compileList(call.getArg(0), true);
044            String order = getLiteralArg(call, 1, "PRE", prePost);
045            final boolean post = order.equals("POST");
046            return new AbstractListCalc(call, new Calc[] {listCalc}) {
047                public List evaluateList(Evaluator evaluator) {
048                    List list = listCalc.evaluateList(evaluator);
049                    hierarchize(list, post);
050                    return list;
051                }
052            };
053        }
054    }
055    
056    // End HierarchizeFunDef.java