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) 2009-2011 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.rolap;
011
012import mondrian.calc.Calc;
013import mondrian.olap.Util;
014
015import java.util.List;
016
017/**
018 * Implementation of {@link mondrian.rolap.RolapCalculation}
019 * that changes one or more dimensions, then evaluates a given calculation.
020 *
021 * <p>It is used to implement sets in slicers, in particular sets of tuples in
022 * the slicer.
023 *
024 * @author jhyde
025 * @since May 15, 2009
026 */
027class RolapTupleCalculation implements RolapCalculation {
028    private final List<RolapHierarchy> hierarchyList;
029    private final Calc calc;
030    private final int hashCode;
031
032    /**
033     * Creates a RolapTupleCalculation.
034     *
035     * @param hierarchyList List of hierarchies to be replaced.
036     * @param calc Compiled scalar expression to compute cell
037     */
038    public RolapTupleCalculation(
039        List<RolapHierarchy> hierarchyList,
040        Calc calc)
041    {
042        this.hierarchyList = hierarchyList;
043        this.calc = calc;
044        this.hashCode = Util.hash(hierarchyList.hashCode(), calc);
045    }
046
047    @Override
048    public int hashCode() {
049        return hashCode;
050    }
051
052    @Override
053    public boolean equals(Object obj) {
054        if (this == obj) {
055            return true;
056        }
057        if (obj instanceof RolapTupleCalculation) {
058            RolapTupleCalculation calculation = (RolapTupleCalculation) obj;
059            return this.hierarchyList.equals(calculation.hierarchyList)
060                && this.calc.equals(calculation.calc);
061        }
062        return false;
063    }
064
065    @Override
066    public String toString() {
067        return calc.toString();
068    }
069
070    public void setContextIn(RolapEvaluator evaluator) {
071        // Restore default member for each hierarchy
072        // in the tuple.
073        for (RolapHierarchy hierarchy : hierarchyList) {
074            final int ordinal = hierarchy.getOrdinalInCube();
075            final RolapMember defaultMember =
076                evaluator.root.defaultMembers[ordinal];
077            evaluator.setContext(defaultMember);
078        }
079
080        evaluator.removeCalculation(this, true);
081    }
082
083    public int getSolveOrder() {
084        return Integer.MIN_VALUE;
085    }
086
087    public int getHierarchyOrdinal() {
088        throw new UnsupportedOperationException();
089    }
090
091    public Calc getCompiledExpression(RolapEvaluatorRoot root) {
092        return calc;
093    }
094
095    public boolean containsAggregateFunction() {
096        return false;
097    }
098
099    public boolean isCalculatedInQuery() {
100        return true;
101    }
102}
103
104// End RolapTupleCalculation.java