001    /*
002    // $Id: //open/mondrian-release/3.2/src/main/mondrian/calc/impl/IterableTupleListCalc.java#1 $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2008-2009 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.calc.impl;
011    
012    import mondrian.olap.*;
013    import mondrian.calc.DummyExp;
014    import mondrian.calc.Calc;
015    import mondrian.calc.TupleIterCalc;
016    
017    import java.util.List;
018    import java.util.ArrayList;
019    
020    /**
021     * Adapter that converts a {@link mondrian.calc.IterCalc} to a
022     * {@link mondrian.calc.ListCalc}.
023     *
024     * @author jhyde
025     * @version $Id: //open/mondrian-release/3.2/src/main/mondrian/calc/impl/IterableTupleListCalc.java#1 $
026     * @since Oct 23, 2008
027     */
028    public class IterableTupleListCalc extends AbstractTupleListCalc {
029        private final TupleIterCalc iterCalc;
030    
031        /**
032         * Creates an IterableListCalc.
033         *
034         * @param iterCalc Calculation that returns an iterable.
035         */
036        public IterableTupleListCalc(TupleIterCalc iterCalc) {
037            super(new DummyExp(iterCalc.getType()), new Calc[] {iterCalc});
038            this.iterCalc = iterCalc;
039        }
040    
041        public List<Member[]> evaluateTupleList(Evaluator evaluator) {
042            // A TupleIterCalc is allowed to return a list. If so, save the copy.
043            final Iterable<Member[]> iterable =
044                iterCalc.evaluateTupleIterable(evaluator);
045            if (iterable instanceof List) {
046                return Util.cast((List) iterable);
047            }
048    
049            final List<Member[]> list = new ArrayList<Member[]>();
050            for (Member[] o : iterable) {
051                list.add(o);
052            }
053            return list;
054        }
055    }
056    
057    // End IterableTupleListCalc.java