001    /*
002    // $Id: //open/mondrian/src/main/mondrian/calc/impl/AbstractListCalc.java#6 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2006-2008 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 java.util.List;
013    
014    import mondrian.calc.*;
015    import mondrian.olap.Evaluator;
016    import mondrian.olap.Exp;
017    import mondrian.olap.Member;
018    import mondrian.olap.type.SetType;
019    
020    /**
021     * Abstract implementation of the {@link mondrian.calc.ListCalc} interface.
022     *
023     * <p>The derived class must
024     * implement the {@link #evaluateList(mondrian.olap.Evaluator)} method,
025     * and the {@link #evaluate(mondrian.olap.Evaluator)} method will call it.
026     *
027     * @author jhyde
028     * @version $Id: //open/mondrian/src/main/mondrian/calc/impl/AbstractListCalc.java#6 $
029     * @since Sep 27, 2005
030     */
031    public abstract class AbstractListCalc
032        extends AbstractCalc
033        implements ListCalc, MemberListCalc, TupleListCalc
034    {
035        private final Calc[] calcs;
036        private final boolean mutable;
037    
038        /**
039         * Creates an abstract implementation of a compiled expression which returns
040         * a mutable list.
041         *
042         * @param exp Expression which was compiled
043         * @param calcs List of child compiled expressions (for dependency
044         *   analysis)
045         */
046        protected AbstractListCalc(Exp exp, Calc[] calcs) {
047            this(exp, calcs, true);
048        }
049    
050        /**
051         * Creates an abstract implementation of a compiled expression which returns
052         * a list.
053         *
054         * @param exp Expression which was compiled
055         * @param calcs List of child compiled expressions (for dependency
056         *   analysis)
057         * @param mutable Whether the list is mutable
058         */
059        protected AbstractListCalc(Exp exp, Calc[] calcs, boolean mutable) {
060            super(exp);
061            this.calcs = calcs;
062            this.mutable = mutable;
063            assert getType() instanceof SetType : "expecting a set: " + getType();
064        }
065    
066        public Object evaluate(Evaluator evaluator) {
067            final List list = evaluateList(evaluator);
068            assert list != null : "null as empty list is deprecated";
069            return list;
070        }
071    
072        public Calc[] getCalcs() {
073            return calcs;
074        }
075    
076        public ResultStyle getResultStyle() {
077            return mutable ?
078                ResultStyle.MUTABLE_LIST :
079                ResultStyle.LIST;
080        }
081    
082        @SuppressWarnings({"unchecked"})
083        public List<Member> evaluateMemberList(Evaluator evaluator) {
084            return (List<Member>) evaluateList(evaluator);
085        }
086    
087        @SuppressWarnings({"unchecked"})
088        public List<Member[]> evaluateTupleList(Evaluator evaluator) {
089            return (List<Member[]>) evaluateList(evaluator);
090        }
091    
092    }
093    
094    // End AbstractListCalc.java