Class SQLDouble

All Implemented Interfaces:
Externalizable, Serializable, Comparable, Formatable, Storable, TypedFormat, DataValueDescriptor, NumberDataValue, Orderable

public final class SQLDouble extends NumberDataType
SQLDouble satisfies the DataValueDescriptor interfaces (i.e., OrderableDataType). It implements a double column, e.g. for * storing a column value; it can be specified when constructed to not allow nulls. Nullability cannot be changed after construction, as it affects the storage size and mechanism.

Because OrderableDataType is a subtype of DataType, SQLDouble can play a role in either a DataType/Row or a OrderableDataType/Row, interchangeably.

We assume the store has a flag for nullness of the value, and simply return a 0-length array for the stored form when the value is null.

PERFORMANCE: There are likely alot of performance improvements possible for this implementation -- it new's Double more than it probably wants to.

This is modeled after SQLInteger.

We don't let doubles get constructed with NaN or Infinity values, and check for those values where they can occur on operations, so the set* operations do not check for them coming in.

See Also:
  • Field Details

    • DOUBLE_LENGTH

      static final int DOUBLE_LENGTH
      See Also:
    • BASE_MEMORY_USAGE

      private static final int BASE_MEMORY_USAGE
    • value

      private double value
    • isnull

      private boolean isnull
  • Constructor Details

  • Method Details

    • getInt

      public int getInt() throws StandardException
      Description copied from class: DataType
      Gets the value in the data value descriptor as a int. Throws an exception if the data value is not receivable as a int.
      Specified by:
      getInt in interface DataValueDescriptor
      Overrides:
      getInt in class DataType
      Returns:
      The data value as a int.
      Throws:
      StandardException - thrown on failure to convert
    • getByte

      public byte getByte() throws StandardException
      Description copied from class: DataType
      Gets the value in the data value descriptor as a byte. Throws an exception if the data value is not receivable as a byte.
      Specified by:
      getByte in interface DataValueDescriptor
      Overrides:
      getByte in class DataType
      Returns:
      The data value as a byte.
      Throws:
      StandardException - thrown on failure to convert
    • getShort

      public short getShort() throws StandardException
      Description copied from class: DataType
      Gets the value in the data value descriptor as a short. Throws an exception if the data value is not receivable as a short.
      Specified by:
      getShort in interface DataValueDescriptor
      Overrides:
      getShort in class DataType
      Returns:
      The data value as a short.
      Throws:
      StandardException - thrown on failure to convert
    • getLong

      public long getLong() throws StandardException
      Description copied from class: DataType
      Gets the value in the data value descriptor as a long. Throws an exception if the data value is not receivable as a long.
      Specified by:
      getLong in interface DataValueDescriptor
      Overrides:
      getLong in class DataType
      Returns:
      The data value as a long.
      Throws:
      StandardException - thrown on failure to convert
    • getFloat

      public float getFloat() throws StandardException
      Description copied from class: DataType
      Gets the value in the data value descriptor as a float. Throws an exception if the data value is not receivable as a float.
      Specified by:
      getFloat in interface DataValueDescriptor
      Overrides:
      getFloat in class DataType
      Returns:
      The data value as a float.
      Throws:
      StandardException - thrown on failure to convert
    • getDouble

      public double getDouble()
      Description copied from class: DataType
      Gets the value in the data value descriptor as a double. Throws an exception if the data value is not receivable as a double.
      Specified by:
      getDouble in interface DataValueDescriptor
      Overrides:
      getDouble in class DataType
      Returns:
      The data value as a double.
    • typeToBigDecimal

      public int typeToBigDecimal()
      DOUBLE implementation. Convert to a BigDecimal using getString.
      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
    • getBoolean

      public boolean getBoolean()
      Description copied from class: DataType
      Gets the value in the data value descriptor as a boolean. Throws an exception if the data value is not receivable as a boolean.
      Specified by:
      getBoolean in interface DataValueDescriptor
      Overrides:
      getBoolean in class DataType
      Returns:
      The data value as a boolean.
    • getString

      public 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.
      Returns:
      The data value as a String.
    • getObject

      public Object getObject()
      Description copied from class: DataType
      Gets the value in the data value descriptor as a int. Throws an exception if the data value is not receivable as a int.
      Specified by:
      getObject in interface DataValueDescriptor
      Overrides:
      getObject in class DataType
      Returns:
      The data value as a int.
    • setObject

      void setObject(Object theValue) throws StandardException
      Set the value from a correctly typed Double object.
      Overrides:
      setObject in class NumberDataType
      Throws:
      StandardException
    • setFrom

      protected void setFrom(DataValueDescriptor theValue) throws StandardException
      Description copied from class: DataType
      Set the value of this DataValueDescriptor based on the value of the specified DataValueDescriptor.
      Overrides:
      setFrom in class DataType
      Parameters:
      theValue - The DataValueDescriptor that holds the value to which we want to set this DataValueDescriptor's value.
      Throws:
      StandardException
    • getLength

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

      public String getTypeName()
      Description copied from interface: DataValueDescriptor
      Get the SQL name of the datatype
      Returns:
      The SQL name of the datatype
    • getTypeFormatId

      public int getTypeFormatId()
      Return my format identifier.
      Returns:
      The identifier. (A UUID stuffed in an array of 16 bytes).
      See Also:
    • isNull

      public boolean isNull()
      Description copied from interface: Storable
      Return whether the value is null or not.
      Returns:
      true if the value is null and false otherwise.
      See Also:
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Throws:
      IOException
    • readExternal

      public void readExternal(ObjectInput in) throws IOException
      Throws:
      IOException
      See Also:
    • restoreToNull

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

      protected int typeCompare(DataValueDescriptor arg) throws StandardException
      Description copied from class: NumberDataType
      Compare this (not null) to a non-null value.
      Specified by:
      typeCompare in class NumberDataType
      Throws:
      StandardException - Thrown on error
    • 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.

      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:
    • getNewNull

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

      public void setValueFromResultSet(ResultSet resultSet, int colNumber, boolean isNullable) throws StandardException, SQLException
      Description copied from interface: DataValueDescriptor
      Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.
      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
      SQLException - Thrown on error
      See Also:
    • setInto

      public final void setInto(PreparedStatement ps, int position) throws SQLException
      Set the value into a PreparedStatement.
      Specified by:
      setInto in interface DataValueDescriptor
      Overrides:
      setInto in class DataType
      Throws:
      SQLException - Error setting value in PreparedStatement
    • setInto

      public final void setInto(ResultSet rs, int position) throws SQLException, StandardException
      Set this value into a ResultSet for a subsequent ResultSet.insertRow or ResultSet.updateRow. This method will only be called for non-null values.
      Specified by:
      setInto in interface DataValueDescriptor
      Overrides:
      setInto in class DataType
      Throws:
      SQLException - thrown by the ResultSet object
      StandardException - thrown by me accessing my value.
    • setValue

      public void setValue(String theValue) throws StandardException
      Description copied from class: DataType
      Set the value of this DataValueDescriptor. At DataType level just throws an error lower classes will override
      Specified by:
      setValue in interface DataValueDescriptor
      Overrides:
      setValue in class DataType
      Parameters:
      theValue - The BigDecimal value to set this DataValueDescriptor to
      Throws:
      StandardException - throws NumberFormatException when the String format is not recognized.
    • setValue

      public void setValue(double theValue) throws StandardException
      Description copied from class: DataType
      Set the value of this DataValueDescriptor to the given double value At DataType level just throws an error lower classes will override
      Specified by:
      setValue in interface DataValueDescriptor
      Overrides:
      setValue in class DataType
      Parameters:
      theValue - The value to set this DataValueDescriptor to
      Throws:
      StandardException - on NaN or Infinite double
    • setValue

      public void setValue(float theValue) throws StandardException
      Description copied from class: DataType
      Set the value of this DataValueDescriptor to the given float value At DataType level just throws an error lower classes will override
      Specified by:
      setValue in interface DataValueDescriptor
      Overrides:
      setValue in class DataType
      Parameters:
      theValue - The value to set this DataValueDescriptor to
      Throws:
      StandardException - on NaN or Infinite float
    • setValue

      public void setValue(long theValue)
      Description copied from class: DataType
      Set the value of this DataValueDescriptor to the given long value At DataType level just throws an error lower classes will override
      Specified by:
      setValue in interface DataValueDescriptor
      Overrides:
      setValue in class DataType
      Parameters:
      theValue - The value to set this DataValueDescriptor to
    • setValue

      public void setValue(int theValue)
      Description copied from class: DataType
      Set the value of this DataValueDescriptor to the given int value At DataType level just throws an error lower classes will override
      Specified by:
      setValue in interface DataValueDescriptor
      Overrides:
      setValue in class DataType
      Parameters:
      theValue - The value to set this DataValueDescriptor to
    • setValue

      public void setValue(Number theValue) throws StandardException
      Description copied from class: NumberDataType
      Common code to handle java.lang.Integer as a Number, used for TINYINT, SMALLINT, INTEGER
      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:
    • setBigDecimal

      public void setBigDecimal(BigDecimal bigDecimal) throws StandardException
      Called for an application setting this value using a BigDecimal
      Specified by:
      setBigDecimal in interface DataValueDescriptor
      Overrides:
      setBigDecimal in class NumberDataType
      Parameters:
      bigDecimal - required to be a BigDecimal or null.
      Throws:
      StandardException
    • setValue

      public void setValue(boolean theValue)
      Description copied from class: DataType
      Set the value. At DataType level just throws an error lower classes will override
      Specified by:
      setValue in interface DataValueDescriptor
      Overrides:
      setValue in class DataType
      Parameters:
      theValue - Contains the boolean value to set this to
      See Also:
    • typePrecedence

      public 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:
    • equals

      The = operator as called from the language module, as opposed to the storage module.
      Specified by:
      equals in interface DataValueDescriptor
      Overrides:
      equals in class DataType
      Parameters:
      left - The value on the left side of the =
      right - The value on the right side of the = is not.
      Returns:
      A SQL boolean value telling whether the two parameters are equal
      Throws:
      StandardException - Thrown on error
    • notEquals

      The <> operator as called from the language module, as opposed to the storage module.
      Specified by:
      notEquals in interface DataValueDescriptor
      Overrides:
      notEquals in class DataType
      Parameters:
      left - The value on the left side of the operator
      right - The value on the right side of the operator is not.
      Returns:
      A SQL boolean value telling whether the two parameters are not equal
      Throws:
      StandardException - Thrown on error
    • lessThan

      The < operator as called from the language module, as opposed to the storage module.
      Specified by:
      lessThan in interface DataValueDescriptor
      Overrides:
      lessThan in class DataType
      Parameters:
      left - The value on the left side of the operator
      right - The value on the right side of the operator
      Returns:
      A SQL boolean value telling whether the first operand is less than the second operand
      Throws:
      StandardException - Thrown on error
    • greaterThan

      The > operator as called from the language module, as opposed to the storage module.
      Specified by:
      greaterThan in interface DataValueDescriptor
      Overrides:
      greaterThan in class DataType
      Parameters:
      left - The value on the left side of the operator
      right - The value on the right side of the operator
      Returns:
      A SQL boolean value telling whether the first operand is greater than the second operand
      Throws:
      StandardException - Thrown on error
    • lessOrEquals

      The <= operator as called from the language module, as opposed to the storage module.
      Specified by:
      lessOrEquals in interface DataValueDescriptor
      Overrides:
      lessOrEquals in class DataType
      Parameters:
      left - The value on the left side of the operator
      right - The value on the right side of the operator
      Returns:
      A SQL boolean value telling whether the first operand is less than or equal to the second operand
      Throws:
      StandardException - Thrown on error
    • greaterOrEquals

      public BooleanDataValue greaterOrEquals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException
      The >= operator as called from the language module, as opposed to the storage module.
      Specified by:
      greaterOrEquals in interface DataValueDescriptor
      Overrides:
      greaterOrEquals in class DataType
      Parameters:
      left - The value on the left side of the operator
      right - The value on the right side of the operator
      Returns:
      A SQL boolean value telling whether the first operand is greater than or equal to the second operand
      Throws:
      StandardException - Thrown on error
    • plus

      public NumberDataValue plus(NumberDataValue addend1, NumberDataValue addend2, NumberDataValue result) throws StandardException
      This method implements the + operator for "double + double".
      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 SQLDouble containing the result of the addition
      Throws:
      StandardException - Thrown on error
    • minus

      This method implements the - operator for "double - double".
      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 SQLDouble containing the result of the subtraction
      Throws:
      StandardException - Thrown on error
    • times

      This method implements the * operator for "double * double".
      Parameters:
      left - The first value to be multiplied
      right - The second value to be multiplied
      result - The result of a previous call to this method, null if not called yet
      Returns:
      A SQLDouble containing the result of the multiplication
      Throws:
      StandardException - Thrown on error
    • divide

      public NumberDataValue divide(NumberDataValue dividend, NumberDataValue divisor, NumberDataValue result) throws StandardException
      This method implements the / operator for "double / double".
      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 SQLDouble containing the result of the division
      Throws:
      StandardException - Thrown on error
    • minus

      public NumberDataValue minus(NumberDataValue result) throws StandardException
      This method implements the unary minus operator for double.
      Parameters:
      result - The result of a previous call to this method, null if not called yet
      Returns:
      A SQLDouble containing the result of the division
      Throws:
      StandardException - Thrown on error
    • isNegative

      protected boolean isNegative()
      This method implements the isNegative method.
      Specified by:
      isNegative in class NumberDataType
      Returns:
      A boolean. If this.value is negative, return true. For positive values or null, return false.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • 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.
      Returns:
      the estimated memory usage