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.calc.impl;
011
012import mondrian.calc.*;
013import mondrian.olap.Evaluator;
014import mondrian.olap.Exp;
015import mondrian.olap.type.SetType;
016
017/**
018 * Abstract implementation of the {@link mondrian.calc.ListCalc} interface.
019 *
020 * <p>The derived class must
021 * implement the {@link #evaluateList(mondrian.olap.Evaluator)} method,
022 * and the {@link #evaluate(mondrian.olap.Evaluator)} method will call it.
023 *
024 * @author jhyde
025 * @since Sep 27, 2005
026 */
027public abstract class AbstractListCalc
028    extends AbstractCalc
029    implements ListCalc
030{
031    private final boolean mutable;
032
033    /**
034     * Creates an abstract implementation of a compiled expression which
035     * returns a mutable list of tuples.
036     *
037     * @param exp Expression which was compiled
038     * @param calcs List of child compiled expressions (for dependency
039     *   analysis)
040     */
041    protected AbstractListCalc(Exp exp, Calc[] calcs) {
042        this(exp, calcs, true);
043    }
044
045    /**
046     * Creates an abstract implementation of a compiled expression which
047     * returns a list.
048     *
049     * @param exp Expression which was compiled
050     * @param calcs List of child compiled expressions (for dependency
051     *   analysis)
052     * @param mutable Whether the list is mutable
053     */
054    protected AbstractListCalc(Exp exp, Calc[] calcs, boolean mutable) {
055        super(exp, calcs);
056        this.mutable = mutable;
057        assert type instanceof SetType : "expecting a set: " + getType();
058    }
059
060    public SetType getType() {
061        return (SetType) super.getType();
062    }
063
064    public final Object evaluate(Evaluator evaluator) {
065        final TupleList tupleList = evaluateList(evaluator);
066        assert tupleList != null : "null as empty tuple list is deprecated";
067        return tupleList;
068    }
069
070    public TupleIterable evaluateIterable(Evaluator evaluator) {
071        return evaluateList(evaluator);
072    }
073
074    public ResultStyle getResultStyle() {
075        return mutable
076            ? ResultStyle.MUTABLE_LIST
077            : ResultStyle.LIST;
078    }
079
080    public String toString() {
081        return "AbstractListCalc object";
082    }
083}
084
085// End AbstractListCalc.java