001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2005-2005 Julian Hyde
008// Copyright (C) 2005-2011 Pentaho
009// All Rights Reserved.
010*/
011package mondrian.olap.type;
012
013import mondrian.olap.*;
014
015/**
016 * Base class for types which represent scalar values.
017 *
018 * <p>An instance of this class means a scalar value of unknown type.
019 * Usually one of the derived classes {@link NumericType},
020 * {@link StringType}, {@link BooleanType} is used instead.
021 *
022 * @author jhyde
023 * @since Feb 17, 2005
024 */
025public class ScalarType implements Type {
026    private final String digest;
027
028    /**
029     * Creates a ScalarType.
030     */
031    public ScalarType() {
032        this("SCALAR");
033    }
034
035    /**
036     * Creates a ScalarType (or subtype) with a given digest.
037     *
038     * <p>The digest is used for {@link #toString()} and {@link #hashCode()}.
039     *
040     * @param digest Description of this type
041     */
042    protected ScalarType(String digest) {
043        this.digest = digest;
044    }
045
046    public int hashCode() {
047        return digest.hashCode();
048    }
049
050    public boolean equals(Object obj) {
051        return obj != null
052            && obj.getClass() == ScalarType.class;
053    }
054
055    public String toString() {
056        return digest;
057    }
058
059    public boolean usesDimension(Dimension dimension, boolean definitely) {
060        return false;
061    }
062
063    public boolean usesHierarchy(Hierarchy hierarchy, boolean definitely) {
064        return false;
065    }
066
067    public Hierarchy getHierarchy() {
068        return null;
069    }
070
071    public Level getLevel() {
072        return null;
073    }
074
075    public Type computeCommonType(Type type, int[] conversionCount) {
076        if (this.equals(type)) {
077            return this;
078        } else if (type instanceof NullType) {
079            return this;
080        } else if (this instanceof NullType
081            && type instanceof ScalarType)
082        {
083            return type;
084        } else if (this.getClass() == ScalarType.class
085            && type instanceof ScalarType)
086        {
087            return this;
088        } else if (type.getClass() == ScalarType.class) {
089            return type;
090        } else if (type instanceof ScalarType) {
091            return new ScalarType();
092        } else if (type instanceof MemberType) {
093            return computeCommonType(
094                ((MemberType) type).getValueType(),
095                conversionCount);
096        } else if (type instanceof TupleType) {
097            return computeCommonType(
098                ((TupleType) type).getValueType(),
099                conversionCount);
100        } else {
101            return null;
102        }
103    }
104
105    public Dimension getDimension() {
106        return null;
107    }
108
109    public boolean isInstance(Object value) {
110        // Somewhat pessimistic.
111        return false;
112    }
113
114    public int getArity() {
115        return 1;
116    }
117}
118
119// End ScalarType.java