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-2009 Pentaho
009// All Rights Reserved.
010*/
011package mondrian.olap.type;
012
013import mondrian.olap.Util;
014
015/**
016 * Subclass of {@link NumericType} which guarantees fixed number of decimal
017 * places. In particular, a decimal with zero scale is an integer.
018 *
019 * @author jhyde
020 * @since May 3, 2005
021 */
022public class DecimalType extends NumericType {
023    private final int precision;
024    private final int scale;
025
026    /**
027     * Creates a decimal type with precision and scale.
028     *
029     * <p>Examples:<ul>
030     * <li>123.45 has precision 5, scale 2.
031     * <li>12,345,000 has precision 5, scale -3.
032     * </ul>
033     *
034     * <p>The largest value is 10 ^ (precision - scale). Hence the largest
035     * <code>DECIMAL(5, -3)</code> value is 10 ^ 8.
036     *
037     * @param precision Maximum number of decimal digits which a value of
038     *   this type can have.
039     *   Must be greater than zero.
040     *   Use {@link Integer#MAX_VALUE} if the precision is unbounded.
041     * @param scale Number of digits to the right of the decimal point.
042     */
043    public DecimalType(int precision, int scale) {
044        super(
045            precision == Integer.MAX_VALUE
046                ? "DecimalType(" + scale + ")"
047                : "DecimalType(" + precision + ", " + scale + ")");
048        Util.assertPrecondition(precision > 0, "precision > 0");
049        this.precision = precision;
050        this.scale = scale;
051    }
052
053    /**
054     * Returns the maximum number of decimal digits which a value of
055     * this type can have.
056     *
057     * @return precision of this type
058     */
059    public int getPrecision() {
060        return precision;
061    }
062
063    /**
064     * Returns the number of digits to the right of the decimal point.
065     *
066     * @return scale of this type
067     */
068    public int getScale() {
069        return scale;
070    }
071
072    public boolean equals(Object obj) {
073        if (obj instanceof DecimalType) {
074            DecimalType that = (DecimalType) obj;
075            return this.precision == that.precision
076                && this.scale == that.scale;
077        }
078        return false;
079    }
080}
081
082// End DecimalType.java