org.apache.derby.iapi.types
Class BinaryDecimal

java.lang.Object
  extended by org.apache.derby.iapi.types.DataType
      extended by org.apache.derby.iapi.types.NumberDataType
          extended by org.apache.derby.iapi.types.BinaryDecimal
All Implemented Interfaces:
java.io.Externalizable, java.io.Serializable, java.lang.Comparable, Formatable, Storable, TypedFormat, DataValueDescriptor, NumberDataValue, Orderable, VariableSizeDataValue
Direct Known Subclasses:
BigIntegerDecimal

abstract class BinaryDecimal
extends NumberDataType
implements VariableSizeDataValue

SQL DECIMAL using raw data. Provides the basis for the BigIntegerDecimal implementation.

The on-disk format must match the SQLDecimal format so that databases are portable across J2ME and J2SE environments.

The format of the byte array is defined by the return of the java.math.BigInteger.toByteArray:, extracted here. Returns a byte array containing the two's-complement representation of this BigInteger. The byte array will be in big-endian byte-order: the most significant byte is in the zeroth element. This is the format for DECIMAL even if BigINteger is not available, e.g. OSGi ee.minimum.


Field Summary
protected  byte[] data2c
          The unscaled value as a binary two's complement array.
private static byte[] ONE_2C
          An unscaled value of 1 in two's complement
protected  int sqlScale
          The SQL scale, zero or positive, of the value
 
Fields inherited from class org.apache.derby.iapi.types.NumberDataType
MAXLONG_PLUS_ONE, MINLONG_MINUS_ONE, 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.DataValueDescriptor
UNKNOWN_LOGICAL_LENGTH
 
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
BinaryDecimal()
           
 
Method Summary
 DataValueDescriptor cloneValue(boolean forceMaterialization)
          Clone this DataValueDescriptor.
 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
abstract  NumberDataValue divideNN(NumberDataValue dividend, NumberDataValue divisor, NumberDataValue result, int scale)
          Divide two non-nullable values using DECIMAL arithmetic.
 int estimateMemoryUsage()
          Estimate the memory usage in bytes of the data value and the overhead of the class.
 byte getByte()
          Return a byte from this value.
 int getDecimalValueScale()
          Return the SQL scale of this value, number of digits after the decimal point, or zero for a whole number.
 int getInt()
          Return a int from this value.
 int getLength()
          Gets the length of the data value.
 short getShort()
          Return a short from this value.
 int getTypeFormatId()
          Return my format identifier.
 java.lang.String getTypeName()
          Get the SQL name of the datatype
 int hashCode()
           
protected  boolean isNegative()
          The isNegative abstract method.
 boolean isNull()
          see if the decimal value is null.
 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.
 NumberDataValue plus(NumberDataValue addend1, NumberDataValue addend2, NumberDataValue result)
          This method implements the + operator for DECIMAL.
abstract  NumberDataValue plusNN(NumberDataValue addend1, NumberDataValue addend2, NumberDataValue result)
          Add two non-nullable values 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(ArrayInputStream in)
          Read the DataValueDescriptor from the stream.
private static byte[] reduceBytes2c(byte[] rd, int offset, int dataLength)
          Compress the passed in byte array so that leading 0x00 and 0xff are removed when possible.
 void restoreToNull()
          Restore this object to its (SQL)null value.
private  void setCoreValue(double theValue)
           
protected  void setFrom(DataValueDescriptor dvd)
          Set this DECIMAL value from another DataValueDescriptor
 void setValue(boolean theValue)
          Set the value from a boolean
 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 setValueFromResultSet(java.sql.ResultSet resultSet, int colNumber, boolean isNullable)
          Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.
 NumberDataValue times(NumberDataValue left, NumberDataValue right, NumberDataValue result)
          The SQL * operator.
abstract  NumberDataValue timesNN(NumberDataValue left, NumberDataValue right, NumberDataValue result)
          Multiple two non-nullable values 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, getDecimalValuePrecision, mod, normalizeDOUBLE, normalizeREAL, normalizeREAL, objectNull, setBigDecimal, setObject, setValue, setValue, sqrt, typeCompare
 
Methods inherited from class org.apache.derby.iapi.types.DataType
checkHostVariable, cloneHolder, coalesce, compare, compare, compareTo, dataTypeConversion, equals, equals, flip, genericSetObject, getBoolean, getBytes, getDate, getDouble, getFloat, getLong, getNationalString, getObject, getStream, getTime, getTimestamp, getTraceString, getTypeName, greaterOrEquals, greaterThan, hasStream, in, invalidFormat, isNotNull, isNullOp, lessOrEquals, lessThan, normalize, notEquals, outOfRange, recycle, 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, toString, wait, wait, wait
 
Methods inherited from interface org.apache.derby.iapi.types.VariableSizeDataValue
setWidth
 
Methods inherited from interface org.apache.derby.iapi.types.NumberDataValue
minus
 
Methods inherited from interface org.apache.derby.iapi.types.DataValueDescriptor
checkHostVariable, cloneHolder, coalesce, compare, compare, equals, getBoolean, getBytes, getDate, getDouble, getFloat, getLong, getNewNull, getObject, getStream, getString, getTime, getTimestamp, getTraceString, greaterOrEquals, greaterThan, hasStream, in, isNotNull, isNullOp, lessOrEquals, lessThan, normalize, notEquals, recycle, setInto, setInto, setObjectForCast, setToNull, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue
 

Field Detail

ONE_2C

private static final byte[] ONE_2C
An unscaled value of 1 in two's complement


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

BinaryDecimal

BinaryDecimal()
Method Detail

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
Returns:
Types.CHAR for String conversion through getString Types.DECIMAL for BigDecimal through getObject or Types.BIGINT for long conversion through getLong

typePrecedence

public final int typePrecedence()
Description copied from class: DataType
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
Returns:
The precedence of this type.
See Also:
DataValueDescriptor.typePrecedence()

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
Returns:
The SQL name of the datatype

getTypeFormatId

public final int getTypeFormatId()
Return my format identifier.

Specified by:
getTypeFormatId in interface TypedFormat
Returns:
The identifier. (A UUID stuffed in an array of 16 bytes).
See Also:
TypedFormat.getTypeFormatId()

isNull

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

Specified by:
isNull in interface Storable
Returns:
true if the value is null and false otherwise.

restoreToNull

public void restoreToNull()
Description copied from interface: Storable
Restore this object to its (SQL)null value.

Specified by:
restoreToNull in interface 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
Returns:
A boolean. If this.value is negative, return true. For positive values or null, return false.

setValue

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

Specified by:
setValue in interface DataValueDescriptor
Overrides:
setValue in class DataType
Parameters:
theValue - The value to set this DataValueDescriptor to

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
Parameters:
theValue - The value to set this DataValueDescriptor to
See Also:
NumberDataValue.setValue(java.lang.Number)

setValue

public void setValue(boolean theValue)
Set the value from a boolean

Specified by:
setValue in interface DataValueDescriptor
Overrides:
setValue in class DataType
Parameters:
theValue - Contains the boolean value to set this to

setValue

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

Specified by:
setValue in interface DataValueDescriptor
Overrides:
setValue in class DataType
Parameters:
theValue - The value to set this DataValueDescriptor to
Throws:
StandardException - Thrown on error

setValue

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

Specified by:
setValue in interface DataValueDescriptor
Overrides:
setValue in class DataType
Parameters:
theValue - The value to set this DataValueDescriptor to
Throws:
StandardException - Thrown on error

setCoreValue

private void setCoreValue(double theValue)
                   throws StandardException
Throws:
StandardException

setValue

public void setValue(java.lang.Number theValue)
              throws 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
Parameters:
theValue - An Number containing the value to set this NumberDataValue to. Null means set the value to SQL null.
Throws:
StandardException - Thrown on error
See Also:
NumberDataValue.setValue(java.lang.Number)

setFrom

protected void setFrom(DataValueDescriptor dvd)
                throws StandardException
Set this DECIMAL value from another DataValueDescriptor

Overrides:
setFrom in class DataType
Parameters:
dvd - The DataValueDescriptor that holds the value to which we want to set this DataValueDescriptor's value.
Throws:
StandardException

getInt

public final int getInt()
                 throws StandardException
Return a int from this value.

Specified by:
getInt in interface DataValueDescriptor
Overrides:
getInt in class DataType
Returns:
The data value as a int.
Throws:
StandardException - this value is out of range for an int

getByte

public final byte getByte()
                   throws StandardException
Return a byte from this value.

Specified by:
getByte in interface DataValueDescriptor
Overrides:
getByte in class DataType
Returns:
The data value as a byte.
Throws:
StandardException - this value is out of range for a short

getShort

public final short getShort()
                     throws StandardException
Return a short from this value.

Specified by:
getShort in interface DataValueDescriptor
Overrides:
getShort in class DataType
Returns:
The data value as a short.
Throws:
StandardException - this value is out of range for a short

plus

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

Specified by:
plus in interface NumberDataValue
Overrides:
plus in class NumberDataType
Parameters:
addend1 - One of the addends
addend2 - The other addend
result - The result of a previous call to this method, null if not called yet
Returns:
A BinaryDecimal containing the result of the addition
Throws:
StandardException - Thrown on error

times

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

Specified by:
times in interface NumberDataValue
Parameters:
left - The left operand
right - The right operand
result - The result of the previous call to this method, null if not called yet.
Returns:
left * right
Throws:
StandardException - Thrown on error, if result is non-null then its value will be unchanged.

divide

public NumberDataValue divide(NumberDataValue dividend,
                              NumberDataValue divisor,
                              NumberDataValue result)
                       throws 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
Parameters:
dividend - The numerator
divisor - The denominator
result - The result of a previous call to this method, null if not called yet
Returns:
A SQLInteger containing the result of the division
Throws:
StandardException - Thrown on error

divide

public final NumberDataValue divide(NumberDataValue dividend,
                                    NumberDataValue divisor,
                                    NumberDataValue result,
                                    int scale)
                             throws StandardException
This method implements the / operator for BigDecimal/BigDecimal

Specified by:
divide in interface NumberDataValue
Overrides:
divide in class NumberDataType
Parameters:
dividend - The numerator
divisor - The denominator
result - The result of a previous call to this method, null if not called yet
scale - The result scale, if < 0, calculate the scale according to the actual values' sizes
Returns:
A SQLDecimal containing the result of the division
Throws:
StandardException - Thrown on error

minus

public final NumberDataValue minus(NumberDataValue left,
                                   NumberDataValue right,
                                   NumberDataValue result)
                            throws 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
Parameters:
left - The value to be subtracted from
right - The value to be subtracted
result - The result of a previous call to this method, null if not called yet
Returns:
A SQLInteger containing the result of the subtraction
Throws:
StandardException - Thrown on error

minusNN

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

Throws:
StandardException

timesNN

public abstract NumberDataValue timesNN(NumberDataValue left,
                                        NumberDataValue right,
                                        NumberDataValue result)
                                 throws StandardException
Multiple two non-nullable values using DECIMAL arithmetic.

Throws:
StandardException

plusNN

public abstract NumberDataValue plusNN(NumberDataValue addend1,
                                       NumberDataValue addend2,
                                       NumberDataValue result)
                                throws StandardException
Add two non-nullable values using DECIMAL arithmetic. For subclasses of BinaryDecimal, any implementation must handle the result and addend2 (right) being references to the same object.

Throws:
StandardException

divideNN

public abstract NumberDataValue divideNN(NumberDataValue dividend,
                                         NumberDataValue divisor,
                                         NumberDataValue result,
                                         int scale)
                                  throws StandardException
Divide two non-nullable values using DECIMAL arithmetic.

Throws:
StandardException

reduceBytes2c

private static byte[] reduceBytes2c(byte[] rd,
                                    int offset,
                                    int dataLength)
Compress the passed in byte array so that leading 0x00 and 0xff are removed when possible. E.g. 0x00000453 ->>> 0x0453 0xfffffff2 ->>> 0xf2 0xff192312 ->>> 0xff192312 (unchanged) 0xffff8039 ->>> 0x8039 data2c is set to the compressed value.

Parameters:
dataLength - Valid length of data in data2c.

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:

Specified by:
writeExternal in interface java.io.Externalizable
Throws:
java.io.IOException

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
Throws:
java.io.IOException
See Also:
Externalizable.readExternal(java.io.ObjectInput)

readExternalFromArray

public void readExternalFromArray(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
Parameters:
in - The array stream positioned at the beginning of the byte stream to read from.
Throws:
java.io.IOException - Usual error is if you try to read past limit on the stream.

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
Returns:
The length of the data value

cloneValue

public DataValueDescriptor cloneValue(boolean forceMaterialization)
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.

Even though the objects can be modified independently regardless of the value of forceMaterialization, both the clone and the original may be dependent on the store state if forceMaterialization is set to false. An example is if you need to access the value you just read using cloneValue after the current transaction has ended, or after the source result set has been closed.

Specified by:
cloneValue in interface DataValueDescriptor
Parameters:
forceMaterialization - any streams representing the data value will be materialized if true, the data value will be kept as a stream if possible if false
Returns:
A clone of the DataValueDescriptor with the same initial value as this.
See Also:
DataValueDescriptor.cloneValue(boolean)

setValueFromResultSet

public void setValueFromResultSet(java.sql.ResultSet resultSet,
                                  int colNumber,
                                  boolean isNullable)
                           throws 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
Parameters:
resultSet - The specified ResultSet.
colNumber - The 1-based column # into the resultSet.
isNullable - Whether or not the column is nullable (No need to call wasNull() if not)
Throws:
StandardException - Thrown on error
java.sql.SQLException - Error accessing the result set

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
Returns:
the estimated memory usage

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

Built on Thu 2010-12-23 20:49:13+0000, from revision ???

Apache Derby V10.6 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.