使用 lombok 定义继承类的默认值

发布于 2025-01-09 01:35:01 字数 1094 浏览 2 评论 0原文

因此,我正在开发一个通知服务,目前只有一个 Notification 和一个 Email 实体,其中 Email 扩展了Notification。

Notification 实体有一个名为 type 的列,它存储通知的类型(即:EMAIL、MESSAGE、PUSH 等),我想知道是否有办法使用 lombok 时,为 Notification 的每个子项的 type 列定义默认值。

我发现一种常见的做法是在构造函数上设置类型,如下所示:

public Email() {
    setType("EMAIL");
}

但我正在使用 lombok 的构建器来实例化 Email,如下所示:

public static Email fromEmailRequest(final EmailRequest emailRequest) {
        if (Objects.isNull(emailRequest)) {
            return Email.builder().build();
        }
        Set<String> set = new LinkedHashSet<>(emailRequest.getRecipients());
        return Email.builder()
                .content(emailRequest.getContent())
                .recipients(Lists.newArrayList(set))
                .sender(emailRequest.getSender())
                .subject(emailRequest.getSubject())
                .type(NotificationTypeEnum.EMAIL)
                .build();
}

我现在使用的方法将类型设置为请求 DTO 映射器,但我认为这个逻辑应该位于实体中的某个位置。

So I am developing a notification service which, for now, only has a Notification and an Email entity, where Email extends Notification.

The Notification entity has a column named type which stores the type of the notification (ie: EMAIL, MESSAGE, PUSH, etc) and I want to know if there is a way to define a default value for the type column for each of the childs of Notification while using lombok.

I see that a common practice is to set the type on the constructor, like this:

public Email() {
    setType("EMAIL");
}

But I am using the builder from lombok to instatiate Email, like this:

public static Email fromEmailRequest(final EmailRequest emailRequest) {
        if (Objects.isNull(emailRequest)) {
            return Email.builder().build();
        }
        Set<String> set = new LinkedHashSet<>(emailRequest.getRecipients());
        return Email.builder()
                .content(emailRequest.getContent())
                .recipients(Lists.newArrayList(set))
                .sender(emailRequest.getSender())
                .subject(emailRequest.getSubject())
                .type(NotificationTypeEnum.EMAIL)
                .build();
}

The approach I am using now sets the type in the request DTO mapper, but I think this logic should be somewhere in the entities.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2025-01-16 01:35:01

也许有更好的方法,但您可以在 Notification 类中创建一个以类型作为参数的构造函数,然后为您的 Email 类手动创建一个全参数构造函数。 Lombok 将使用此构造函数作为构建器,而不是生成一个。

public class Notification {

    private final String type;

    protected Notification(String type) {
        this.type = type;
    }
}

@Builder
public class Email extends Notification {

    private String test;

    private Email(String test) {
        super("EMAIL");
        this.test = test;
    }
}

@SuperBuilder 使用起来有点复杂,但想法是相同的。您仍然可以自己编写通常由 lombok 生成的部分代码,并使用您自己的代码更改其行为。

因此,您自己实现 build 方法并将 type 设置为 EMAIL

@SuperBuilder(toBuilder = true)
public class Email extends Notification {

    private String test;

    public static class EmailBuilderImpl extends EmailBuilder<Email, EmailBuilderImpl> {
        /* Make sure the value is never accidently set to something other than `EMAIL`*/
        @Override
        public EmailBuilderImpl type(String type) {
            if (!type.equals("EMAIL")) {
                throw new UnsupportedOperationException();
            }
            return super.type(type);
        }

        public Email build() {
            return new Email(type("EMAIL"));
        }
    }
}

Maybe there is a better way, but you can create a constructor inside your Notification class which has the type as parameter and then manually create an all args constructor for your Email class. Lombok will use this constructor for the builder instead of generating one.

public class Notification {

    private final String type;

    protected Notification(String type) {
        this.type = type;
    }
}

@Builder
public class Email extends Notification {

    private String test;

    private Email(String test) {
        super("EMAIL");
        this.test = test;
    }
}

@SuperBuilder is a bit more complex to work with, but the idea is the same. You can still write parts of the code that is normally generated by lombok yourself and change its behavior with your own code.

So you implement the build method yourself and set the type to EMAIL.

@SuperBuilder(toBuilder = true)
public class Email extends Notification {

    private String test;

    public static class EmailBuilderImpl extends EmailBuilder<Email, EmailBuilderImpl> {
        /* Make sure the value is never accidently set to something other than `EMAIL`*/
        @Override
        public EmailBuilderImpl type(String type) {
            if (!type.equals("EMAIL")) {
                throw new UnsupportedOperationException();
            }
            return super.type(type);
        }

        public Email build() {
            return new Email(type("EMAIL"));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文