All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hedera.hapi.streams.ContractBytecode Maven / Gradle / Ivy

There is a newer version: 0.54.0
Show newest version
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;

/**
 * ContractBytecode
 *
 * @param contractId (1) The contract to which the bytecodes apply to
 * @param initcode (2) Contract bytecode during deployment
 * @param runtimeBytecode (3) Contract bytecode after deployment
 */
public record ContractBytecode(
    @Nullable ContractID contractId,
    @NonNull Bytes initcode,
    @NonNull Bytes runtimeBytecode
) {
    /** Protobuf codec for reading and writing in protobuf format */
    public static final Codec PROTOBUF = new com.hedera.hapi.streams.codec.ContractBytecodeProtoCodec();
    /** JSON codec for reading and writing in JSON format */
    public static final JsonCodec JSON = new com.hedera.hapi.streams.codec.ContractBytecodeJsonCodec();
    
    /** Default instance with all fields set to default values */
    public static final ContractBytecode DEFAULT = newBuilder().build();
    /**
     * Create a pre-populated ContractBytecode.
     * 
     * @param contractId (1) The contract to which the bytecodes apply to, 
     * @param initcode (2) Contract bytecode during deployment, 
     * @param runtimeBytecode (3) Contract bytecode after deployment
     */
    public ContractBytecode(ContractID contractId, Bytes initcode, Bytes runtimeBytecode) {
        this.contractId = contractId;
        this.initcode = initcode != null ? initcode : Bytes.EMPTY;
        this.runtimeBytecode = runtimeBytecode != null ? runtimeBytecode : Bytes.EMPTY;
    }
    /**
    * Override the default hashCode method for
    * all other objects to make hashCode
    */
    @Override
    public int hashCode() {
    	int result = 1;
        if (contractId != null && !contractId.equals(DEFAULT.contractId)) {
           result = 31 * result + contractId.hashCode();
        }
        if (initcode != null && !initcode.equals(DEFAULT.initcode)) {
           result = 31 * result + initcode.hashCode();
        }
        if (runtimeBytecode != null && !runtimeBytecode.equals(DEFAULT.runtimeBytecode)) {
           result = 31 * result + runtimeBytecode.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;
        }
        ContractBytecode thatObj = (ContractBytecode)that;
        if (contractId == null && thatObj.contractId != null) {
            return false;
        }
        if (contractId != null && !contractId.equals(thatObj.contractId)) {
            return false;
        }
        if (initcode == null && thatObj.initcode != null) {
            return false;
        }
        if (initcode != null && !initcode.equals(thatObj.initcode)) {
            return false;
        }
        if (runtimeBytecode == null && thatObj.runtimeBytecode != null) {
            return false;
        }
        if (runtimeBytecode != null && !runtimeBytecode.equals(thatObj.runtimeBytecode)) {
            return false;
        }
        return true;
    }
    /**
     * Convenience method to check if the contractId has a value
     *
     * @return true of the contractId has a value
     */
    public boolean hasContractId() {
    	return contractId != null;
    }
    
    /**
     * Gets the value for contractId if it has a value, or else returns the default
     * value for the type.
     *
     * @param defaultValue the default value to return if contractId is null
     * @return the value for contractId if it has a value, or else returns the default value
     */
    public ContractID contractIdOrElse(@NonNull final ContractID defaultValue) {
    	return hasContractId() ? contractId : defaultValue;
    }
    
    /**
     * Gets the value for contractId if it has a value, or else throws an NPE.
     * value for the type.
     *
     * @return the value for contractId if it has a value
     * @throws NullPointerException if contractId is null
     */
    public @NonNull ContractID contractIdOrThrow() {
    	return requireNonNull(contractId, "Field contractId is null");
    }
    
    /**
     * Executes the supplied {@link Consumer} if, and only if, the contractId has a value
     *
     * @param ifPresent the {@link Consumer} to execute
     */
    public void ifContractId(@NonNull final Consumer ifPresent) {
    	if (hasContractId()) {
    		ifPresent.accept(contractId);
    	}
    }


    /**
     * 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(contractId, initcode, runtimeBytecode);
    }
    
    /**
     * 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 ContractID contractId = null;
        @NonNull private Bytes initcode = Bytes.EMPTY;
        @NonNull private Bytes runtimeBytecode = Bytes.EMPTY;
    
        /**
         * Create an empty builder
         */
        public Builder() {}
    
            /**
         * Create a pre-populated Builder.
         * 
         * @param contractId (1) The contract to which the bytecodes apply to, 
         * @param initcode (2) Contract bytecode during deployment, 
         * @param runtimeBytecode (3) Contract bytecode after deployment
         */
        public Builder(ContractID contractId, Bytes initcode, Bytes runtimeBytecode) {
            this.contractId = contractId;
            this.initcode = initcode != null ? initcode : Bytes.EMPTY;
            this.runtimeBytecode = runtimeBytecode != null ? runtimeBytecode : Bytes.EMPTY;
        }
    
    
        /**
         * Build a new model record with data set on builder
         *
         * @return new model record with data set
         */
        public ContractBytecode build() {
            return new ContractBytecode(contractId, initcode, runtimeBytecode);
        }
    
            /**
         * (1) The contract to which the bytecodes apply to
         *
         * @param contractId value to set
         * @return builder to continue building with
         */
        public Builder contractId(@Nullable ContractID contractId) {
            this.contractId = contractId;
            return this;
        }
    
        /**
         * (1) The contract to which the bytecodes apply to
         *
         * @param builder A pre-populated builder
         * @return builder to continue building with
         */
        public Builder contractId(ContractID.Builder builder) {
            this.contractId = builder.build() ;
            return this;
        }
    
        /**
         * (2) Contract bytecode during deployment
         *
         * @param initcode value to set
         * @return builder to continue building with
         */
        public Builder initcode(@NonNull Bytes initcode) {
            this.initcode = initcode != null ? initcode : Bytes.EMPTY;
            return this;
        }
    
        /**
         * (3) Contract bytecode after deployment
         *
         * @param runtimeBytecode value to set
         * @return builder to continue building with
         */
        public Builder runtimeBytecode(@NonNull Bytes runtimeBytecode) {
            this.runtimeBytecode = runtimeBytecode != null ? runtimeBytecode : Bytes.EMPTY;
            return this;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy