001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/type/DimensionType.java#9 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.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 mondrian.olap.type;
011    
012    import mondrian.olap.*;
013    
014    /**
015     * The type of an expression which represents a Dimension.
016     *
017     * @author jhyde
018     * @since Feb 17, 2005
019     * @version $Id: //open/mondrian/src/main/mondrian/olap/type/DimensionType.java#9 $
020     */
021    public class DimensionType implements Type {
022        private final Dimension dimension;
023        private final String digest;
024    
025        public static final DimensionType Unknown = new DimensionType(null);
026    
027        /**
028         * Creates a type representing a dimension.
029         *
030         * @param dimension Dimension that values of this type must belong to, or
031         *   null if the dimension is unknown
032         */
033        public DimensionType(Dimension dimension) {
034            this.dimension = dimension;
035            StringBuilder buf = new StringBuilder("DimensionType<");
036            if (dimension != null) {
037                buf.append("dimension=").append(dimension.getUniqueName());
038            }
039            buf.append(">");
040            this.digest = buf.toString();
041        }
042    
043        public static DimensionType forDimension(Dimension dimension) {
044            return new DimensionType(dimension);
045        }
046    
047        public static DimensionType forType(Type type) {
048            return new DimensionType(type.getDimension());
049        }
050    
051        public boolean usesDimension(Dimension dimension, boolean definitely) {
052            return this.dimension == dimension ||
053                (definitely && this.dimension == null);
054        }
055    
056        public Hierarchy getHierarchy() {
057            return dimension == null ?
058                    null :
059                    dimension.getHierarchies().length > 1 ?
060                    null :
061                    dimension.getHierarchies()[0];
062        }
063    
064        public Level getLevel() {
065            return null;
066        }
067    
068        public Dimension getDimension() {
069            return dimension;
070        }
071    
072        public int hashCode() {
073            return digest.hashCode();
074        }
075    
076        public boolean equals(Object obj) {
077            if (obj instanceof DimensionType) {
078                DimensionType that = (DimensionType) obj;
079                return Util.equals(this.getDimension(), that.getDimension());
080            }
081            return false;
082        }
083    
084        public String toString() {
085            return digest;
086        }
087    
088        public Type computeCommonType(Type type, int[] conversionCount) {
089            if (conversionCount != null && type instanceof HierarchyType) {
090                HierarchyType hierarchyType = (HierarchyType) type;
091                if (Util.equals(hierarchyType.getDimension(), dimension)) {
092                    ++conversionCount[0];
093                    return this;
094                }
095                return null;
096            }
097            if (!(type instanceof DimensionType)) {
098                return null;
099            }
100            DimensionType that = (DimensionType) type;
101            if (this.getDimension() != null
102                && this.getDimension().equals(that.getDimension())) {
103                return new DimensionType(
104                    this.getDimension());
105            }
106            return DimensionType.Unknown;
107        }
108    }
109    
110    // End DimensionType.java