EqualsBuilder.reflectionEquals

2019. 9. 11. 14:42[개발] 지식/미분류

EqualsBuilder.reflectionEquals

This method uses reflection to determine if the two Objects are equal.

It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly. Non-primitive fields are compared using equals().

Transient members will be not be tested, as they are likely derived fields, and not part of the value of the Object.

Static fields will not be tested. Superclass fields will be included.

API Reference

Equals와 HashCode를 구현하기 위한 간단한 방법 중 하나입니다.
여러 속성이있는 POJO 클래스의 경우 일반적으로 동일한 해시 코드 구현은 대부분의 경우 동일합니다. 따라서 각 POJO 클래스에서 동일 및 해시 코드에 대한 코드를 별도로 작성하는 대신 아파치는 아래 예제와 같이 동일 및 해시 코드 방법을 구현하는 쉽고 간단한 방법을 제공합니다.

아래 예제에서 Person 은 equals() 및 hashCode() 구현이 필요한 POJO 클래스입니다. 따라서 사용자 클래스의 모든 속성을 사용하여 equals를 수행하고 hashCode를 제공하는 아파치 공통 lang API에서 제공하는 빌더 클래스를 사용할 수 있습니다.

import org.apache.commons.lang3.builder.EqualsBuilder;

public class Marks {
    final int maths, physics, chemistry;

    Marks(int maths, int physics, int chemistry){
        this.maths = maths;
        this.physics = physics;
        this.chemistry = chemistry;
    }

     @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 79 * hash + this.maths;
        hash = 79 * hash + this.physics;
        hash = 79 * hash + this.chemistry;
        return hash;
    }
}
import org.apache.commons.lang3.builder.EqualsBuilder;

public class Student {
    Marks marks;
    String firstName, lastName;
    final int id;

    Student(int id, String firstName, String lastName, Marks marks){
        this.firstName = firstName;
        this.lastName = lastName;
        this.marks = marks;
        this.id = id;
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 17 * hash + this.id;
        return hash;
    }

    /**
     *
     * @param args
     */
    public static void main(String args[]){
        Marks m1 = new Marks(95, 85, 75);
        Marks m2 = new Marks(95, 85, 75);
        Student s1 = new Student(1, "Hari", "Krishna", m1);
        Student s2 = new Student(1, "Hari", "Krishna", m2);

        System.out.println("Is s1 and s2 are equal "+s1.equals(s2));

        m1 = new Marks(95, 85, 75);
        m2 = new Marks(95, 85, 76);
        s2 = new Student(1, "Hari", "Krishna", m2);
        System.out.println("Is s1 and s2 are equal "+s1.equals(s2));
    }
}

Is s1 and s2 are equal true
Is s1 and s2 are equal false

'[개발] 지식 > 미분류' 카테고리의 다른 글

[Linux] find 명령어  (0) 2020.11.25
Workspace associated with branch '[branch name]' has been restored  (0) 2020.11.12
*정규표현식 기초  (0) 2020.05.07
<