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;
014
015/**
016 * Adapter that converts a {@link mondrian.calc.IterCalc} to a
017 * {@link mondrian.calc.ListCalc}.
018 *
019 * @author jhyde
020 * @since Oct 23, 2008
021 */
022public class IterableListCalc extends AbstractListCalc {
023    private final IterCalc iterCalc;
024
025    /**
026     * Creates an IterableListCalc.
027     *
028     * @param iterCalc Calculation that returns an iterable.
029     */
030    public IterableListCalc(IterCalc iterCalc) {
031        super(new DummyExp(iterCalc.getType()), new Calc[] {iterCalc});
032        this.iterCalc = iterCalc;
033    }
034
035    public TupleList evaluateList(Evaluator evaluator) {
036        // A TupleIterCalc is allowed to return a list. If so, save the copy.
037        final TupleIterable iterable =
038            iterCalc.evaluateIterable(evaluator);
039        if (iterable instanceof TupleList) {
040            return (TupleList) iterable;
041        }
042
043        final TupleList list = TupleCollections.createList(iterable.getArity());
044        final TupleCursor tupleCursor = iterable.tupleCursor();
045        while (tupleCursor.forward()) {
046            // REVIEW: Worth creating TupleList.addAll(TupleCursor)?
047            list.addCurrent(tupleCursor);
048        }
049        return list;
050    }
051}
052
053// End IterableListCalc.java