001/*
002// Licensed to Julian Hyde under one or more contributor license
003// agreements. See the NOTICE file distributed with this work for
004// additional information regarding copyright ownership.
005//
006// Julian Hyde licenses this file to you under the Apache License,
007// Version 2.0 (the "License"); you may not use this file except in
008// compliance with the License. You may obtain a copy of the License at:
009//
010// http://www.apache.org/licenses/LICENSE-2.0
011//
012// Unless required by applicable law or agreed to in writing, software
013// distributed under the License is distributed on an "AS IS" BASIS,
014// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015// See the License for the specific language governing permissions and
016// limitations under the License.
017*/
018package org.olap4j.type;
019
020/**
021 * Subclass of {@link NumericType} which guarantees fixed number of decimal
022 * places. In particular, a decimal with zero scale is an integer.
023 *
024 * @author jhyde
025 * @since May 3, 2005
026 */
027public class DecimalType extends NumericType {
028    private final int precision;
029    private final int scale;
030
031    /**
032     * Creates a decimal type with precision and scale.
033     *
034     * <p>Examples:<ul>
035     * <li>123.45 has precision 5, scale 2.
036     * <li>12,345,000 has precision 5, scale -3.
037     * </ul>
038     *
039     * <p>The largest value is 10 ^ (precision - scale). Hence the largest
040     * <code>DECIMAL(5, -3)</code> value is 10 ^ 8.
041     *
042     * @param precision Maximum number of decimal digits which a value of
043     *   this type can have.
044     *   Must be greater than zero.
045     *   Use {@link Integer#MAX_VALUE} if the precision is unbounded.
046     * @param scale Number of digits to the right of the decimal point.
047     */
048    public DecimalType(int precision, int scale) {
049        super();
050        assert precision > 0 : "expected precision > 0";
051        this.precision = precision;
052        this.scale = scale;
053    }
054
055    /**
056     * Returns the maximum number of decimal digits which a value of
057     * this type can have.
058     *
059     * @return maximum precision allowed for values of this type
060     */
061    public int getPrecision() {
062        return precision;
063    }
064
065    /**
066     * Returns the number of digits to the right of the decimal point.
067     *
068     * @return number of digits to the right of the decimal point
069     */
070    public int getScale() {
071        return scale;
072    }
073
074    public String toString() {
075        return precision == Integer.MAX_VALUE
076            ? "DECIMAL(" + scale + ")"
077            : "DECIMAL(" + precision + ", " + scale + ")";
078    }
079}
080
081// End DecimalType.java