001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/MinMaxFunDef.java#4 $
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.olap.FunDef;
013    import mondrian.olap.Evaluator;
014    import mondrian.olap.Dimension;
015    import mondrian.calc.Calc;
016    import mondrian.calc.ExpCompiler;
017    import mondrian.calc.ListCalc;
018    import mondrian.calc.impl.ValueCalc;
019    import mondrian.calc.impl.AbstractDoubleCalc;
020    import mondrian.mdx.ResolvedFunCall;
021    
022    import java.util.List;
023    
024    /**
025     * Definition of the <code>Min</code> and <code>Max</code> MDX functions.
026     *
027     * @author jhyde
028     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/MinMaxFunDef.java#4 $
029     * @since Mar 23, 2006
030     */
031    class MinMaxFunDef extends AbstractAggregateFunDef {
032        static final ReflectiveMultiResolver MinResolver = new ReflectiveMultiResolver(
033                "Min",
034                "Min(<Set>[, <Numeric Expression>])",
035                "Returns the minimum value of a numeric expression evaluated over a set.",
036                new String[]{"fnx", "fnxn"},
037                MinMaxFunDef.class);
038    
039        static final MultiResolver MaxResolver = new ReflectiveMultiResolver(
040                "Max",
041                "Max(<Set>[, <Numeric Expression>])",
042                "Returns the maximum value of a numeric expression evaluated over a set.",
043                new String[]{"fnx", "fnxn"},
044                MinMaxFunDef.class);
045    
046        private final boolean max;
047    
048        public MinMaxFunDef(FunDef dummyFunDef) {
049            super(dummyFunDef);
050            this.max = dummyFunDef.getName().equals("Max");
051        }
052    
053        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
054            final ListCalc listCalc =
055                    compiler.compileList(call.getArg(0));
056            final Calc calc = call.getArgCount() > 1 ?
057                    compiler.compileScalar(call.getArg(1), true) :
058                    new ValueCalc(call);
059            return new AbstractDoubleCalc(call, new Calc[] {listCalc, calc}) {
060                public double evaluateDouble(Evaluator evaluator) {
061                    List memberList = evaluateCurrentList(listCalc, evaluator);
062                    return (Double)(max ?
063                            max(evaluator.push(), memberList, calc) :
064                            min(evaluator.push(), memberList, calc));
065                }
066    
067                public boolean dependsOn(Dimension dimension) {
068                    return anyDependsButFirst(getCalcs(), dimension);
069                }
070            };
071        }
072    }
073    
074    // End MinMaxFunDef.java