001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/type/ScalarType.java#7 $
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.Dimension;
013    import mondrian.olap.Hierarchy;
014    import mondrian.olap.Level;
015    
016    /**
017     * Base class for types which represent scalar values.
018     *
019     * <p>An instance of this class means a scalar value of unknown type.
020     * Usually one of the derived classes {@link NumericType},
021     * {@link StringType}, {@link BooleanType} is used instead.
022     *
023     * @author jhyde
024     * @since Feb 17, 2005
025     * @version $Id: //open/mondrian/src/main/mondrian/olap/type/ScalarType.java#7 $
026     */
027    public class ScalarType implements Type {
028        private final String digest;
029    
030        /**
031         * Creates a ScalarType.
032         */
033        public ScalarType() {
034            this("SCALAR");
035        }
036    
037        /**
038         * Creates a ScalarType (or subtype) with a given digest.
039         *
040         * <p>The digest is used for {@link #toString()} and {@link #hashCode()}.
041         *
042         * @param digest Description of this type
043         */
044        protected ScalarType(String digest) {
045            this.digest = digest;
046        }
047    
048        public int hashCode() {
049            return digest.hashCode();
050        }
051    
052        public boolean equals(Object obj) {
053            return obj != null
054                && obj.getClass() == ScalarType.class;
055        }
056    
057        public String toString() {
058            return digest;
059        }
060    
061        public boolean usesDimension(Dimension dimension, boolean definitely) {
062            return false;
063        }
064    
065        public Hierarchy getHierarchy() {
066            return null;
067        }
068    
069        public Level getLevel() {
070            return null;
071        }
072    
073        public Type computeCommonType(Type type, int[] conversionCount) {
074            if (this.equals(type)) {
075                return this;
076            } else if (type instanceof NullType) {
077                return this;
078            } else if (this instanceof NullType
079                && type instanceof ScalarType) {
080                return type;
081            } else if (this.getClass() == ScalarType.class
082                && type instanceof ScalarType) {
083                return this;
084            } else if (type.getClass() == ScalarType.class) {
085                return type;
086            } else if (type instanceof ScalarType) {
087                return new ScalarType();
088            } else if (type instanceof MemberType) {
089                return computeCommonType(((MemberType) type).getValueType(),
090                    conversionCount);
091            } else if (type instanceof TupleType) {
092                return computeCommonType(((TupleType) type).getValueType(),
093                    conversionCount);
094            } else {
095                return null;
096            }
097        }
098    
099        public Dimension getDimension() {
100            return null;
101        }
102    }
103    
104    // End ScalarType.java