001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/IsFunDef.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-2006 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.*;
013    import mondrian.calc.Calc;
014    import mondrian.calc.ExpCompiler;
015    import mondrian.calc.TupleCalc;
016    import mondrian.calc.impl.AbstractBooleanCalc;
017    import mondrian.mdx.ResolvedFunCall;
018    
019    /**
020     * Definition of the <code>IS</code> MDX function.
021     *
022     * @see IsNullFunDef
023     * @author jhyde
024     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/IsFunDef.java#4 $
025     * @since Mar 23, 2006
026     */
027    class IsFunDef extends FunDefBase {
028        static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver(
029                "IS",
030                "<Expression> IS <Expression>",
031                "Returns whether two objects are the same",
032                new String[] {"ibmm", "ibll", "ibhh", "ibdd", "ibtt"},
033                IsFunDef.class);
034    
035        public IsFunDef(FunDef dummyFunDef) {
036            super(dummyFunDef);
037        }
038    
039        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
040            final int category = call.getArg(0).getCategory();
041            switch (category) {
042            case Category.Tuple:
043                final TupleCalc tupleCalc0 = compiler.compileTuple(call.getArg(0));
044                final TupleCalc tupleCalc1 = compiler.compileTuple(call.getArg(1));
045                return new AbstractBooleanCalc(call, new Calc[] {tupleCalc0, tupleCalc1}) {
046                    public boolean evaluateBoolean(Evaluator evaluator) {
047                        Member[] o0 = tupleCalc0.evaluateTuple(evaluator);
048                        Member[] o1 = tupleCalc1.evaluateTuple(evaluator);
049                        return equalTuple(o0, o1);
050                    }
051                };
052            default:
053                assert category == call.getArg(1).getCategory();
054                final Calc calc0 = compiler.compile(call.getArg(0));
055                final Calc calc1 = compiler.compile(call.getArg(1));
056                return new AbstractBooleanCalc(call, new Calc[] {calc0, calc1}) {
057                    public boolean evaluateBoolean(Evaluator evaluator) {
058                        Object o0 = calc0.evaluate(evaluator);
059                        Object o1 = calc1.evaluate(evaluator);
060                        return o0.equals(o1);
061                    }
062                };
063            }
064        }
065    }
066    
067    // End IsFunDef.java