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.type;
019
020import org.olap4j.OlapException;
021import org.olap4j.metadata.*;
022
023/**
024 * The type of an expression which represents a hierarchy.
025 *
026 * @author jhyde
027 * @since Feb 17, 2005
028 */
029public class HierarchyType implements Type {
030    private final Dimension dimension;
031    private final Hierarchy hierarchy;
032    private final String digest;
033
034    /**
035     * Creates a type representing a hierarchy.
036     *
037     * @param dimension Dimension which values of this type must belong to, or
038     *   null if not known
039     *
040     * @param hierarchy Hierarchy which values of this type must belong to, or
041     *   null if not known
042     */
043    public HierarchyType(
044        Dimension dimension,
045        Hierarchy hierarchy)
046    {
047        this.dimension = dimension;
048        this.hierarchy = hierarchy;
049        StringBuilder buf = new StringBuilder("HierarchyType<");
050        if (hierarchy != null) {
051            buf.append("hierarchy=").append(hierarchy.getUniqueName());
052        } else if (dimension != null) {
053            buf.append("dimension=").append(dimension.getUniqueName());
054        }
055        buf.append(">");
056        this.digest = buf.toString();
057    }
058
059    // not part of public olap4j API
060    private static HierarchyType forType(Type type) throws OlapException {
061        return new HierarchyType(type.getDimension(), type.getHierarchy());
062    }
063
064    public boolean usesDimension(Dimension dimension, boolean maybe) {
065        if (this.dimension == null) {
066            return maybe;
067        } else {
068            return this.dimension.equals(dimension);
069        }
070    }
071
072    public Dimension getDimension() {
073        return dimension;
074    }
075
076    public Hierarchy getHierarchy() {
077        return hierarchy;
078    }
079
080    public Level getLevel() {
081        return null;
082    }
083
084    public String toString() {
085        return digest;
086    }
087}
088
089// End HierarchyType.java