Class BaseUInt256Value<T extends UInt256Value<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>
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
EMPTYFields inherited from interface org.apache.tuweni.bytes.Bytes32
SIZE, ZERO -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBaseUInt256Value(long value, Function<UInt256, T> ctor) protectedBaseUInt256Value(BigInteger value, Function<UInt256, T> ctor) protectedBaseUInt256Value(UInt256 value, Function<UInt256, T> ctor) -
Method Summary
Modifier and TypeMethodDescriptionadd(long value) Returns a value that is(this + value).Returns a value that is(this + value).Returns 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).Returns a value equivalent to((this + value) mod modulus).intCompare twoUInt256values.copy()Return a copy of this value, or itself if immutable.divide(long value) Returns a value that is(this / 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(UInt256 value) Returns a value that is(this / value).divideCeil(T value) Returns a value that isceiling(this / value).booleanbyteget(int i) inthashCode()max()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).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.MutableBytes32pow(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>)org.apache.tuweni.bytes.Bytesslice(int i, int length) subtract(long value) Returns a value that is(this - value).Returns a value that is(this - value).Returns a value that is(this - value).org.apache.tuweni.bytes.Bytes32toBytes()Provides the value as bytes.longorg.apache.tuweni.bytes.BytesProvides the value as bytes without any leading zero bytes.toString()Convert this value to aUInt256.protected Tzero()Return the zero value for this type.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods 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, xorMethods inherited from interface org.apache.tuweni.bytes.Bytes32
and, not, or, shiftLeft, shiftRight, size, xorMethods inherited from interface org.apache.tuweni.units.bigints.UInt256Value
addExact, addExact, addSafe, addSafe, fitsInt, fitsInt, fitsLong, fitsLong, greaterOrEqualThan, greaterThan, intValue, lessOrEqualThan, lessThan, plus, plus, subtractExact, subtractExact, toBigInteger, toBigInteger, toDecimalString, toInt, toSignedBigInteger, toSignedBigInteger
-
Constructor Details
-
BaseUInt256Value
- Parameters:
value- The value to instantiate thisUInt256Valuewith.ctor- A constructor for the concrete type.
-
BaseUInt256Value
- Parameters:
value- An unsigned value to instantiate thisUInt256Valuewith.ctor- A constructor for the concrete type.
-
BaseUInt256Value
- Parameters:
value- An unsigned value to instantiate thisUInt256Valuewith.ctor- A constructor for the concrete type.
-
-
Method Details
-
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 returnthis.- Specified by:
copyin interfaceorg.apache.tuweni.bytes.Bytes- Specified by:
copyin interfaceorg.apache.tuweni.bytes.Bytes32- Returns:
- A copy of this value, or itself if immutable.
-
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
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:
maxin interfaceUInt256Value<T extends UInt256Value<T>>- Returns:
- The max value for this type.
-
add
Description copied from interface:UInt256ValueReturns a value that is(this + value).- Specified by:
addin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to be added to this value.- Returns:
this + value
-
add
Returns a value that is(this + value).- Parameters:
value- The amount to be added to this value.- Returns:
this + value
-
add
Description copied from interface:UInt256ValueReturns a value that is(this + value).- Specified by:
addin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to be added to this value.- Returns:
this + value
-
addMod
Description copied from interface:UInt256ValueReturns a value equivalent to((this + value) mod modulus).- Specified by:
addModin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to be added to this value.modulus- The modulus.- Returns:
(this + value) mod modulus
-
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
Description copied from interface:UInt256ValueReturns a value equivalent to((this + value) mod modulus).- Specified by:
addModin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to be added to this value.modulus- The modulus.- Returns:
(this + value) mod modulus
-
addMod
Description copied from interface:UInt256ValueReturns a value equivalent to((this + value) mod modulus).- Specified by:
addModin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to be added to this value.modulus- The modulus.- Returns:
(this + value) mod modulus
-
subtract
Description copied from interface:UInt256ValueReturns a value that is(this - value).- Specified by:
subtractin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to be subtracted from this value.- Returns:
this - value
-
subtract
Returns a value that is(this - value).- Parameters:
value- The amount to be subtracted from this value.- Returns:
this - value
-
subtract
Description copied from interface:UInt256ValueReturns a value that is(this - value).- Specified by:
subtractin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to be subtracted from this value.- Returns:
this - value
-
multiply
Description copied from interface:UInt256ValueReturns a value that is(this * value).- Specified by:
multiplyin interfaceUInt256Value<T extends UInt256Value<T>>- 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
-
multiply
Description copied from interface:UInt256ValueReturns a value that is(this * value).- Specified by:
multiplyin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to multiply this value by.- Returns:
this * value
-
multiplyMod
Description copied from interface:UInt256ValueReturns a value that is((this * value) mod modulus).- Specified by:
multiplyModin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to multiply this value by.modulus- The modulus.- Returns:
(this * value) mod modulus
-
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
Description copied from interface:UInt256ValueReturns a value that is((this * value) mod modulus).- Specified by:
multiplyModin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to multiply this value by.modulus- The modulus.- Returns:
(this * value) mod modulus
-
multiplyMod
Description copied from interface:UInt256ValueReturns a value that is((this * value) mod modulus).- Specified by:
multiplyModin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to multiply this value by.modulus- The modulus.- Returns:
(this * value) mod modulus
-
divide
Description copied from interface:UInt256ValueReturns a value that is(this / value).- Specified by:
dividein interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to divide this value by.- Returns:
this / value
-
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
Description copied from interface:UInt256ValueReturns a value that is(this / value).- Specified by:
dividein interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to divide this value by.- Returns:
this / value
-
divideCeil
Description copied from interface:UInt256ValueReturns a value that isceiling(this / value).- Specified by:
divideCeilin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to divide this value by.- Returns:
this / value + ( this % value == 0 ? 0 : 1)
-
divideCeil
Returns a value that is(this / value).- Parameters:
value- The amount to divide this value by.- Returns:
this / value- Throws:
ArithmeticException-value== 0.
-
divideCeil
Description copied from interface:UInt256ValueReturns a value that isceiling(this / value).- Specified by:
divideCeilin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
value- The amount to divide this value by.- Returns:
this / value + ( this % value == 0 ? 0 : 1)
-
pow
Description copied from interface:UInt256ValueReturns 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.- Specified by:
powin interfaceUInt256Value<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
Description copied from interface:UInt256ValueReturns 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:
powin interfaceUInt256Value<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
Description copied from interface:UInt256ValueReturns a value that is(this mod modulus).- Specified by:
modin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
mod
Description copied from interface:UInt256ValueReturns a value that is(this mod modulus).- Specified by:
modin interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
mod0
Description copied from interface:UInt256ValueReturns a value that is(this mod modulus), or 0 if modulus is 0.- Specified by:
mod0in interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
mod0
Description copied from interface:UInt256ValueReturns a value that is(this mod modulus), or 0 if modulus is 0.- Specified by:
mod0in interfaceUInt256Value<T extends UInt256Value<T>>- Parameters:
modulus- The modulus.- Returns:
this mod modulus.
-
compareTo
Compare twoUInt256values.- 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
-
hashCode
public int hashCode() -
toString
-
toUInt256
Description copied from interface:UInt256ValueConvert this value to aUInt256.- Specified by:
toUInt256in interfaceUInt256Value<T extends UInt256Value<T>>- Returns:
- This value as a
UInt256.
-
toBytes
public org.apache.tuweni.bytes.Bytes32 toBytes()Description copied from interface:UInt256ValueProvides the value as bytes.- Specified by:
toBytesin interfaceUInt256Value<T extends UInt256Value<T>>- Returns:
- The value as bytes.
-
toMinimalBytes
public org.apache.tuweni.bytes.Bytes toMinimalBytes()Description copied from interface:UInt256ValueProvides the value as bytes without any leading zero bytes.- Specified by:
toMinimalBytesin interfaceUInt256Value<T extends UInt256Value<T>>- Returns:
- The value as bytes without any leading zero bytes.
-
get
public byte get(int i) - Specified by:
getin interfaceorg.apache.tuweni.bytes.Bytes
-
slice
public org.apache.tuweni.bytes.Bytes slice(int i, int length) - Specified by:
slicein interfaceorg.apache.tuweni.bytes.Bytes
-
mutableCopy
public org.apache.tuweni.bytes.MutableBytes32 mutableCopy()- Specified by:
mutableCopyin interfaceorg.apache.tuweni.bytes.Bytes- Specified by:
mutableCopyin interfaceorg.apache.tuweni.bytes.Bytes32
-
toLong
- Specified by:
toLongin interfaceorg.apache.tuweni.bytes.Bytes- Specified by:
toLongin interfaceUInt256Value<T extends UInt256Value<T>>
-