본문 바로가기

Project/Table_of_Organization_Management_System

@EqualsAndHashCode

728x90
/*
 * Copyright (C) 2009-2020 The Project Lombok Authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package lombok;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * equals, hashCode 를 구현해준다.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface EqualsAndHashCode {
 /**
  * @Deprecated
  * 이 속성을 사용한 필드는 제외된다.
  * 현재는 필드에 @EqualsAndHashCode.Exclude 를 사용하는 것을 권장한다.
  */
 String[] exclude() default {};
 
 /**
  * @Deprecated
  * 명시적으로 사용할 필드를 정의한다. 
  * 사용하지 않을경우 일반적으로  all (non-static, non-transient) (모든) 필드가 사용된다.
  * of 속성보다는 @EqualsAndHashCode(onlyExplicitlyIncluded = true)와
  * 필드에 @EqualsAndHashCode.Include 를 사용하자.
  */
 String[] of() default {};
 
 /**
  * 이 속성은 상위클래스의 equals , hashCode 를 먼저 호출한다.
  */
 boolean callSuper() default false;
 
 /**
  * getter 메서드를 호출하지 않고 직접 필드에 접근한다.
  */
 boolean doNotUseGetters() default false;

 /**
  * hashCode 의 결과가 캐시되는 전략
  */
 CacheStrategy cacheStrategy() default CacheStrategy.NEVER;
 
 /**
  *  equals 메서드의 매개변수에 어노테이션을 적용된다.
  * ex) 
  * @EqualsAndHashCode(onParam=@__({@NonNull}))
  * ...
  * public boolean equals(@NonNull final java.lang.Object o) {
  * ...}
  */
 AnyAnnotation[] onParam() default {};
 
 /**
  * Placeholder annotation to enable the placement of annotations on the generated code.
  * @deprecated Don't use this annotation, ever - Read the documentation.
  */
 @Deprecated
 @Retention(RetentionPolicy.SOURCE)
 @Target({})
 @interface AnyAnnotation {}
 
 /**
  * @EqualsAndHashCode.Include 를 명시적으로 선언된 필드만 포함시킨다.
  * true 로 설정시 non-static, non-transient 필드는 자동으로 포함하지 않는다.
  */
 boolean onlyExplicitlyIncluded() default false;
 
 /**
  * 제외시킨다.
  */
 @Target(ElementType.FIELD)
 @Retention(RetentionPolicy.SOURCE)
 public @interface Exclude {}
 
 /**
  * 포함시킨다.
  */
 @Target({ElementType.FIELD, ElementType.METHOD})
 @Retention(RetentionPolicy.SOURCE)
 public @interface Include {
  /**
   * 기본값은 필드와 같은 메서드가 존재하면 필드가 사용되지만 replaces를 사용하면,
   * 필드와 이름이 같은 return 값을 가진 메서드가 있다면 필드를 대체한다.
   */
  String replaces() default "";

  /**
   * 더 높은 숫자로 지정된 필드가 먼저 equals, hashCode 에 비교대상이된다.
   */
  int rank() default 0;
 }

 public enum CacheStrategy {
  /**
   * Never cache. Perform the calculation every time the method is called.
   */
  NEVER,
  /**
   * hashCode 의 첫 번째 호출 결과를 캐시하고 후속 호출에 사용한다.
   * hashCode 계산에 사용되는 모든 필드가 변경 불가능한 경우 성능이 향상될 수 있다.
   * 따라서 hashCode 를 호출할 때마다 항상 동일한 값이 반환된다.
   * hashCode} 호출을 다른곳에서 할 가능성이 있는 경우 사용하지 말자.
   * 원치 않는 값이 반환될 수 있다.
   */
  LAZY
 }
}

 


사용예

@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
public class ExampleDTO {

@EqualsAndHashCode.Include
    private Long id = 0L;
    
    private String content;
}
728x90