001    /*
002    // $Id: LevelNode.java 482 2012-01-05 23:27:27Z jhyde $
003    //
004    // Licensed to Julian Hyde under one or more contributor license
005    // agreements. See the NOTICE file distributed with this work for
006    // additional information regarding copyright ownership.
007    //
008    // Julian Hyde licenses this file to you under the Apache License,
009    // Version 2.0 (the "License"); you may not use this file except in
010    // compliance with the License. You may obtain a copy of the License at:
011    //
012    // http://www.apache.org/licenses/LICENSE-2.0
013    //
014    // Unless required by applicable law or agreed to in writing, software
015    // distributed under the License is distributed on an "AS IS" BASIS,
016    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017    // See the License for the specific language governing permissions and
018    // limitations under the License.
019    */
020    package org.olap4j.mdx;
021    
022    import org.olap4j.metadata.Level;
023    import org.olap4j.type.LevelType;
024    import org.olap4j.type.Type;
025    
026    /**
027     * Usage of a {@link org.olap4j.metadata.Level} as an expression in an MDX
028     * parse tree.
029     *
030     * @author jhyde
031     * @version $Id: LevelNode.java 482 2012-01-05 23:27:27Z jhyde $
032     * @since Jun 4, 2007
033     */
034    public class LevelNode implements ParseTreeNode {
035        private final ParseRegion region;
036        private final Level level;
037    
038        /**
039         * Creates a LevelNode.
040         *
041         * @param region Region of source code
042         * @param level Level which is used in the expression
043         */
044        public LevelNode(
045            ParseRegion region,
046            Level level)
047        {
048            this.region = region;
049            this.level = level;
050        }
051    
052        public ParseRegion getRegion() {
053            return region;
054        }
055    
056        /**
057         * Returns the Level used in this expression.
058         *
059         * @return level used in this expression
060         */
061        public Level getLevel() {
062            return level;
063        }
064    
065        public <T> T accept(ParseTreeVisitor<T> visitor) {
066            return visitor.visit(this);
067        }
068    
069        public Type getType() {
070            return new LevelType(
071                level.getDimension(),
072                level.getHierarchy(),
073                level);
074        }
075    
076        public void unparse(ParseTreeWriter writer) {
077            writer.getPrintWriter().print(level.getUniqueName());
078        }
079    
080        public String toString() {
081            return level.getUniqueName();
082        }
083    
084        public LevelNode deepCopy() {
085            // LevelNode is immutable
086            return this;
087        }
088    
089    }
090    
091    // End LevelNode.java