001/*
002// $Id: DimensionType.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*/
020package org.olap4j.type;
021
022import org.olap4j.metadata.*;
023
024/**
025 * The type of an expression which represents a Dimension.
026 *
027 * @author jhyde
028 * @since Feb 17, 2005
029 * @version $Id: DimensionType.java 482 2012-01-05 23:27:27Z jhyde $
030 */
031public class DimensionType implements Type {
032    private final Dimension dimension;
033    private final String digest;
034
035    public static final DimensionType Unknown = new DimensionType(null);
036
037    /**
038     * Creates a type representing a dimension.
039     *
040     * @param dimension Dimension which values of this type must belong to, or
041     *   null if not known
042     */
043    public DimensionType(Dimension dimension) {
044        this.dimension = dimension;
045        StringBuilder buf = new StringBuilder("DimensionType<");
046        if (dimension != null) {
047            buf.append("dimension=").append(dimension.getUniqueName());
048        }
049        buf.append(">");
050        this.digest = buf.toString();
051    }
052
053    public boolean usesDimension(Dimension dimension, boolean maybe) {
054        if (this.dimension == null) {
055            return maybe;
056        } else {
057            return this.dimension.equals(dimension);
058        }
059    }
060
061    public Hierarchy getHierarchy() {
062        return dimension == null
063            ? null
064            : dimension.getHierarchies().size() > 1
065            ? null
066            : dimension.getHierarchies().get(0);
067    }
068
069    public Level getLevel() {
070        return null;
071    }
072
073    public Dimension getDimension() {
074        return dimension;
075    }
076
077    public String toString() {
078        return digest;
079    }
080}
081
082// End DimensionType.java