Class CVMBigInteger

All Implemented Interfaces:
IValidated, IWriteable, Comparable<ANumeric>

public final class CVMBigInteger extends AInteger
Arbitrary precision Integer implementation for the CVM. A CVMBigInteger is a canonical CVM value if and only if it represents a number that cannot be a CVMLong
  • Field Details

    • MIN_POSITIVE

      public static final CVMBigInteger MIN_POSITIVE
    • MIN_NEGATIVE

      public static final CVMBigInteger MIN_NEGATIVE
    • MIN_POSITIVE_BIG

      public static final BigInteger MIN_POSITIVE_BIG
    • MIN_NEGATIVE_BIG

      public static final BigInteger MIN_NEGATIVE_BIG
    • LONG_BYTELENGTH

      protected static final long LONG_BYTELENGTH
      See Also:
  • Method Details

    • wrap

      public static CVMBigInteger wrap(byte[] bs)
      Creates a CVMBigInteger WARNING: might not be canonical
      Parameters:
      bs - Bytes representing BigInteger value. Highest bit assumed to be sign.
      Returns:
      CVMBigInteger instance
    • wrap

      public static CVMBigInteger wrap(BigInteger value)
      Creates a CVMBigInteger WARNING: might not be canonical
      Parameters:
      value - Java BigInteger
      Returns:
      CVMBigInteger instance
    • create

      public static CVMBigInteger create(ABlob data)
      Create a big integer from a valid blob representation. Blob can be non-canonical.
      Parameters:
      data - Blob data containing minimal BigInteger twos complement representation
      Returns:
      Big Integer value or null if not valid.
    • toLong

      public CVMLong toLong()
      Description copied from class: ANumeric
      Casts to a CVM Long value. Takes last 64 bits.
      Specified by:
      toLong in class ANumeric
      Returns:
      Long representation of this number
    • longValue

      public long longValue()
      Specified by:
      longValue in class APrimitive
      Returns:
      Java long value representing this primitive CVM value
    • big

      public BigInteger big()
      Description copied from class: AInteger
      Converts this integer to a Java BigInteger. WARNING: might be O(n)
      Specified by:
      big in class AInteger
      Returns:
      Java BigInteger
    • blob

      protected ABlob blob()
    • buildBlob

      protected ABlob buildBlob()
    • buildBigInteger

      protected BigInteger buildBigInteger()
    • toDouble

      public CVMDouble toDouble()
      Description copied from class: ANumeric
      Casts to a CVM Double value.
      Specified by:
      toDouble in class ANumeric
      Returns:
      Double value
    • numericType

      public Class<?> numericType()
      Description copied from class: ANumeric
      Gets the numeric type that should be used as for calculations
      Specified by:
      numericType in class ANumeric
      Returns:
      Double.class or Long.class, or null if not a numeric type
    • signum

      public CVMLong signum()
      Description copied from class: ANumeric
      Gets the signum of this numerical value. Will be -1, 0 or 1 for Longs, -1.0, 0.0 , 1.0 or ##NaN for Doubles.
      Specified by:
      signum in class AInteger
      Returns:
      Signum of the numeric value
    • estimatedEncodingSize

      public int estimatedEncodingSize()
      Description copied from interface: IWriteable
      Estimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures
      Returns:
      The estimated size for the binary representation of this object.
    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in class ANumeric
      Returns:
      Java double value representing this primitive CVM value
    • validateCell

      public void validateCell() throws InvalidDataException
      Description copied from class: ACell
      Validates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.
      Specified by:
      validateCell in class ACell
      Throws:
      InvalidDataException - If the Cell is invalid
    • getTag

      public byte getTag()
      Description copied from class: ACell
      Gets the tag byte for this cell. The tag byte is always equal to the first byte of the Cell's canonical Encoding, and is sufficient to distinguish how to read the rest of the encoding.
      Specified by:
      getTag in class ACell
      Returns:
      Tag byte for this Cell
    • encode

      public int encode(byte[] bs, int pos)
      Description copied from class: ACell
      Writes this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.
      Specified by:
      encode in interface IWriteable
      Specified by:
      encode in class ACell
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • encodeRaw

      protected int encodeRaw(byte[] bs, int pos)
      Description copied from class: ACell
      Writes this Cell's encoding to a byte array, excluding the tag byte.
      Specified by:
      encodeRaw in class ACell
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • print

      public boolean print(BlobBuilder sb, long limit)
      Description copied from class: AObject
      Prints this Object to a readable String Representation. SECURITY: Must halt and return false in O(1) time when limit of printing is exceeded otherwise DoS attacks may be possible.
      Specified by:
      print in class AObject
      Parameters:
      sb - BlobBuilder to append to. May be partially written if print limit exceeded
      limit - Limit of printing in string bytes
      Returns:
      True if fully printed within limit, false otherwise
    • byteLength

      public long byteLength()
      Description copied from class: AInteger
      Number of bytes in minimal representation of this Integer. Returns 0 if and only if the integer is zero.
      Specified by:
      byteLength in class AInteger
      Returns:
      Number of bytes
    • toString

      public String toString()
      Description copied from class: ACell
      Returns the Java String representation of this Cell. The String representation is intended to be a easy-to-read textual representation of the Cell's data content.
      Overrides:
      toString in class ACell
    • equals

      public boolean equals(ACell a)
      Description copied from class: ACell
      Checks for equality with another Cell. In general, Cells are considered equal if they have the same canonical representation, i.e. an identical encoding with the same hash value. Subclasses SHOULD override this if they have a more efficient equals implementation. MUST NOT require reads from Store.
      Overrides:
      equals in class ACell
      Parameters:
      a - Cell to compare with. May be null.
      Returns:
      True if this cell is equal to the other object
    • equals

      public boolean equals(CVMBigInteger a)
    • getBigInteger

      public BigInteger getBigInteger()
      Gets the Java BigInteger representing this number
      Returns:
      Java BigInteger
    • isCanonical

      public boolean isCanonical()
      Description copied from class: ACell
      Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).
      Specified by:
      isCanonical in class AInteger
      Returns:
      true if the object is in canonical format, false otherwise
    • toCanonical

      public AInteger toCanonical()
      Description copied from class: ACell
      Converts this Cell to its canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise.
      Specified by:
      toCanonical in class AInteger
      Returns:
      Canonical version of Cell
    • read

      public static CVMBigInteger read(Blob blob, int offset) throws BadFormatException
      Throws:
      BadFormatException
    • abs

      public ANumeric abs()
      Description copied from class: ANumeric
      Gets the absolute value of this number. May be ##NaN for Doubles.
      Specified by:
      abs in class ANumeric
      Returns:
      Absolute value of the numeric value
    • inc

      public AInteger inc()
      Description copied from class: AInteger
      Increments this Integer
      Specified by:
      inc in class AInteger
      Returns:
      Incremented value
    • dec

      public AInteger dec()
      Description copied from class: AInteger
      Decrements this Integer
      Specified by:
      dec in class AInteger
      Returns:
      Decremented value
    • compareTo

      public int compareTo(ANumeric o)
    • add

      public AInteger add(AInteger a)
      Description copied from class: AInteger
      Adds another integer to this integer
      Specified by:
      add in class AInteger
      Parameters:
      a - Integer value to add
      Returns:
      New integer
    • sub

      public AInteger sub(AInteger b)
      Description copied from class: AInteger
      Subtracts another integer from this integer
      Specified by:
      sub in class AInteger
      Parameters:
      b - Integer value to subtract
      Returns:
      New integer
    • negate

      public AInteger negate()
      Description copied from class: ANumeric
      Negates this numeric value
      Specified by:
      negate in class ANumeric
      Returns:
      Negated value
    • parse

      public static CVMBigInteger parse(String s)
      Parses a string as a CVMBigInteger. Might not be canonical.
      Parameters:
      s - String containing at least one numeric digit. May have an optional sign.
      Returns:
      Integer result
    • ensureLong

      public CVMLong ensureLong()
      Description copied from class: ANumeric
      Gets the numeric value as a Long integer, or null if not a valid Long
      Specified by:
      ensureLong in class ANumeric
      Returns:
      CVMLong value, or null
    • multiply

      public ANumeric multiply(ANumeric b)
      Description copied from class: ANumeric
      Multiplies a second numeric value with this value
      Specified by:
      multiply in class ANumeric
      Parameters:
      b - Second number to add
      Returns:
      Result of multiplication
    • isZero

      public boolean isZero()
      Description copied from class: ANumeric
      Check if this numeric value is equal to zero
      Specified by:
      isZero in class ANumeric
      Returns:
      True if this value is numerically equal to zero
    • mod

      public AInteger mod(AInteger base)
      Description copied from class: AInteger
      Returns the modulus of this integer with a given integer base
      Specified by:
      mod in class AInteger
      Parameters:
      base - Base of modulus operation
      Returns:
      Modulus result
    • rem

      public AInteger rem(AInteger base)
      Description copied from class: AInteger
      Returns the remainder of dividing this integer with a given divisor
      Specified by:
      rem in class AInteger
      Parameters:
      base - Base of division operation
      Returns:
      Modulus result
    • div

      public AInteger div(AInteger base)
      Description copied from class: AInteger
      Divides this integer with a given denominator. Performs Euclidian division consistent with mod
      Specified by:
      div in class AInteger
      Parameters:
      base - Base of division
      Returns:
      Division result
    • quot

      public AInteger quot(AInteger base)
      Description copied from class: AInteger
      Divides this integer with a given denominator. Performs division consistent with rem
      Specified by:
      quot in class AInteger
      Parameters:
      base - Base of division
      Returns:
      Division result
    • toBlob

      public ABlob toBlob()
      Description copied from class: AInteger
      Converts this Integer value to Blob form
      Specified by:
      toBlob in class AInteger
      Returns:
      minimal blob representing the integer value