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