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