Class DecimalType

All Implemented Interfaces:
Comparator<ByteBuffer>, AssignmentTestable

public class DecimalType extends NumberType<BigDecimal>
  • Field Details

  • Method Details

    • allowsEmpty

      public boolean allowsEmpty()
      Description copied from class: AbstractType
      Defines if the type allows an empty set of bytes (new byte[0]) as valid input. The AbstractType.validate(Object, ValueAccessor) and AbstractType.compose(Object, ValueAccessor) methods must allow empty bytes when this returns true, and must reject empty bytes when this is false.

      As of this writing, the main user of this API is for testing to know what types allow empty values and what types don't, so that the data that gets generated understands when ByteBufferUtil.EMPTY_BYTE_BUFFER is allowed as valid data.

      Overrides:
      allowsEmpty in class AbstractType<BigDecimal>
    • isEmptyValueMeaningless

      public boolean isEmptyValueMeaningless()
      Description copied from class: AbstractType
      Returns true for types where empty should be handled like null like Int32Type.
      Overrides:
      isEmptyValueMeaningless in class AbstractType<BigDecimal>
    • isFloatingPoint

      public boolean isFloatingPoint()
      Description copied from class: NumberType
      Checks if this type support floating point numbers.
      Overrides:
      isFloatingPoint in class NumberType<BigDecimal>
      Returns:
      true if this type support floating point numbers, false otherwise.
    • compareCustom

      public <VL, VR> int compareCustom(VL left, ValueAccessor<VL> accessorL, VR right, ValueAccessor<VR> accessorR)
      Description copied from class: AbstractType
      Implement IFF ComparisonType is CUSTOM Compares the byte representation of two instances of this class, for types where this cannot be done by simple in-order comparison of the unsigned bytes Standard Java compare semantics
      Overrides:
      compareCustom in class AbstractType<BigDecimal>
    • asComparableBytes

      public <V> ByteSource asComparableBytes(ValueAccessor<V> accessor, V data, ByteComparable.Version version)
      Constructs a byte-comparable representation. This is rather difficult and involves reconstructing the decimal. To compare, we need a normalized value, i.e. one with a sign, exponent and (0,1) mantissa. To avoid loss of precision, both exponent and mantissa need to be base-100. We can't get this directly off the serialized bytes, as they have base-10 scale and base-256 unscaled part. We store: - sign bit inverted * 0x80 + 0x40 + signed exponent length, where exponent is negated if value is negative - zero or more exponent bytes (as given by length) - 0x80 + first pair of decimal digits, negative if value is negative, rounded to -inf - zero or more 0x80 + pair of decimal digits, always positive - trailing 0x00 Zero is special-cased as 0x80. Because the trailing 00 cannot be produced from a pair of decimal digits (positive or not), no value can be a prefix of another. Encoding examples: 1.1 as c1 = 0x80 (positive number) + 0x40 + (positive exponent) 0x01 (exp length 1) 01 = exponent 1 (100^1) 81 = 0x80 + 01 (0.01) 8a = 0x80 + 10 (....10) 0.0110e2 00 -1 as 3f = 0x00 (negative number) + 0x40 - (negative exponent) 0x01 (exp length 1) ff = exponent -1. negative number, thus 100^1 7f = 0x80 - 01 (-0.01) -0.01e2 00 -99.9 as 3f = 0x00 (negative number) + 0x40 - (negative exponent) 0x01 (exp length 1) ff = exponent -1. negative number, thus 100^1 1c = 0x80 - 100 (-1.00) 8a = 0x80 + 10 (+....10) -0.999e2 00
      Overrides:
      asComparableBytes in class AbstractType<BigDecimal>
    • fromComparableBytes

      public <V> V fromComparableBytes(ValueAccessor<V> accessor, ByteSource.Peekable comparableBytes, ByteComparable.Version version)
      Description copied from class: AbstractType
      Translates the given byte-ordered representation to the common, non-byte-ordered binary representation of a payload for this abstract type (the latter, common binary representation is what we mostly work with in the storage engine internals). If the given bytes don't correspond to the encoding of some payload value for this abstract type, an IllegalArgumentException may be thrown.
      Overrides:
      fromComparableBytes in class AbstractType<BigDecimal>
      Parameters:
      accessor - value accessor used to construct the value.
      comparableBytes - A byte-ordered representation (presumably of a payload for this abstract type).
      version - The byte-comparable version used to construct the representation.
      Returns:
      A of a payload for this abstract type, corresponding to the given byte-ordered representation, constructed using the supplied value accessor.
      See Also:
    • fromString

      public ByteBuffer fromString(String source) throws MarshalException
      Description copied from class: AbstractType
      get a byte representation of the given string.
      Specified by:
      fromString in class AbstractType<BigDecimal>
      Throws:
      MarshalException
    • fromJSONObject

      public Term fromJSONObject(Object parsed) throws MarshalException
      Description copied from class: AbstractType
      Given a parsed JSON string, return a byte representation of the object.
      Specified by:
      fromJSONObject in class AbstractType<BigDecimal>
      Parameters:
      parsed - the result of parsing a json string
      Throws:
      MarshalException
    • toJSONString

      public String toJSONString(ByteBuffer buffer, ProtocolVersion protocolVersion)
      Description copied from class: AbstractType
      Converts the specified value into its JSON representation.

      The buffer position will stay the same.

      Overrides:
      toJSONString in class AbstractType<BigDecimal>
      Parameters:
      buffer - the value to convert
      protocolVersion - the protocol version to use for the conversion
      Returns:
      a JSON string representing the specified value
    • asCQL3Type

      public CQL3Type asCQL3Type()
      Overrides:
      asCQL3Type in class AbstractType<BigDecimal>
    • getSerializer

      public TypeSerializer<BigDecimal> getSerializer()
      Specified by:
      getSerializer in class AbstractType<BigDecimal>
    • getArgumentDeserializer

      public ArgumentDeserializer getArgumentDeserializer()
      Overrides:
      getArgumentDeserializer in class AbstractType<BigDecimal>
      Returns:
      the deserializer used to deserialize the function arguments of this type.
    • toBigDecimal

      protected BigDecimal toBigDecimal(Number number)
      Converts the specified number into a BigDecimal.
      Parameters:
      number - the value to convert
      Returns:
      the converted value
    • add

      public ByteBuffer add(Number left, Number right)
      Description copied from class: NumberType
      Adds the left argument to the right one.
      Specified by:
      add in class NumberType<BigDecimal>
      Parameters:
      left - the left argument
      right - the right argument
      Returns:
      the addition result
    • substract

      public ByteBuffer substract(Number left, Number right)
      Description copied from class: NumberType
      Substracts the left argument from the right one.
      Specified by:
      substract in class NumberType<BigDecimal>
      Parameters:
      left - the left argument
      right - the right argument
      Returns:
      the substraction result
    • multiply

      public ByteBuffer multiply(Number left, Number right)
      Description copied from class: NumberType
      Multiplies the left argument with the right one.
      Specified by:
      multiply in class NumberType<BigDecimal>
      Parameters:
      left - the left argument
      right - the right argument
      Returns:
      the multiplication result
    • divide

      public ByteBuffer divide(Number left, Number right)
      Description copied from class: NumberType
      Divides the left argument by the right one.
      Specified by:
      divide in class NumberType<BigDecimal>
      Parameters:
      left - the left argument
      right - the right argument
      Returns:
      the division result
    • mod

      public ByteBuffer mod(Number left, Number right)
      Description copied from class: NumberType
      Return the remainder.
      Specified by:
      mod in class NumberType<BigDecimal>
      Parameters:
      left - the left argument
      right - the right argument
      Returns:
      the remainder
    • negate

      public ByteBuffer negate(Number input)
      Description copied from class: NumberType
      Negates the argument.
      Specified by:
      negate in class NumberType<BigDecimal>
      Parameters:
      input - the argument to negate
      Returns:
      the negated argument
    • abs

      public ByteBuffer abs(Number input)
      Description copied from class: NumberType
      Takes the absolute value of the argument.
      Specified by:
      abs in class NumberType<BigDecimal>
      Parameters:
      input - the argument to take the absolute value of
      Returns:
      a ByteBuffer containing the absolute value of the argument. The type of the contents of the ByteBuffer will match that of the input.
    • exp

      public ByteBuffer exp(Number input)
      Description copied from class: NumberType
      Raises e to the power of the argument.
      Specified by:
      exp in class NumberType<BigDecimal>
      Parameters:
      input - the argument to raise e to
      Returns:
      a ByteBuffer containg the value of e (the base of natural logarithms) raised to the power of the argument. The type of the contents of the ByteBuffer will be a double, unless the input is a DecimalType or IntegerType, in which the ByteBuffer will contain a DecimalType.
    • exp

      protected BigDecimal exp(BigDecimal input)
    • log

      public ByteBuffer log(Number input)
      Description copied from class: NumberType
      Takes the natural logrithm of the argument.
      Specified by:
      log in class NumberType<BigDecimal>
      Parameters:
      input - the argument to take the natural log (ln) of
      Returns:
      a ByteBuffer containg the log base e (ln) of the argument. The type of the contents of the ByteBuffer will be a double, unless the input is a DecimalType or IntegerType, in which the ByteBuffer will contain a DecimalType.
    • log

      protected BigDecimal log(BigDecimal input)
    • log10

      public ByteBuffer log10(Number input)
      Description copied from class: NumberType
      Takes the log base 10 of the arguement.
      Specified by:
      log10 in class NumberType<BigDecimal>
      Parameters:
      input - the argument to take the log base ten of
      Returns:
      a ByteBuffer containg the log base 10 of the argument. The type of the contents of the ByteBuffer will be a double, unless the input is a DecimalType or IntegerType, in which the ByteBuffer will contain a DecimalType.
    • log10

      protected BigDecimal log10(BigDecimal input)
    • round

      public ByteBuffer round(Number input)
      Description copied from class: NumberType
      Rounds the argument to the nearest whole number.
      Specified by:
      round in class NumberType<BigDecimal>
      Parameters:
      input - the argument to round
      Returns:
      a ByteBuffer containg the rounded argument. If the input is an integral type, the ByteBuffer will contain a copy of the input. If the input is a float, the ByteBuffer will contain an int32. If the input is a double, the output will contain a long. If the input is a DecimalType, the output will contain an IntegerType.
    • getMaskedValue

      public ByteBuffer getMaskedValue()
      Overrides:
      getMaskedValue in class AbstractType<BigDecimal>
      Returns:
      A fixed, serialized value to be used when the column is masked, to be returned instead of the real value.