Java Lombok | 데이터 객체 - @Data

@Data 사용

클래스에 @Data 어노테이션이 선언되면, 다음의 어노테이션들을 모두 선언이 들어간다.

  • @ToString
  • @Getter
  • @Setter
  • @RequiredArgsConstructor
  • @EqualsAndHashCode
package com.devkuma.tutorial.lombok;

import lombok.Data;

@Data
public class DataTutorial {
    public static void main(String[] args) {
        DataTutorial a = new DataTutorial("devkuma");
        a.setNumber(300);

        DataTutorial b = new DataTutorial("devkuma");
        b.setNumber(300);

        System.out.println("a = " + a);
        System.out.println(a.equals(b));
    }

    private final String required;
    private int number;
}

실행 결과:

a = DataTutorial(required=devkuma, number=300)
true

위 코드는 @Data로 인해 아래와 같이 변경된다.

package com.devkuma.tutorial.lombok;

public class DataTutorial {
    private final String required;
    private int number;

    public static void main(String[] args) {
        DataTutorial a = new DataTutorial("devkuma");
        a.setNumber(300);
        DataTutorial b = new DataTutorial("devkuma");
        b.setNumber(300);
        System.out.println("a = " + a);
        System.out.println(a.equals(b));
    }

    public DataTutorial(String required) {
        this.required = required;
    }

    public String getRequired() {
        return this.required;
    }

    public int getNumber() {
        return this.number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof DataTutorial)) {
            return false;
        } else {
            DataTutorial other = (DataTutorial)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getNumber() != other.getNumber()) {
                return false;
            } else {
                Object this$required = this.getRequired();
                Object other$required = other.getRequired();
                if (this$required == null) {
                    if (other$required != null) {
                        return false;
                    }
                } else if (!this$required.equals(other$required)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof DataTutorial;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        result = result * 59 + this.getNumber();
        Object $required = this.getRequired();
        result = result * 59 + ($required == null ? 43 : $required.hashCode());
        return result;
    }

    public String toString() {
        String var10000 = this.getRequired();
        return "DataTutorial(required=" + var10000 + ", number=" + this.getNumber() + ")";
    }
}



최종 수정 : 2024-01-18