Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.derby.iapi.types
Class BigIntegerDecimal  view BigIntegerDecimal download BigIntegerDecimal.java

java.lang.Object
  extended byorg.apache.derby.iapi.types.DataType
      extended byorg.apache.derby.iapi.types.NumberDataType
          extended byorg.apache.derby.iapi.types.BinaryDecimal
              extended byorg.apache.derby.iapi.types.BigIntegerDecimal
All Implemented Interfaces:
CloneableObject, DataValueDescriptor, java.io.Externalizable, org.apache.derby.iapi.services.io.Formatable, NumberDataValue, Orderable, java.io.Serializable, org.apache.derby.iapi.services.io.Storable, org.apache.derby.iapi.services.io.TypedFormat, VariableSizeDataValue

public final class BigIntegerDecimal
extends BinaryDecimal

DECIMAL support using the immutable java.math.BigInteger to perform arithmetic and conversions. Extends BinaryDecimal to use the base support of that class. J2ME/CDC/Foundation includes BigInteger. A BigInteger is used in calculations etc. to represent the integral unscaled value. It is simply created from new BigInteger(data2c). No additional instance fields are used by this class, a possible enhancement would be to keep the BigInteger around but would require calls from the parent class to reset state etc.


Field Summary
protected  byte[] data2c
          The unscaled value as a binary two's complement array.
private static java.math.BigInteger MAXLONG_PLUS_ONE
           
private static java.math.BigInteger MINLONG_MINUS_ONE
           
protected  int sqlScale
          The SQL scale, zero or positive, of the value
private static java.math.BigInteger TEN
           
 
Fields inherited from class org.apache.derby.iapi.types.NumberDataType
ZERO_DECIMAL
 
Fields inherited from interface org.apache.derby.iapi.types.VariableSizeDataValue
IGNORE_PRECISION
 
Fields inherited from interface org.apache.derby.iapi.types.NumberDataValue
MAX_DECIMAL_PRECISION_SCALE, MIN_DECIMAL_DIVIDE_SCALE
 
Fields inherited from interface org.apache.derby.iapi.types.Orderable
ORDER_OP_EQUALS, ORDER_OP_GREATEROREQUALS, ORDER_OP_GREATERTHAN, ORDER_OP_LESSOREQUALS, ORDER_OP_LESSTHAN
 
Constructor Summary
BigIntegerDecimal()
           
 
Method Summary
 NumberDataValue divide(NumberDataValue dividend, NumberDataValue divisor, NumberDataValue result)
          This method implements the / operator for TINYINT, SMALLINT and INTEGER.
 NumberDataValue divide(NumberDataValue dividend, NumberDataValue divisor, NumberDataValue result, int scale)
          This method implements the / operator for BigDecimal/BigDecimal
 NumberDataValue divideNN(NumberDataValue left, NumberDataValue right, NumberDataValue result, int scale)
          Divide two non-null NumberDataValues using DECIMAL arithmetic.
 int estimateMemoryUsage()
          Estimate the memory usage in bytes of the data value and the overhead of the class.
private  BigIntegerDecimal getBID(DataValueDescriptor value)
          Obtain a BinaryDecimal that represents the passed in value.
 byte getByte()
          Return a byte from this value.
 DataValueDescriptor getClone()
          Clone this DataValueDescriptor.
 int getDecimalValuePrecision()
          Return the SQL precision of this value.
 int getDecimalValueScale()
          Return the SQL scale of this value, number of digits after the decimal point, or zero for a whole number.
 double getDouble()
          Gets the value in the data value descriptor as a double.
 float getFloat()
          Gets the value in the data value descriptor as a float.
 int getInt()
          Return a int from this value.
 int getLength()
          Gets the length of the data value.
 long getLong()
          Gets the value in the data value descriptor as a long.
 DataValueDescriptor getNewNull()
          Get a new null value of the same type as this data value.
 short getShort()
          Return a short from this value.
 java.lang.String getString()
          Gets the value in the data value descriptor as a String.
 int getTypeFormatId()
          Return my format identifier.
 java.lang.String getTypeName()
          Get the SQL name of the datatype
 int hashCode()
          Get a value that represents this Object, as uniquely as possible within the confines of an int.
protected  boolean isNegative()
          The isNegative abstract method.
 boolean isNull()
          see if the decimal value is null.
 NumberDataValue minus(NumberDataValue result)
          Negate the number.
 NumberDataValue minus(NumberDataValue left, NumberDataValue right, NumberDataValue result)
          This method implements the - operator for TINYINT, SMALLINT and INTEGER.
 NumberDataValue minusNN(NumberDataValue left, NumberDataValue right, NumberDataValue result)
          Implement subtraction using addition and negation of the right value.
 void normalize(DataTypeDescriptor desiredType, DataValueDescriptor source)
          Normalize the source value to this type described by this class and the passed in DataTypeDescriptor.
 NumberDataValue plus(NumberDataValue addend1, NumberDataValue addend2, NumberDataValue result)
          This method implements the + operator for DECIMAL.
 NumberDataValue plusNN(NumberDataValue left, NumberDataValue right, NumberDataValue result)
          Add two non-null NumberDataValues using DECIMAL arithmetic.
 void readExternal(java.io.ObjectInput in)
          Note the use of data2c: we reuse the array if the incoming array is the same length or smaller than the array length.
 void readExternalFromArray(org.apache.derby.iapi.services.io.ArrayInputStream in)
          Read the DataValueDescriptor from the stream.
private static java.math.BigInteger rescale(java.math.BigInteger bi, int deltaScale)
          Rescale a BigInteger, a positive delta means the scale is increased, zero no change and negative decrease of scale.
 void restoreToNull()
          Restore this object to its (SQL)null value.
protected  void setFrom(DataValueDescriptor dvd)
          Set this DECIMAL value from another DataValueDescriptor
 void setValue(double theValue)
          Convert from a double, normalize and then convert as a String.
 void setValue(float theValue)
          Convert from a float, normalize and then convert as a String.
 void setValue(int theValue)
          Set the value from an int, just copy 'byte-by-byte' from the int to a four byte array.
 void setValue(long theValue)
          Set the value from a long.
 void setValue(java.lang.Number theValue)
          Called when setting a DECIMAL value internally or from through a procedure or function.
 void setValue(java.lang.String theValue)
          Set the value from a String, the format is nnnn Scale always set to zero.
 void setValueFromResultSet(java.sql.ResultSet resultSet, int colNumber, boolean isNullable)
          Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.
 DataValueDescriptor setWidth(int desiredPrecision, int desiredScale, boolean errorOnTrunc)
           
 NumberDataValue times(NumberDataValue left, NumberDataValue right, NumberDataValue result)
          The SQL * operator.
 NumberDataValue timesNN(NumberDataValue left, NumberDataValue right, NumberDataValue result)
          Multiple two non-null NumberDataValues using DECIMAL arithmetic.
 java.lang.String toString()
          Convert this Object to a human-readable String.
protected  int typeCompare(DataValueDescriptor arg)
          Compare two non-null NumberDataValues using DECIMAL arithmetic.
 int typePrecedence()
          Each built-in type in JSQL has a precedence.
 int typeToBigDecimal()
          DECIMAL implementation.
 void writeExternal(java.io.ObjectOutput out)
          Distill the Decimal to a byte array and Write out: scale (unsigned byte) length of byte array the byte array
 
Methods inherited from class org.apache.derby.iapi.types.NumberDataType
absolute, compare, compare, mod, normalizeDOUBLE, normalizeREAL, normalizeREAL, objectNull, setBigDecimal, setValue, setValue, sqrt
 
Methods inherited from class org.apache.derby.iapi.types.DataType
checkHostVariable, cloneObject, coalesce, dataTypeConversion, equals, equals, flip, genericSetObject, getBoolean, getBytes, getDate, getNationalString, getObject, getStream, getTime, getTimestamp, greaterOrEquals, greaterThan, in, invalidFormat, isNotNull, isNullOp, lessOrEquals, lessThan, notEquals, outOfRange, setInto, setInto, setObjectForCast, setToNull, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, throwLangSetMismatch, throwLangSetMismatch
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.derby.iapi.types.DataValueDescriptor
checkHostVariable, coalesce, equals, getBoolean, getBytes, getDate, getObject, getStream, getTime, getTimestamp, greaterOrEquals, greaterThan, in, isNotNull, isNullOp, lessOrEquals, lessThan, notEquals, setInto, setInto, setObjectForCast, setToNull, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue
 

Field Detail

TEN

private static final java.math.BigInteger TEN

MAXLONG_PLUS_ONE

private static final java.math.BigInteger MAXLONG_PLUS_ONE

MINLONG_MINUS_ONE

private static final java.math.BigInteger MINLONG_MINUS_ONE

data2c

protected byte[] data2c
The unscaled value as a binary two's complement array.


sqlScale

protected int sqlScale
The SQL scale, zero or positive, of the value

Constructor Detail

BigIntegerDecimal

public BigIntegerDecimal()
Method Detail

getNewNull

public DataValueDescriptor getNewNull()
Description copied from interface: DataValueDescriptor
Get a new null value of the same type as this data value.


getLong

public long getLong()
             throws org.apache.derby.iapi.error.StandardException
Description copied from interface: DataValueDescriptor
Gets the value in the data value descriptor as a long. Throws an exception if the data value is not a long.

Specified by:
getLong in interface DataValueDescriptor
Overrides:
getLong in class DataType

getFloat

public float getFloat()
               throws org.apache.derby.iapi.error.StandardException
Description copied from interface: DataValueDescriptor
Gets the value in the data value descriptor as a float. Throws an exception if the data value is not a float.

Specified by:
getFloat in interface DataValueDescriptor
Overrides:
getFloat in class DataType

getDouble

public double getDouble()
                 throws org.apache.derby.iapi.error.StandardException
Description copied from interface: DataValueDescriptor
Gets the value in the data value descriptor as a double. Throws an exception if the data value is not a double.

Specified by:
getDouble in interface DataValueDescriptor
Overrides:
getDouble in class DataType

setValue

public void setValue(java.lang.String theValue)
              throws org.apache.derby.iapi.error.StandardException
Set the value from a String, the format is nnnn Scale always set to zero.

Specified by:
setValue in interface DataValueDescriptor
Overrides:
setValue in class DataType

getString

public java.lang.String getString()
Description copied from interface: DataValueDescriptor
Gets the value in the data value descriptor as a String. Throws an exception if the data value is not a string.


getDecimalValuePrecision

public int getDecimalValuePrecision()
Return the SQL precision of this value.

Specified by:
getDecimalValuePrecision in interface NumberDataValue
Overrides:
getDecimalValuePrecision in class NumberDataType

typeCompare

protected int typeCompare(DataValueDescriptor arg)
                   throws org.apache.derby.iapi.error.StandardException
Compare two non-null NumberDataValues using DECIMAL arithmetic.

Specified by:
typeCompare in class NumberDataType

plusNN

public NumberDataValue plusNN(NumberDataValue left,
                              NumberDataValue right,
                              NumberDataValue result)
                       throws org.apache.derby.iapi.error.StandardException
Add two non-null NumberDataValues using DECIMAL arithmetic. Uses add() to perform the calculation.

Specified by:
plusNN in class BinaryDecimal

minus

public NumberDataValue minus(NumberDataValue result)
                      throws org.apache.derby.iapi.error.StandardException
Negate the number.


timesNN

public NumberDataValue timesNN(NumberDataValue left,
                               NumberDataValue right,
                               NumberDataValue result)
                        throws org.apache.derby.iapi.error.StandardException
Multiple two non-null NumberDataValues using DECIMAL arithmetic. Uses BigInteger.multipy() to perform the calculation. Simply multiply the unscaled values and add the scales, proof: left * right = (left_unscaled * 10^-left_scale) * (right_unscaled * 10^-right_scale) = (left_unscaled * 10^-left_scale) * (right_unscaled * 10^-right_scale) = (left_unscaled * right_unscaled) * 10^-(left_scale + right_scale)

Specified by:
timesNN in class BinaryDecimal

divideNN

public NumberDataValue divideNN(NumberDataValue left,
                                NumberDataValue right,
                                NumberDataValue result,
                                int scale)
                         throws org.apache.derby.iapi.error.StandardException
Divide two non-null NumberDataValues using DECIMAL arithmetic. Uses divide() to perform the calculation. Simply multiply the unscaled values and subtract the scales, proof: left / right = (left_unscaled * 10^-left_scale) / (right_unscaled * 10^-right_scale) = (left_unscaled / right_unscaled) * (10^-left_scale / 10^-right_scale) = (left_unscaled / right_unscaled) * (10^-(left_scale-right_scale))

Specified by:
divideNN in class BinaryDecimal

normalize

public void normalize(DataTypeDescriptor desiredType,
                      DataValueDescriptor source)
               throws org.apache.derby.iapi.error.StandardException
Description copied from interface: DataValueDescriptor
Normalize the source value to this type described by this class and the passed in DataTypeDescriptor. The type of the DataTypeDescriptor must match this class.

Specified by:
normalize in interface DataValueDescriptor
Overrides:
normalize in class DataType

setWidth

public DataValueDescriptor setWidth(int desiredPrecision,
                                    int desiredScale,
                                    boolean errorOnTrunc)
                             throws org.apache.derby.iapi.error.StandardException

getBID

private BigIntegerDecimal getBID(DataValueDescriptor value)
                          throws org.apache.derby.iapi.error.StandardException
Obtain a BinaryDecimal that represents the passed in value.


rescale

private static java.math.BigInteger rescale(java.math.BigInteger bi,
                                            int deltaScale)
Rescale a BigInteger, a positive delta means the scale is increased, zero no change and negative decrease of scale. It is up to the caller to manage the actual scale of the value, e.g. don't allow the scale to go negative.


toString

public java.lang.String toString()
Description copied from class: java.lang.Object
Convert this Object to a human-readable String. There are no limits placed on how long this String should be or what it should contain. We suggest you make it as intuitive as possible to be able to place it into System.out.println() 55 and such.

It is typical, but not required, to ensure that this method never completes abruptly with a java.lang.RuntimeException.

This method will be called when performing string concatenation with this object. If the result is null, string concatenation will instead use "null".

The default implementation returns getClass().getName() + "@" + Integer.toHexString(hashCode()).


typeToBigDecimal

public final int typeToBigDecimal()
DECIMAL implementation. Use DECIMAL to indicate to self that another passed in value is an instance of this type.

Specified by:
typeToBigDecimal in interface DataValueDescriptor
Overrides:
typeToBigDecimal in class NumberDataType

typePrecedence

public final int typePrecedence()
Description copied from interface: DataValueDescriptor
Each built-in type in JSQL has a precedence. This precedence determines how to do type promotion when using binary operators. For example, float has a higher precedence than int, so when adding an int to a float, the result type is float. The precedence for some types is arbitrary. For example, it doesn't matter what the precedence of the boolean type is, since it can't be mixed with other types. But the precedence for the number types is critical. The SQL standard requires that exact numeric types be promoted to approximate numeric when one operator uses both. Also, the precedence is arranged so that one will not lose precision when promoting a type.

Specified by:
typePrecedence in interface DataValueDescriptor
Overrides:
typePrecedence in class DataType

getTypeName

public final java.lang.String getTypeName()
Description copied from interface: DataValueDescriptor
Get the SQL name of the datatype

Specified by:
getTypeName in interface DataValueDescriptor

getTypeFormatId

public final int getTypeFormatId()
Return my format identifier.

Specified by:
getTypeFormatId in interface org.apache.derby.iapi.services.io.TypedFormat

isNull

public boolean isNull()
see if the decimal value is null.

Specified by:
isNull in interface org.apache.derby.iapi.services.io.Storable

restoreToNull

public void restoreToNull()
Description copied from interface: org.apache.derby.iapi.services.io.Storable
Restore this object to its (SQL)null value.

Specified by:
restoreToNull in interface org.apache.derby.iapi.services.io.Storable

isNegative

protected boolean isNegative()
Description copied from class: NumberDataType
The isNegative abstract method. Checks to see if this.value is negative. To be implemented by each NumberDataType.

Specified by:
isNegative in class NumberDataType

setValue

public void setValue(long theValue)
Set the value from a long.

Specified by:
setValue in interface DataValueDescriptor
Overrides:
setValue in class DataType

setValue

public final void setValue(int theValue)
Set the value from an int, just copy 'byte-by-byte' from the int to a four byte array. Then reduce.

Specified by:
setValue in interface DataValueDescriptor
Overrides:
setValue in class DataType

setValue

public final void setValue(double theValue)
                    throws org.apache.derby.iapi.error.StandardException
Convert from a double, normalize and then convert as a String.

Specified by:
setValue in interface DataValueDescriptor
Overrides:
setValue in class DataType

setValue

public final void setValue(float theValue)
                    throws org.apache.derby.iapi.error.StandardException
Convert from a float, normalize and then convert as a String.

Specified by:
setValue in interface DataValueDescriptor
Overrides:
setValue in class DataType

setValue

public void setValue(java.lang.Number theValue)
              throws org.apache.derby.iapi.error.StandardException
Called when setting a DECIMAL value internally or from through a procedure or function. Handles long in addition to BigDecimal to handle identity being stored as a long but returned as a DECIMAL.

Specified by:
setValue in interface NumberDataValue
Overrides:
setValue in class NumberDataType

setFrom

protected void setFrom(DataValueDescriptor dvd)
                throws org.apache.derby.iapi.error.StandardException
Set this DECIMAL value from another DataValueDescriptor

Overrides:
setFrom in class DataType

getInt

public final int getInt()
                 throws org.apache.derby.iapi.error.StandardException
Return a int from this value.

Specified by:
getInt in interface DataValueDescriptor
Overrides:
getInt in class DataType

getByte

public final byte getByte()
                   throws org.apache.derby.iapi.error.StandardException
Return a byte from this value.

Specified by:
getByte in interface DataValueDescriptor
Overrides:
getByte in class DataType

getShort

public final short getShort()
                     throws org.apache.derby.iapi.error.StandardException
Return a short from this value.

Specified by:
getShort in interface DataValueDescriptor
Overrides:
getShort in class DataType

plus

public final NumberDataValue plus(NumberDataValue addend1,
                                  NumberDataValue addend2,
                                  NumberDataValue result)
                           throws org.apache.derby.iapi.error.StandardException
This method implements the + operator for DECIMAL.

Specified by:
plus in interface NumberDataValue
Overrides:
plus in class NumberDataType

times

public final NumberDataValue times(NumberDataValue left,
                                   NumberDataValue right,
                                   NumberDataValue result)
                            throws org.apache.derby.iapi.error.StandardException
Description copied from interface: NumberDataValue
The SQL * operator.

Specified by:
times in interface NumberDataValue

divide

public NumberDataValue divide(NumberDataValue dividend,
                              NumberDataValue divisor,
                              NumberDataValue result)
                       throws org.apache.derby.iapi.error.StandardException
Description copied from class: NumberDataType
This method implements the / operator for TINYINT, SMALLINT and INTEGER. Specialized methods are not required for TINYINT and SMALLINT as the Java virtual machine always executes byte and int division as integer.

Specified by:
divide in interface NumberDataValue
Overrides:
divide in class NumberDataType

divide

public final NumberDataValue divide(NumberDataValue dividend,
                                    NumberDataValue divisor,
                                    NumberDataValue result,
                                    int scale)
                             throws org.apache.derby.iapi.error.StandardException
This method implements the / operator for BigDecimal/BigDecimal

Specified by:
divide in interface NumberDataValue
Overrides:
divide in class NumberDataType

minus

public final NumberDataValue minus(NumberDataValue left,
                                   NumberDataValue right,
                                   NumberDataValue result)
                            throws org.apache.derby.iapi.error.StandardException
Description copied from class: NumberDataType
This method implements the - operator for TINYINT, SMALLINT and INTEGER.

Specified by:
minus in interface NumberDataValue
Overrides:
minus in class NumberDataType

minusNN

public NumberDataValue minusNN(NumberDataValue left,
                               NumberDataValue right,
                               NumberDataValue result)
                        throws org.apache.derby.iapi.error.StandardException
Implement subtraction using addition and negation of the right value.


getDecimalValueScale

public int getDecimalValueScale()
Return the SQL scale of this value, number of digits after the decimal point, or zero for a whole number.

Specified by:
getDecimalValueScale in interface NumberDataValue
Overrides:
getDecimalValueScale in class NumberDataType

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Distill the Decimal to a byte array and Write out:
  • scale (unsigned byte)
  • length of byte array
  • the byte array

Specified by:
writeExternal in interface java.io.Externalizable

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException
Note the use of data2c: we reuse the array if the incoming array is the same length or smaller than the array length.

Specified by:
readExternal in interface java.io.Externalizable

readExternalFromArray

public void readExternalFromArray(org.apache.derby.iapi.services.io.ArrayInputStream in)
                           throws java.io.IOException
Description copied from interface: DataValueDescriptor
Read the DataValueDescriptor from the stream.

Initialize the data value by reading it's values from the ArrayInputStream. This interface is provided as a way to achieve possible performance enhancement when reading an array can be optimized over reading from a generic stream from readExternal().

Specified by:
readExternalFromArray in interface DataValueDescriptor

getLength

public final int getLength()
Description copied from interface: DataValueDescriptor
Gets the length of the data value. The meaning of this is implementation-dependent. For string types, it is the number of characters in the string. For numeric types, it is the number of bytes used to store the number. This is the actual length of this value, not the length of the type it was defined as. For example, a VARCHAR value may be shorter than the declared VARCHAR (maximum) length.

Specified by:
getLength in interface DataValueDescriptor

getClone

public DataValueDescriptor getClone()
Description copied from interface: DataValueDescriptor
Clone this DataValueDescriptor. Results in a new object that has the same value as this but can be modified independently.

Specified by:
getClone in interface DataValueDescriptor

setValueFromResultSet

public void setValueFromResultSet(java.sql.ResultSet resultSet,
                                  int colNumber,
                                  boolean isNullable)
                           throws org.apache.derby.iapi.error.StandardException,
                                  java.sql.SQLException
Description copied from interface: DataValueDescriptor
Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.

Specified by:
setValueFromResultSet in interface DataValueDescriptor

estimateMemoryUsage

public int estimateMemoryUsage()
Description copied from interface: DataValueDescriptor
Estimate the memory usage in bytes of the data value and the overhead of the class.

Specified by:
estimateMemoryUsage in interface DataValueDescriptor

hashCode

public int hashCode()
Description copied from class: java.lang.Object
Get a value that represents this Object, as uniquely as possible within the confines of an int.

There are some requirements on this method which subclasses must follow:

  • Semantic equality implies identical hashcodes. In other words, if a.equals(b) is true, then a.hashCode() == b.hashCode() must be as well. However, the reverse is not necessarily true, and two objects may have the same hashcode without being equal.
  • It must be consistent. Whichever value o.hashCode() returns on the first invocation must be the value returned on all later invocations as long as the object exists. Notice, however, that the result of hashCode may change between separate executions of a Virtual Machine, because it is not invoked on the same object.

Notice that since hashCode is used in java.util.Hashtable and other hashing classes, a poor implementation will degrade the performance of hashing (so don't blindly implement it as returning a constant!). Also, if calculating the hash is time-consuming, a class may consider caching the results.

The default implementation returns System.identityHashCode(this)