com.hedera.hapi.node.base.TokenAssociation Maven / Gradle / Ivy
The newest version!
package com.hedera.hapi.node.base;
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;
/**
* TokenAssociation
*
* @param tokenId (1)
* @param accountId (2)
*/
public record TokenAssociation(
@Nullable TokenID tokenId,
@Nullable AccountID accountId
) {
/** Protobuf codec for reading and writing in protobuf format */
public static final Codec PROTOBUF = new com.hedera.hapi.node.base.codec.TokenAssociationProtoCodec();
/** JSON codec for reading and writing in JSON format */
public static final JsonCodec JSON = new com.hedera.hapi.node.base.codec.TokenAssociationJsonCodec();
/** Default instance with all fields set to default values */
public static final TokenAssociation DEFAULT = newBuilder().build();
/**
* Create a pre-populated TokenAssociation.
*
* @param tokenId (1) ,
* @param accountId (2)
*/
public TokenAssociation(TokenID tokenId, AccountID accountId) {
this.tokenId = tokenId;
this.accountId = accountId;
}
/**
* 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 (accountId != null && !accountId.equals(DEFAULT.accountId)) {
result = 31 * result + accountId.hashCode();
}
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;
}
TokenAssociation thatObj = (TokenAssociation)that;
if (tokenId == null && thatObj.tokenId != null) {
return false;
}
if (tokenId != null && !tokenId.equals(thatObj.tokenId)) {
return false;
}
if (accountId == null && thatObj.accountId != null) {
return false;
}
if (accountId != null && !accountId.equals(thatObj.accountId)) {
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);
}
}
/**
* Convenience method to check if the accountId has a value
*
* @return true of the accountId has a value
*/
public boolean hasAccountId() {
return accountId != null;
}
/**
* Gets the value for accountId if it has a value, or else returns the default
* value for the type.
*
* @param defaultValue the default value to return if accountId is null
* @return the value for accountId if it has a value, or else returns the default value
*/
public AccountID accountIdOrElse(@NonNull final AccountID defaultValue) {
return hasAccountId() ? accountId : defaultValue;
}
/**
* Gets the value for accountId if it has a value, or else throws an NPE.
* value for the type.
*
* @return the value for accountId if it has a value
* @throws NullPointerException if accountId is null
*/
public @NonNull AccountID accountIdOrThrow() {
return requireNonNull(accountId, "Field accountId is null");
}
/**
* Executes the supplied {@link Consumer} if, and only if, the accountId has a value
*
* @param ifPresent the {@link Consumer} to execute
*/
public void ifAccountId(@NonNull final Consumer ifPresent) {
if (hasAccountId()) {
ifPresent.accept(accountId);
}
}
/**
* 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, accountId);
}
/**
* 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;
@Nullable private AccountID accountId = null;
/**
* Create an empty builder
*/
public Builder() {}
/**
* Create a pre-populated Builder.
*
* @param tokenId (1) ,
* @param accountId (2)
*/
public Builder(TokenID tokenId, AccountID accountId) {
this.tokenId = tokenId;
this.accountId = accountId;
}
/**
* Build a new model record with data set on builder
*
* @return new model record with data set
*/
public TokenAssociation build() {
return new TokenAssociation(tokenId, accountId);
}
/**
* (1)
*
* @param tokenId value to set
* @return builder to continue building with
*/
public Builder tokenId(@Nullable TokenID tokenId) {
this.tokenId = tokenId;
return this;
}
/**
* (1)
*
* @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)
*
* @param accountId value to set
* @return builder to continue building with
*/
public Builder accountId(@Nullable AccountID accountId) {
this.accountId = accountId;
return this;
}
/**
* (2)
*
* @param builder A pre-populated builder
* @return builder to continue building with
*/
public Builder accountId(AccountID.Builder builder) {
this.accountId = builder.build() ;
return this;
}
}
}