com.hedera.hapi.streams.TokenUnitBalance Maven / Gradle / Ivy
package com.hedera.hapi.streams;
import com.hedera.hapi.node.base.*;
import com.hedera.pbj.runtime.*;
import com.hedera.pbj.runtime.io.*;
import com.hedera.pbj.runtime.io.buffer.*;
import com.hedera.pbj.runtime.io.stream.*;
import edu.umd.cs.findbugs.annotations.*;
import com.hedera.pbj.runtime.Codec;
import java.util.function.Consumer;
import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.NonNull;
import static java.util.Objects.requireNonNull;
/**
* TokenUnitBalance
*
* @param tokenId (1) A unique token id
* @param balance (2) Number of transferable units of the identified token. For token of type FUNGIBLE_COMMON -
* balance in the smallest denomination. For token of type NON_FUNGIBLE_UNIQUE - the number of
* NFTs held by the account
*/
public record TokenUnitBalance(
@Nullable TokenID tokenId,
long balance
) {
/** Protobuf codec for reading and writing in protobuf format */
public static final Codec PROTOBUF = new com.hedera.hapi.streams.codec.TokenUnitBalanceProtoCodec();
/** JSON codec for reading and writing in JSON format */
public static final JsonCodec JSON = new com.hedera.hapi.streams.codec.TokenUnitBalanceJsonCodec();
/** Default instance with all fields set to default values */
public static final TokenUnitBalance DEFAULT = newBuilder().build();
/**
* Create a pre-populated TokenUnitBalance.
*
* @param tokenId (1) A unique token id,
* @param balance (2) Number of transferable units of the identified token. For token of type FUNGIBLE_COMMON -
* balance in the smallest denomination. For token of type NON_FUNGIBLE_UNIQUE - the number of
* NFTs held by the account
*/
public TokenUnitBalance(TokenID tokenId, long balance) {
this.tokenId = tokenId;
this.balance = balance;
}
/**
* Override the default hashCode method for
* all other objects to make hashCode
*/
@Override
public int hashCode() {
int result = 1;
if (tokenId != null && !tokenId.equals(DEFAULT.tokenId)) {
result = 31 * result + tokenId.hashCode();
}
if (balance != DEFAULT.balance) {
result = 31 * result + Long.hashCode(balance);
}
long hashCode = result;
// Shifts: 30, 27, 16, 20, 5, 18, 10, 24, 30
hashCode += hashCode << 30;
hashCode ^= hashCode >>> 27;
hashCode += hashCode << 16;
hashCode ^= hashCode >>> 20;
hashCode += hashCode << 5;
hashCode ^= hashCode >>> 18;
hashCode += hashCode << 10;
hashCode ^= hashCode >>> 24;
hashCode += hashCode << 30;
return (int)hashCode;
}
/**
* Override the default equals method for
*/
@Override
public boolean equals(Object that) {
if (that == null || this.getClass() != that.getClass()) {
return false;
}
TokenUnitBalance thatObj = (TokenUnitBalance)that;
if (tokenId == null && thatObj.tokenId != null) {
return false;
}
if (tokenId != null && !tokenId.equals(thatObj.tokenId)) {
return false;
}
if (balance != thatObj.balance) {
return false;
}
return true;
}
/**
* Convenience method to check if the tokenId has a value
*
* @return true of the tokenId has a value
*/
public boolean hasTokenId() {
return tokenId != null;
}
/**
* Gets the value for tokenId if it has a value, or else returns the default
* value for the type.
*
* @param defaultValue the default value to return if tokenId is null
* @return the value for tokenId if it has a value, or else returns the default value
*/
public TokenID tokenIdOrElse(@NonNull final TokenID defaultValue) {
return hasTokenId() ? tokenId : defaultValue;
}
/**
* Gets the value for tokenId if it has a value, or else throws an NPE.
* value for the type.
*
* @return the value for tokenId if it has a value
* @throws NullPointerException if tokenId is null
*/
public @NonNull TokenID tokenIdOrThrow() {
return requireNonNull(tokenId, "Field tokenId is null");
}
/**
* Executes the supplied {@link Consumer} if, and only if, the tokenId has a value
*
* @param ifPresent the {@link Consumer} to execute
*/
public void ifTokenId(@NonNull final Consumer ifPresent) {
if (hasTokenId()) {
ifPresent.accept(tokenId);
}
}
/**
* Return a builder for building a copy of this model object. It will be pre-populated with all the data from this
* model object.
*
* @return a pre-populated builder
*/
public Builder copyBuilder() {
return new Builder(tokenId, balance);
}
/**
* Return a new builder for building a model object. This is just a shortcut for new Model.Builder()
.
*
* @return a new builder
*/
public static Builder newBuilder() {
return new Builder();
}
/**
* Builder class for easy creation, ideal for clean code where performance is not critical. In critical performance
* paths use the constructor directly.
*/
public static final class Builder {
@Nullable private TokenID tokenId = null;
private long balance = 0;
/**
* Create an empty builder
*/
public Builder() {}
/**
* Create a pre-populated Builder.
*
* @param tokenId (1) A unique token id,
* @param balance (2) Number of transferable units of the identified token. For token of type FUNGIBLE_COMMON -
* balance in the smallest denomination. For token of type NON_FUNGIBLE_UNIQUE - the number of
* NFTs held by the account
*/
public Builder(TokenID tokenId, long balance) {
this.tokenId = tokenId;
this.balance = balance;
}
/**
* Build a new model record with data set on builder
*
* @return new model record with data set
*/
public TokenUnitBalance build() {
return new TokenUnitBalance(tokenId, balance);
}
/**
* (1) A unique token id
*
* @param tokenId value to set
* @return builder to continue building with
*/
public Builder tokenId(@Nullable TokenID tokenId) {
this.tokenId = tokenId;
return this;
}
/**
* (1) A unique token id
*
* @param builder A pre-populated builder
* @return builder to continue building with
*/
public Builder tokenId(TokenID.Builder builder) {
this.tokenId = builder.build() ;
return this;
}
/**
* (2) Number of transferable units of the identified token. For token of type FUNGIBLE_COMMON -
* balance in the smallest denomination. For token of type NON_FUNGIBLE_UNIQUE - the number of
* NFTs held by the account
*
* @param balance value to set
* @return builder to continue building with
*/
public Builder balance(long balance) {
this.balance = balance;
return this;
}
}
}