13 Nisan 2021 Salı

Lombok @Singular Anotasyonu

Giriş
Şu satırı dahil ederiz
import lombok.Singular;
Açıklaması şöyle
When the @Singular annotation is placed on a collection property, Lombok creates special builder methods to individually add items to that collection, rather than adding the entire collection at once. This is particularly nice for tests as creating small collections in Java is not concise.
Örnek
Şöyle yaparız
@Value
@Builder(toBuilder = true)
public class User {
  @NonNull
  UUID userId;
  @NonNull
  String email;
  @Singular
  Set<String> favoriteFoods;
  @NonNull
  @Builder.Default
  String avatar = “default.png”;
}

User user = User.builder()
  .userId(UUID.random())
  .email(“grubhub@grubhub.com”)
  .favoriteFood(“burritos”)
  .favoriteFood(“dosas”)
  .build()

Hiç yorum yok:

Yorum Gönder