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