001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/type/DecimalType.java#6 $
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.Util;
013    
014    /**
015     * Subclass of {@link NumericType} which guarantees fixed number of decimal
016     * places. In particular, a decimal with zero scale is an integer.
017     *
018     * @author jhyde
019     * @since May 3, 2005
020     * @version $Id: //open/mondrian/src/main/mondrian/olap/type/DecimalType.java#6 $
021     */
022    public 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