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) 2008-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 * Adapter which computes a set expression and converts it to any list or
019 * iterable type.
020 *
021 * @author jhyde
022 * @since Nov 7, 2008
023 */
024public abstract class GenericIterCalc
025    extends AbstractCalc
026    implements ListCalc, IterCalc
027{
028    /**
029     * Creates a GenericIterCalc without specifying child calculated
030     * expressions.
031     *
032     * <p>Subclass should override {@link #getCalcs()}.
033     *
034     * @param exp Source expression
035     */
036    protected GenericIterCalc(Exp exp) {
037        super(exp, null);
038    }
039
040    /**
041     * Creates an GenericIterCalc.
042     *
043     * @param exp Source expression
044     * @param calcs Child compiled expressions
045     */
046    protected GenericIterCalc(Exp exp, Calc[] calcs) {
047        super(exp, calcs);
048    }
049
050    public SetType getType() {
051        return (SetType) type;
052    }
053
054    public TupleList evaluateList(Evaluator evaluator) {
055        Object o = evaluate(evaluator);
056        if (o instanceof TupleList) {
057            return (TupleList) o;
058        } else {
059            // Iterable
060            final TupleIterable iterable = (TupleIterable) o;
061            TupleList tupleList =
062                TupleCollections.createList(iterable.getArity());
063            TupleCursor cursor = iterable.tupleCursor();
064            while (cursor.forward()) {
065                tupleList.addCurrent(cursor);
066            }
067            return tupleList;
068        }
069    }
070
071    public TupleIterable evaluateIterable(Evaluator evaluator) {
072        Object o = evaluate(evaluator);
073        return (TupleIterable) o;
074    }
075}
076
077// End GenericIterCalc.java