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