Class BaseUInt256Value<T extends UInt256Value<T>>

java.lang.Object
org.apache.tuweni.units.bigints.BaseUInt256Value<T>
Type Parameters:
T - The concrete type of the value.
All Implemented Interfaces:
Comparable<org.apache.tuweni.bytes.Bytes>, org.apache.tuweni.bytes.Bytes, org.apache.tuweni.bytes.Bytes32, UInt256Value<T>
Direct Known Subclasses:
Gas, Wei

public abstract class BaseUInt256Value<T extends UInt256Value<T>> extends Object implements UInt256Value<T>
Base class for UInt256Value.

This class is abstract as it is not meant to be used directly, but it has no abstract methods. As mentioned in UInt256Value, this is used to create strongly-typed type aliases of UInt256. In other words, this allow to "tag" numbers with the unit of what they represent for the type-system, which can help clarity, but also forbid mixing numbers that are mean to be of different units (the strongly-typed part).

This class implements UInt256Value, but also adds a few operations that take a UInt256 directly, for instance multiply(UInt256). The rational is that multiplying a given quantity of something by a "raw" number is always meaningful, and return a new quantity of the same thing.

  • Field Summary

    Fields inherited from interface org.apache.tuweni.bytes.Bytes

    EMPTY

    Fields inherited from interface org.apache.tuweni.bytes.Bytes32

    SIZE, ZERO
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    BaseUInt256Value(long value, Function<UInt256,T> ctor)
     
    protected
     
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    add(long value)
    Returns a value that is (this + value).
    add(UInt256 value)
    Returns a value that is (this + value).
    add(T value)
    Returns a value that is (this + value).
    addMod(long value, long modulus)
    Returns a value equivalent to ((this + value) mod modulus).
    addMod(long value, UInt256 modulus)
    Returns a value equivalent to ((this + value) mod modulus).
    addMod(UInt256 value, UInt256 modulus)
    Returns a value equivalent to ((this + value) mod modulus).
    addMod(T value, UInt256 modulus)
    Returns a value equivalent to ((this + value) mod modulus).
    int
    Compare two UInt256 values.
    Return a copy of this value, or itself if immutable.
    divide(long value)
    Returns a value that is (this / value).
    divide(UInt256 value)
    Returns a value that is (this / value).
    divide(T value)
    Returns a value that is (this / value).
    divideCeil(long value)
    Returns a value that is ceiling(this / value).
    Returns a value that is (this / value).
    divideCeil(T value)
    Returns a value that is ceiling(this / value).
    boolean
     
    byte
    get(int i)
     
    int
     
    max()
    Return the max value for this type.
    mod(long modulus)
    Returns a value that is (this mod modulus).
    mod(UInt256 modulus)
    Returns a value that is (this mod modulus).
    mod0(long modulus)
    Returns a value that is (this mod modulus), or 0 if modulus is 0.
    mod0(UInt256 modulus)
    Returns a value that is (this mod modulus), or 0 if modulus is 0.
    multiply(long value)
    Returns a value that is (this * value).
    Returns a value that is (this * value).
    multiply(T value)
    Returns a value that is (this * value).
    multiplyMod(long value, long modulus)
    Returns a value that is ((this * value) mod modulus).
    multiplyMod(long value, UInt256 modulus)
    Returns a value that is ((this * value) mod modulus).
    multiplyMod(UInt256 value, UInt256 modulus)
    Returns a value that is ((this * value) mod modulus).
    multiplyMod(T value, UInt256 modulus)
    Returns a value that is ((this * value) mod modulus).
    org.apache.tuweni.bytes.MutableBytes32
     
    pow(long exponent)
    Returns a value that is (this<sup>exponent</sup> mod 2<sup>256</sup>)
    pow(UInt256 exponent)
    Returns a value that is (this<sup>exponent</sup> mod 2<sup>256</sup>)
    org.apache.tuweni.bytes.Bytes
    slice(int i, int length)
     
    subtract(long value)
    Returns a value that is (this - value).
    Returns a value that is (this - value).
    subtract(T value)
    Returns a value that is (this - value).
    org.apache.tuweni.bytes.Bytes32
    Provides the value as bytes.
    long
     
    org.apache.tuweni.bytes.Bytes
    Provides the value as bytes without any leading zero bytes.
     
    Convert this value to a UInt256.
    protected T
    Return the zero value for this type.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.apache.tuweni.bytes.Bytes

    and, and, appendHexTo, appendTo, appendTo, bitLength, commonPrefix, commonPrefixLength, compareTo, copyTo, copyTo, getInt, getInt, getLong, getLong, hasLeadingZero, hasLeadingZeroByte, isEmpty, isZero, not, numberOfLeadingZeroBytes, numberOfLeadingZeros, numberOfTrailingZeroBytes, or, or, reverse, shiftLeft, shiftRight, slice, toArray, toArray, toArrayUnsafe, toBase64String, toEllipsisHexString, toFastHex, toHexString, toInt, toLong, toQuantityHexString, toShortHexString, toUnprefixedHexString, toUnsignedBigInteger, toUnsignedBigInteger, trimLeadingZeros, trimTrailingZeros, update, xor, xor

    Methods inherited from interface org.apache.tuweni.bytes.Bytes32

    and, not, or, shiftLeft, shiftRight, size, xor
  • Constructor Details

    • BaseUInt256Value

      protected BaseUInt256Value(UInt256 value, Function<UInt256,T> ctor)
      Parameters:
      value - The value to instantiate this UInt256Value with.
      ctor - A constructor for the concrete type.
    • BaseUInt256Value

      protected BaseUInt256Value(long value, Function<UInt256,T> ctor)
      Parameters:
      value - An unsigned value to instantiate this UInt256Value with.
      ctor - A constructor for the concrete type.
    • BaseUInt256Value

      protected BaseUInt256Value(BigInteger value, Function<UInt256,T> ctor)
      Parameters:
      value - An unsigned value to instantiate this UInt256Value with.
      ctor - A constructor for the concrete type.
  • Method Details

    • copy

      public T copy()
      Return a copy of this value, or itself if immutable.

      The default implementation of this method returns a copy using the constructor for the concrete type and the bytes returned from toBytes(). Most implementations will want to override this method to instead return this.

      Specified by:
      copy in interface org.apache.tuweni.bytes.Bytes
      Specified by:
      copy in interface org.apache.tuweni.bytes.Bytes32
      Returns:
      A copy of this value, or itself if immutable.
    • zero

      protected T zero()
      Return the zero value for this type.

      The default implementation of this method returns a value obtained from calling the concrete type constructor with an argument of UInt256.ZERO. Most implementations will want to override this method to instead return a static constant.

      Returns:
      The zero value for this type.
    • max

      public T max()
      Return the max value for this type.

      The default implementation of this method returns a value obtained from calling the concrete type constructor with an argument of UInt256.MAX_VALUE. Most implementations will want to override this method to instead return a static constant.

      Specified by:
      max in interface UInt256Value<T extends UInt256Value<T>>
      Returns:
      The max value for this type.
    • add

      public T add(T value)
      Description copied from interface: UInt256Value
      Returns a value that is (this + value).
      Specified by:
      add in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to be added to this value.
      Returns:
      this + value
    • add

      public T add(UInt256 value)
      Returns a value that is (this + value).
      Parameters:
      value - The amount to be added to this value.
      Returns:
      this + value
    • add

      public T add(long value)
      Description copied from interface: UInt256Value
      Returns a value that is (this + value).
      Specified by:
      add in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to be added to this value.
      Returns:
      this + value
    • addMod

      public T addMod(T value, UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value equivalent to ((this + value) mod modulus).
      Specified by:
      addMod in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to be added to this value.
      modulus - The modulus.
      Returns:
      (this + value) mod modulus
    • addMod

      public T addMod(UInt256 value, UInt256 modulus)
      Returns a value equivalent to ((this + value) mod modulus).
      Parameters:
      value - The amount to be added to this value.
      modulus - The modulus.
      Returns:
      (this + value) mod modulus
      Throws:
      ArithmeticException - modulus == 0.
    • addMod

      public T addMod(long value, UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value equivalent to ((this + value) mod modulus).
      Specified by:
      addMod in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to be added to this value.
      modulus - The modulus.
      Returns:
      (this + value) mod modulus
    • addMod

      public T addMod(long value, long modulus)
      Description copied from interface: UInt256Value
      Returns a value equivalent to ((this + value) mod modulus).
      Specified by:
      addMod in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to be added to this value.
      modulus - The modulus.
      Returns:
      (this + value) mod modulus
    • subtract

      public T subtract(T value)
      Description copied from interface: UInt256Value
      Returns a value that is (this - value).
      Specified by:
      subtract in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to be subtracted from this value.
      Returns:
      this - value
    • subtract

      public T subtract(UInt256 value)
      Returns a value that is (this - value).
      Parameters:
      value - The amount to be subtracted from this value.
      Returns:
      this - value
    • subtract

      public T subtract(long value)
      Description copied from interface: UInt256Value
      Returns a value that is (this - value).
      Specified by:
      subtract in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to be subtracted from this value.
      Returns:
      this - value
    • multiply

      public T multiply(T value)
      Description copied from interface: UInt256Value
      Returns a value that is (this * value).
      Specified by:
      multiply in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to multiply this value by.
      Returns:
      this * value
    • multiply

      public T multiply(UInt256 value)
      Returns a value that is (this * value).
      Parameters:
      value - The amount to multiply this value by.
      Returns:
      this * value
    • multiply

      public T multiply(long value)
      Description copied from interface: UInt256Value
      Returns a value that is (this * value).
      Specified by:
      multiply in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to multiply this value by.
      Returns:
      this * value
    • multiplyMod

      public T multiplyMod(T value, UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value that is ((this * value) mod modulus).
      Specified by:
      multiplyMod in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to multiply this value by.
      modulus - The modulus.
      Returns:
      (this * value) mod modulus
    • multiplyMod

      public T multiplyMod(UInt256 value, UInt256 modulus)
      Returns a value that is ((this * value) mod modulus).
      Parameters:
      value - The amount to multiply this value by.
      modulus - The modulus.
      Returns:
      (this * value) mod modulus
      Throws:
      ArithmeticException - value < 0 or modulus == 0.
    • multiplyMod

      public T multiplyMod(long value, UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value that is ((this * value) mod modulus).
      Specified by:
      multiplyMod in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to multiply this value by.
      modulus - The modulus.
      Returns:
      (this * value) mod modulus
    • multiplyMod

      public T multiplyMod(long value, long modulus)
      Description copied from interface: UInt256Value
      Returns a value that is ((this * value) mod modulus).
      Specified by:
      multiplyMod in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to multiply this value by.
      modulus - The modulus.
      Returns:
      (this * value) mod modulus
    • divide

      public T divide(T value)
      Description copied from interface: UInt256Value
      Returns a value that is (this / value).
      Specified by:
      divide in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value
    • divide

      public T divide(UInt256 value)
      Returns a value that is (this / value).
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value
      Throws:
      ArithmeticException - value == 0.
    • divide

      public T divide(long value)
      Description copied from interface: UInt256Value
      Returns a value that is (this / value).
      Specified by:
      divide in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value
    • divideCeil

      public T divideCeil(T value)
      Description copied from interface: UInt256Value
      Returns a value that is ceiling(this / value).
      Specified by:
      divideCeil in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value + ( this % value == 0 ? 0 : 1)
    • divideCeil

      public T divideCeil(UInt256 value)
      Returns a value that is (this / value).
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value
      Throws:
      ArithmeticException - value == 0.
    • divideCeil

      public T divideCeil(long value)
      Description copied from interface: UInt256Value
      Returns a value that is ceiling(this / value).
      Specified by:
      divideCeil in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      value - The amount to divide this value by.
      Returns:
      this / value + ( this % value == 0 ? 0 : 1)
    • pow

      public T pow(UInt256 exponent)
      Description copied from interface: UInt256Value
      Returns a value that is (this<sup>exponent</sup> mod 2<sup>256</sup>)

      This calculates an exponentiation over the modulus of 2^256.

      Note that exponent is an UInt256 rather than of the type T.

      Specified by:
      pow in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      exponent - The exponent to which this value is to be raised.
      Returns:
      this<sup>exponent</sup> mod 2<sup>256</sup>
    • pow

      public T pow(long exponent)
      Description copied from interface: UInt256Value
      Returns a value that is (this<sup>exponent</sup> mod 2<sup>256</sup>)

      This calculates an exponentiation over the modulus of 2^256.

      Specified by:
      pow in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      exponent - The exponent to which this value is to be raised.
      Returns:
      this<sup>exponent</sup> mod 2<sup>256</sup>
    • mod

      public T mod(UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value that is (this mod modulus).
      Specified by:
      mod in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      modulus - The modulus.
      Returns:
      this mod modulus.
    • mod

      public T mod(long modulus)
      Description copied from interface: UInt256Value
      Returns a value that is (this mod modulus).
      Specified by:
      mod in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      modulus - The modulus.
      Returns:
      this mod modulus.
    • mod0

      public T mod0(UInt256 modulus)
      Description copied from interface: UInt256Value
      Returns a value that is (this mod modulus), or 0 if modulus is 0.
      Specified by:
      mod0 in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      modulus - The modulus.
      Returns:
      this mod modulus.
    • mod0

      public T mod0(long modulus)
      Description copied from interface: UInt256Value
      Returns a value that is (this mod modulus), or 0 if modulus is 0.
      Specified by:
      mod0 in interface UInt256Value<T extends UInt256Value<T>>
      Parameters:
      modulus - The modulus.
      Returns:
      this mod modulus.
    • compareTo

      public int compareTo(UInt256 other)
      Compare two UInt256 values.
      Parameters:
      other - The value to compare to.
      Returns:
      A negative integer, zero, or a positive integer as this value is less than, equal to, or greater than the specified value.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Specified by:
      toString in interface org.apache.tuweni.bytes.Bytes
      Overrides:
      toString in class Object
    • toUInt256

      public UInt256 toUInt256()
      Description copied from interface: UInt256Value
      Convert this value to a UInt256.
      Specified by:
      toUInt256 in interface UInt256Value<T extends UInt256Value<T>>
      Returns:
      This value as a UInt256.
    • toBytes

      public org.apache.tuweni.bytes.Bytes32 toBytes()
      Description copied from interface: UInt256Value
      Provides the value as bytes.
      Specified by:
      toBytes in interface UInt256Value<T extends UInt256Value<T>>
      Returns:
      The value as bytes.
    • toMinimalBytes

      public org.apache.tuweni.bytes.Bytes toMinimalBytes()
      Description copied from interface: UInt256Value
      Provides the value as bytes without any leading zero bytes.
      Specified by:
      toMinimalBytes in interface UInt256Value<T extends UInt256Value<T>>
      Returns:
      The value as bytes without any leading zero bytes.
    • get

      public byte get(int i)
      Specified by:
      get in interface org.apache.tuweni.bytes.Bytes
    • slice

      public org.apache.tuweni.bytes.Bytes slice(int i, int length)
      Specified by:
      slice in interface org.apache.tuweni.bytes.Bytes
    • mutableCopy

      public org.apache.tuweni.bytes.MutableBytes32 mutableCopy()
      Specified by:
      mutableCopy in interface org.apache.tuweni.bytes.Bytes
      Specified by:
      mutableCopy in interface org.apache.tuweni.bytes.Bytes32
    • toLong

      public long toLong(ByteOrder order)
      Specified by:
      toLong in interface org.apache.tuweni.bytes.Bytes
      Specified by:
      toLong in interface UInt256Value<T extends UInt256Value<T>>