Interface UInt256Value<T extends UInt256Value<T>>
- Type Parameters:
T- The concrete type of the value.
- All Superinterfaces:
org.apache.tuweni.bytes.Bytes,org.apache.tuweni.bytes.Bytes32,Comparable<org.apache.tuweni.bytes.Bytes>
- All Known Subinterfaces:
BytesUInt256Value<T>
- All Known Implementing Classes:
BaseUInt256Value,Gas,UInt256,Wei
A UInt256Value is an unsigned integer value stored with 32 bytes, so whose value can
range between 0 and 2^256-1.
This interface defines operations for value types with a 256-bit precision range. The methods
provided by this interface take parameters of the same type (and also long. This provides
type safety by ensuring calculations cannot mix different UInt256Value types.
Where only a pure numerical 256-bit value is required, UInt256 should be used.
It is strongly advised to extend BaseUInt256Value rather than implementing this
interface directly. Doing so provides type safety in that quantities of different units cannot be
mixed accidentally.
-
Field Summary
Fields inherited from interface org.apache.tuweni.bytes.Bytes
EMPTYFields inherited from interface org.apache.tuweni.bytes.Bytes32
SIZE, ZERO -
Method Summary
Modifier and TypeMethodDescriptionadd(long value) Returns a value that is(this + value).Returns a value that is(this + value).default TaddExact(long value) Returns a value that is(this + value).default TReturns a value that is(this + value).addMod(long value, long modulus) Returns a value equivalent to((this + value) mod modulus).Returns a value equivalent to((this + value) mod modulus).Returns a value equivalent to((this + value) mod modulus).default TaddSafe(long value) Returns a value that is(this + value), or MAX_VALUE if it overflows.default TReturns a value that is(this + value), or MAX_VALUE if it overflows.divide(long value) Returns a value that is(this / value).Returns a value that is(this / value).divideCeil(long value) Returns a value that isceiling(this / value).divideCeil(T value) Returns a value that isceiling(this / value).default booleanfitsInt()Returns true if the value can fit in an int.default booleanReturns true if the value can fit in an int according to the byte order.default booleanfitsLong()Returns true if the value can fit in a long.default booleanReturns true if the value can fit in a long.default booleangreaterOrEqualThan(UInt256Value<T> other) Returns true if this value is greater or equal than the other onedefault booleangreaterThan(UInt256Value<T> other) Returns true if this value is greater than the other onedefault intintValue()Provides this value as an int.default booleanlessOrEqualThan(UInt256Value<T> other) Returns true if this value is less or equal than the other onedefault booleanlessThan(UInt256Value<T> other) Returns true if this value is less than the other onemax()Return the max value for this type.mod(long modulus) Returns a value that is(this mod 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.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).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(T value, UInt256 modulus) Returns a value that is((this * value) mod modulus).default Tplus(long value) Returns a value that is(this + value).default TReturns a value that is(this + value).pow(long exponent) Returns a value that is(this<sup>exponent</sup> mod 2<sup>256</sup>)Returns a value that is(this<sup>exponent</sup> mod 2<sup>256</sup>)subtract(long value) Returns a value that is(this - value).Returns a value that is(this - value).default TsubtractExact(long value) Returns a value that is(this - value).default TsubtractExact(T value) Returns a value that is(this - value).default BigIntegerThe BigInteger corresponding to interpreting these bytes as an unsigned integer.default BigIntegertoBigInteger(ByteOrder order) The BigInteger corresponding to interpreting these bytes as an unsigned integer.org.apache.tuweni.bytes.Bytes32toBytes()Provides the value as bytes.default StringReturns the decimal representation of this value as a String.default intdefault longorg.apache.tuweni.bytes.BytesProvides the value as bytes without any leading zero bytes.default BigIntegerThe BigInteger corresponding to interpreting these bytes as a two's-complement signed integer.default BigIntegertoSignedBigInteger(ByteOrder order) The BigInteger corresponding to interpreting these bytes as a two's-complement signed integer.Convert this value to aUInt256.Methods inherited from interface org.apache.tuweni.bytes.Bytes
and, and, appendHexTo, appendTo, appendTo, bitLength, commonPrefix, commonPrefixLength, compareTo, copyTo, copyTo, get, getInt, getInt, getLong, getLong, hasLeadingZero, hasLeadingZeroByte, isEmpty, isZero, not, numberOfLeadingZeroBytes, numberOfLeadingZeros, numberOfTrailingZeroBytes, or, or, reverse, shiftLeft, shiftRight, slice, slice, toArray, toArray, toArrayUnsafe, toBase64String, toEllipsisHexString, toFastHex, toHexString, toInt, toLong, toQuantityHexString, toShortHexString, toString, toUnprefixedHexString, toUnsignedBigInteger, toUnsignedBigInteger, trimLeadingZeros, trimTrailingZeros, update, xor, xorMethods inherited from interface org.apache.tuweni.bytes.Bytes32
and, copy, mutableCopy, not, or, shiftLeft, shiftRight, size, xor
-
Method Details
-
add
Returns a value that is(this + value).- Parameters:
value- The amount to be added to this value.- Returns:
this + value
-
plus
Returns a value that is(this + value).This notation can be used in Kotlin with the
+operator.- Parameters:
value- The amount to be added to this value.- Returns:
this + value
-
plus
Returns a value that is(this + value).This notation can be used in Kotlin with the
+operator.- Parameters:
value- The amount to be added to this value.- Returns:
this + value
-
addExact
Returns a value that is(this + value).- Parameters:
value- the amount to be added to this value- Returns:
this + value- Throws:
ArithmeticException- if the result of the addition overflows
-
addSafe
Returns a value that is(this + value), or MAX_VALUE if it overflows.- Parameters:
value- the amount to be added to this value- Returns:
this + valueor UInt256.MAX
-
add
Returns a value that is(this + value).- Parameters:
value- The amount to be added to this value.- Returns:
this + value
-
addExact
Returns a value that is(this + value).- Parameters:
value- the amount to be added to this value- Returns:
this + value- Throws:
ArithmeticException- if the result of the addition overflows
-
addSafe
Returns a value that is(this + value), or MAX_VALUE if it overflows.- Parameters:
value- the amount to be added to this value- Returns:
this + valueor UInt256.MAX
-
addMod
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
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
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.
-
subtract
Returns a value that is(this - value).- Parameters:
value- The amount to be subtracted from this value.- Returns:
this - value
-
subtractExact
Returns a value that is(this - value).- Parameters:
value- the amount to be subtracted to this value- Returns:
this - value- Throws:
ArithmeticException- if the result of the addition overflows
-
subtract
Returns a value that is(this - value).- Parameters:
value- The amount to be subtracted from this value.- Returns:
this - value
-
subtractExact
Returns a value that is(this - value).- Parameters:
value- the amount to be subtracted to this value- Returns:
this - value- Throws:
ArithmeticException- if the result of the addition overflows
-
max
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.- Returns:
- The max value for this type.
-
multiply
Returns a value that is(this * value).- Parameters:
value- The amount to multiply this value by.- Returns:
this * value
-
multiply
Returns a value that is(this * value).- Parameters:
value- The amount to multiply this value by.- Returns:
this * value- Throws:
ArithmeticException-value< 0.
-
multiplyMod
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 ormodulus== 0.
-
multiplyMod
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 ormodulus== 0.
-
multiplyMod
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 ormodulus≤ 0.
-
divide
Returns a value that is(this / value).- Parameters:
value- The amount to divide this value by.- Returns:
this / value- Throws:
ArithmeticException-value== 0.
-
divide
Returns a value that is(this / value).- Parameters:
value- The amount to divide this value by.- Returns:
this / value- Throws:
ArithmeticException-value≤ 0.
-
divideCeil
Returns a value that isceiling(this / value).- Parameters:
value- The amount to divide this value by.- Returns:
this / value + ( this % value == 0 ? 0 : 1)- Throws:
ArithmeticException-value== 0.
-
divideCeil
Returns a value that isceiling(this / value).- Parameters:
value- The amount to divide this value by.- Returns:
this / value + ( this % value == 0 ? 0 : 1)- Throws:
ArithmeticException-value== 0.
-
pow
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
exponentis anUInt256rather than of the typeT.- Parameters:
exponent- The exponent to which this value is to be raised.- Returns:
this<sup>exponent</sup> mod 2<sup>256</sup>
-
pow
Returns a value that is(this<sup>exponent</sup> mod 2<sup>256</sup>)This calculates an exponentiation over the modulus of
2^256.- Parameters:
exponent- The exponent to which this value is to be raised.- Returns:
this<sup>exponent</sup> mod 2<sup>256</sup>
-
mod
Returns a value that is(this mod modulus).- Parameters:
modulus- The modulus.- Returns:
this mod modulus.- Throws:
ArithmeticException-modulus== 0.
-
mod
Returns a value that is(this mod modulus).- Parameters:
modulus- The modulus.- Returns:
this mod modulus.- Throws:
ArithmeticException-modulus≤ 0.
-
mod0
Returns a value that is(this mod modulus), or 0 if modulus is 0.- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
mod0
Returns a value that is(this mod modulus), or 0 if modulus is 0.- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
fitsInt
default boolean fitsInt()Returns true if the value can fit in an int.- Returns:
- True if this value fits a java
int(i.e. is less or equal toInteger.MAX_VALUE).
-
fitsInt
Returns true if the value can fit in an int according to the byte order.- Parameters:
order- the byte order, little or big endian- Returns:
- True if this value fits a java
int(i.e. is less or equal toInteger.MAX_VALUE).
-
intValue
default int intValue()Provides this value as an int.- Returns:
- This value as a java
intassuming it is small enough to fit anint. - Throws:
ArithmeticException- If the value does not fit anint, that is if!fitsInt().
-
toInt
- Specified by:
toIntin interfaceorg.apache.tuweni.bytes.Bytes
-
toLong
- Specified by:
toLongin interfaceorg.apache.tuweni.bytes.Bytes
-
fitsLong
default boolean fitsLong()Returns true if the value can fit in a long.- Returns:
- True if this value fits a java
long(i.e. is less or equal toLong.MAX_VALUE).
-
fitsLong
Returns true if the value can fit in a long.- Parameters:
order- byte order, little or big endian- Returns:
- True if this value fits a java
long(i.e. is less or equal toLong.MAX_VALUE).
-
toUInt256
UInt256 toUInt256()Convert this value to aUInt256.- Returns:
- This value as a
UInt256.
-
toBytes
org.apache.tuweni.bytes.Bytes32 toBytes()Provides the value as bytes.- Returns:
- The value as bytes.
-
toMinimalBytes
org.apache.tuweni.bytes.Bytes toMinimalBytes()Provides the value as bytes without any leading zero bytes.- Returns:
- The value as bytes without any leading zero bytes.
-
greaterThan
Returns true if this value is greater than the other one- Parameters:
other- the other value being compared- Returns:
- true if this value is greater than the other one, false otherwise
-
greaterOrEqualThan
Returns true if this value is greater or equal than the other one- Parameters:
other- the other value being compared- Returns:
- true if this value is greater or equal than the other one, false otherwise
-
lessThan
Returns true if this value is less than the other one- Parameters:
other- the other value being compared- Returns:
- true if this value is less than the other one, false otherwise
-
lessOrEqualThan
Returns true if this value is less or equal than the other one- Parameters:
other- the other value being compared- Returns:
- true if this value is less or equal than the other one, false otherwise
-
toDecimalString
Returns the decimal representation of this value as a String.- Returns:
- the decimal representation of this value as a String.
-
toSignedBigInteger
The BigInteger corresponding to interpreting these bytes as a two's-complement signed integer.- Returns:
- A
BigIntegercorresponding to interpreting these bytes as a two's-complement signed integer.
-
toSignedBigInteger
The BigInteger corresponding to interpreting these bytes as a two's-complement signed integer.- Parameters:
order- The byte-order for decoding the integer.- Returns:
- A
BigIntegercorresponding to interpreting these bytes as a two's-complement signed integer.
-
toBigInteger
The BigInteger corresponding to interpreting these bytes as an unsigned integer.- Specified by:
toBigIntegerin interfaceorg.apache.tuweni.bytes.Bytes- Returns:
- A positive (or zero)
BigIntegercorresponding to interpreting these bytes as an unsigned integer.
-
toBigInteger
The BigInteger corresponding to interpreting these bytes as an unsigned integer.- Specified by:
toBigIntegerin interfaceorg.apache.tuweni.bytes.Bytes- Parameters:
order- The byte-order for decoding the integer.- Returns:
- A positive (or zero)
BigIntegercorresponding to interpreting these bytes as an unsigned integer.
-