001    /*
002    // $Id: WithMemberNode.java 229 2009-05-08 19:11:29Z jhyde $
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) 2007-2008 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009     */
010    package org.olap4j.mdx;
011    
012    import org.olap4j.type.Type;
013    
014    import java.util.List;
015    import java.io.PrintWriter;
016    
017    /**
018     * Parse tree node which declares a calculated member. Represented as the
019     * <code>WITH MEMBER</code> clause of an MDX <code>SELECT</code> statement.
020     *
021     * @version $Id: WithMemberNode.java 229 2009-05-08 19:11:29Z jhyde $
022     * @author jhyde
023     */
024    public class WithMemberNode implements ParseTreeNode {
025    
026        private final ParseRegion region;
027    
028        /** name of set or member */
029        private final IdentifierNode name;
030    
031        /** defining expression */
032        private ParseTreeNode expression;
033    
034        // properties of member, such as SOLVE_ORDER
035        private final List<PropertyValueNode> memberPropertyList;
036    
037        /**
038         * Constructs a formula specifying a member.
039         *
040         * @param region Source code region
041         * @param name   Name of member being declared
042         * @param exp    Expression for value of member
043         * @param memberPropertyList Collection of properties of member
044         */
045        public WithMemberNode(
046            ParseRegion region,
047            IdentifierNode name,
048            ParseTreeNode exp,
049            List<PropertyValueNode> memberPropertyList)
050        {
051            this.region = region;
052            this.name = name;
053            this.expression = exp;
054            this.memberPropertyList = memberPropertyList;
055        }
056    
057        public ParseRegion getRegion() {
058            return region;
059        }
060    
061        public void unparse(ParseTreeWriter writer) {
062            PrintWriter pw = writer.getPrintWriter();
063            pw.print("MEMBER ");
064            name.unparse(writer);
065            pw.print(" AS '");
066            expression.unparse(writer);
067            pw.print("'");
068            if (memberPropertyList != null) {
069                for (PropertyValueNode memberProperty : memberPropertyList) {
070                    pw.print(", ");
071                    memberProperty.unparse(writer);
072                }
073            }
074        }
075    
076        /**
077         * Returns the name of the member declared.
078         *
079         * <p>The name is as specified in the parse tree; it may not be identical
080         * to the unique name of the member.
081         *
082         * @return Name of member
083         */
084        public IdentifierNode getIdentifier() {
085            return name;
086        }
087    
088        /**
089         * Returns the expression to evaluate to calculate the member.
090         *
091         * @return expression
092         */
093        public ParseTreeNode getExpression() {
094            return expression;
095        }
096    
097        /**
098         * Sets the expression to evaluate to calculate the member.
099         *
100         * @param expression Expression
101         */
102        public void setExpression(ParseTreeNode expression) {
103            this.expression = expression;
104        }
105    
106    
107        public <T> T accept(ParseTreeVisitor<T> visitor) {
108            T t = visitor.visit(this);
109            name.accept(visitor);
110            expression.accept(visitor);
111            return t;
112        }
113    
114        public Type getType() {
115            // not an expression
116            throw new UnsupportedOperationException();
117        }
118    
119        /**
120         * Returns the list of properties of this member.
121         *
122         * <p>The list may be empty, but is never null.
123         * Each entry is a (name, expression) pair.
124         *
125         * @return list of properties
126         */
127        public List<PropertyValueNode> getMemberPropertyList() {
128            return memberPropertyList;
129        }
130    
131        public WithMemberNode deepCopy() {
132            return new WithMemberNode(
133                this.region, // immutable
134                this.name.deepCopy(),
135                this.expression.deepCopy(),
136                MdxUtil.deepCopyList(memberPropertyList));
137        }
138    }
139    
140    // End WithMemberNode.java