Kotlin Data Class:

https://kotlinlang.org/docs/reference/data-classes.html

fun main(args: Array<String>) {

    //Using the normal constructor like Java
    val noBuilder: MakePaymentRequestDto = MakePaymentRequestDto(100L,
            10,
            "New",
            "2342342",
            "no-error",
            "merchat-1234",
            "123",
            "42324234")

    //Also we don't need to specify the type, as Kotlin will infer it based on the assignment which is MakePaymentRequestDto
    //Kotlin has a concept of named parameter, therefore it acts like the builder pattern:    
     val builder = MakePaymentRequestDto(
            created = 100L,
            amount = 10,
            status = "New",
            id = "2342342",
            errorCode = "no-error",
            merchant = "nomer",
            ip = "123",
            order = "42324234")

    //To mutate a value we use the copy function: 
    //It is creating a new instance by copying the values of object first, then override the value. 
    //So it is not mutating the existing object. 
    val changeState = MakePaymentRequestDto(
            created = 100L,
            amount = 10,
            status = "New",
            id = "2342342",
            errorCode = "no-error",
            merchant = "nomer",
            ip = "123",
            order = "42324234").copy(amount = 200)

}

Data Class provides:

The compiler automatically derives the following members from all properties declared in the primary constructor:

  • equals()/hashCode()pair,
  • toString()of the form"User(name=John, age=42)"
  • copy()function

Kotlin 'MakePaymentRequestDto':

data class MakePaymentRequestDto(val created: Long,
                                 val amount: Int,
                                 val status: String,
                                 val id: String,
                                 val errorCode: String,
                                 val merchant: String,
                                 val ip: String,
                                 val order: String)

Java 'MakePaymentRequestDto':

public class MakePaymentRequestDto {

    private final Long created;
    private final int amount;
    private final String status;
    private final String id;
    private final String errorCode;
    private final String merchant;
    private final String ip;
    private final String order;
    private final String description;

    public MakePaymentRequestDto(Long created,
                                 int amount,
                                 String status,
                                 String id,
                                 String errorCode,
                                 String merchant,
                                 String ip,
                                 String order,
                                 String description) {
        this.created = created;
        this.amount = amount;
        this.status = status;
        this.id = id;
        this.errorCode = errorCode;
        this.merchant = merchant;
        this.ip = ip;
        this.order = order;
        this.description = description;
    }

    public static Builder copy(MakePaymentRequestDto dto) {
        return new Builder()
                .withId(dto.id)
                .withCreated(dto.created)
                .withAmount(dto.amount)
                .withStatus(dto.status)
                .withErrorCode(dto.errorCode)
                .withMerchant(dto.merchant)
                .withIp(dto.ip)
                .withId(dto.order);
    }

    public Long getCreated() {
        return created;
    }

    public int getAmount() {
        return amount;
    }

    public String getStatus() {
        return status;
    }

    public String getId() {
        return id;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getMerchant() {
        return merchant;
    }

    public String getIp() {
        return ip;
    }

    public String getOrder() {
        return order;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MakePaymentRequestDto that = (MakePaymentRequestDto) o;
        if (amount != that.amount) return false;
        if (created != null ? !created.equals(that.created) : that.created != null) return false;
        if (status != null ? !status.equals(that.status) : that.status != null) return false;
        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (errorCode != null ? !errorCode.equals(that.errorCode) : that.errorCode != null) return false;
        if (merchant != null ? !merchant.equals(that.merchant) : that.merchant != null) return false;
        if (ip != null ? !ip.equals(that.ip) : that.ip != null) return false;
        if (order != null ? !order.equals(that.order) : that.order != null) return false;
        return description != null ? description.equals(that.description) : that.description == null;
    }

    @Override
    public int hashCode() {
        int result = created != null ? created.hashCode() : 0;
        result = 31 * result + amount;
        result = 31 * result + (status != null ? status.hashCode() : 0);
        result = 31 * result + (id != null ? id.hashCode() : 0);
        result = 31 * result + (errorCode != null ? errorCode.hashCode() : 0);
        result = 31 * result + (merchant != null ? merchant.hashCode() : 0);
        result = 31 * result + (ip != null ? ip.hashCode() : 0);
        result = 31 * result + (order != null ? order.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        return result;
    }

    public static final class Builder {

        private Long created;
        private int amount;
        private String status;
        private String id;
        private String errorCode;
        private String merchant;
        private String ip;
        private String order;
        private String description;

        private Builder() {
        }

        public static Builder newBuilder() {
            return new Builder();
        }

        public Builder withCreated(Long created) {
            this.created = created;
            return this;
        }

        public Builder withDescription(String description) {
            this.description = description;
            return this;
        }

        public Builder withAmount(int amount) {
            this.amount = amount;
            return this;
        }

        public Builder withStatus(String status) {
            this.status = status;
            return this;
        }

        public Builder withId(String id) {
            this.id = id;
            return this;
        }

        public Builder withErrorCode(String errorCode) {
            this.errorCode = errorCode;
            return this;
        }

        public Builder withMerchant(String merchant) {
            this.merchant = merchant;
            return this;
        }

        public Builder withIp(String ip) {
            this.ip = ip;
            return this;
        }

        public Builder withOrder(String order) {
            this.order = order;
            return this;
        }

        public MakePaymentRequestDto build() {
            return new MakePaymentRequestDto(
                    created,
                    amount,
                    status,
                    id,
                    errorCode,
                    merchant,
                    ip,
                    order,
                    description );
        }
    }
}

results matching ""

    No results matching ""