001    /*
002    // $Id: HierarchyType.java 247 2009-06-20 05:52:40Z jhyde $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2005-2009 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package org.olap4j.type;
011    
012    import org.olap4j.metadata.Hierarchy;
013    import org.olap4j.metadata.Dimension;
014    import org.olap4j.metadata.Level;
015    import org.olap4j.OlapException;
016    
017    /**
018     * The type of an expression which represents a hierarchy.
019     *
020     * @author jhyde
021     * @since Feb 17, 2005
022     * @version $Id: HierarchyType.java 247 2009-06-20 05:52:40Z jhyde $
023     */
024    public class HierarchyType implements Type {
025        private final Dimension dimension;
026        private final Hierarchy hierarchy;
027        private final String digest;
028    
029        /**
030         * Creates a type representing a hierarchy.
031         *
032         * @param dimension Dimension which values of this type must belong to, or
033         *   null if not known
034         *
035         * @param hierarchy Hierarchy which values of this type must belong to, or
036         *   null if not known
037         */
038        public HierarchyType(
039            Dimension dimension,
040            Hierarchy hierarchy)
041        {
042            this.dimension = dimension;
043            this.hierarchy = hierarchy;
044            StringBuilder buf = new StringBuilder("HierarchyType<");
045            if (hierarchy != null) {
046                buf.append("hierarchy=").append(hierarchy.getUniqueName());
047            } else if (dimension != null) {
048                buf.append("dimension=").append(dimension.getUniqueName());
049            }
050            buf.append(">");
051            this.digest = buf.toString();
052        }
053    
054        // not part of public olap4j API
055        private static HierarchyType forType(Type type) throws OlapException {
056            return new HierarchyType(type.getDimension(), type.getHierarchy());
057        }
058    
059        public boolean usesDimension(Dimension dimension, boolean maybe) {
060            if (this.dimension == null) {
061                return maybe;
062            } else {
063                return this.dimension.equals(dimension);
064            }
065        }
066    
067        public Dimension getDimension() {
068            return dimension;
069        }
070    
071        public Hierarchy getHierarchy() {
072            return hierarchy;
073        }
074    
075        public Level getLevel() {
076            return null;
077        }
078    
079        public String toString() {
080            return digest;
081        }
082    }
083    
084    // End HierarchyType.java