001 /*
002 // $Id: DecimalType.java 247 2009-06-20 05:52:40Z jhyde $
003 // This software is subject to the terms of the Eclipse Public License v1.0
004 // Agreement, available at the following URL:
005 // http://www.eclipse.org/legal/epl-v10.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 org.olap4j.type;
011
012 /**
013 * Subclass of {@link NumericType} which guarantees fixed number of decimal
014 * places. In particular, a decimal with zero scale is an integer.
015 *
016 * @author jhyde
017 * @since May 3, 2005
018 * @version $Id: DecimalType.java 247 2009-06-20 05:52:40Z jhyde $
019 */
020 public class DecimalType extends NumericType {
021 private final int precision;
022 private final int scale;
023
024 /**
025 * Creates a decimal type with precision and scale.
026 *
027 * <p>Examples:<ul>
028 * <li>123.45 has precision 5, scale 2.
029 * <li>12,345,000 has precision 5, scale -3.
030 * </ul>
031 *
032 * <p>The largest value is 10 ^ (precision - scale). Hence the largest
033 * <code>DECIMAL(5, -3)</code> value is 10 ^ 8.
034 *
035 * @param precision Maximum number of decimal digits which a value of
036 * this type can have.
037 * Must be greater than zero.
038 * Use {@link Integer#MAX_VALUE} if the precision is unbounded.
039 * @param scale Number of digits to the right of the decimal point.
040 */
041 public DecimalType(int precision, int scale) {
042 super();
043 assert precision > 0 : "expected precision > 0";
044 this.precision = precision;
045 this.scale = scale;
046 }
047
048 /**
049 * Returns the maximum number of decimal digits which a value of
050 * this type can have.
051 *
052 * @return maximum precision allowed for values of this type
053 */
054 public int getPrecision() {
055 return precision;
056 }
057
058 /**
059 * Returns the number of digits to the right of the decimal point.
060 *
061 * @return number of digits to the right of the decimal point
062 */
063 public int getScale() {
064 return scale;
065 }
066
067 public String toString() {
068 return precision == Integer.MAX_VALUE
069 ? "DECIMAL(" + scale + ")"
070 : "DECIMAL(" + precision + ", " + scale + ")";
071 }
072 }
073
074 // End DecimalType.java