龙目岛-如何创建自定义设置器并应用于Java中的其他成员 [英] Lombok - how to create custom setters and apply on different member in java

查看:103
本文介绍了龙目岛-如何创建自定义设置器并应用于Java中的其他成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何在Lombok中创建自定义设置器并将该设置器应用于特定成员.我有一个具有100个成员的类,其中有50个成员有一个自定义设置器,该设置器在设置值之前检查X值,另一个50个具有自定义设置器,在设置值之前检查Y值.能做到吗 这是一个例子, 2名成员2名二传手, 此代码将为我班上的所有成员重复:

I would like to understand how to create a custom setter in Lombok and apply the setter on specific member. I have a class with 100 members, and for 50 of them I have a custom setter that check something X before I set the value, and another 50 that have a custom setter that check something Y before the I set the value. Can it be done? this is a exmple , 2 members 2 diffrent setters , this code is repeated for all members in my class :

@JsonProperty("TAC_LAC_Start_UE1")
private Integer tacLacStartUe1;

@JsonProperty("TAC_LAC_Start_UE2")
private Integer tacLacStartUe2;

@Override
public void setTacLacStartUe1(Integer tacLacStartUe1) {
    if (Objects.equals(getTacLacStartUe1(), tacLacStartUe1)) {
        return;
    }
    this.tacLacStartUe1 = tacLacStartUe1;
    if (DocKind.ORIG == docKind) {
        ((EventDocument) prepareDirtyDocument()).setTacLacStartUe1(tacLacStartUe1);
    }
}

@Override
public Integer getTacLacStartUe2() {
    return tacLacStartUe2;
}

@Override
public void setTacLacStartUe2(Integer tacLacStartUe2) {
    if (Objects.equals(getTacLacStartUe2(), tacLacStartUe2)) {
        return;
    }
    this.tacLacStartUe2 = tacLacStartUe2;
    if (DocKind.ORIG == docKind) {
        ((EventDocument) prepareDirtyDocument()).setTacLacStartUe2(tacLacStartUe2);
    }
}

推荐答案

基于当前版本的文档( https: //projectlombok.org/features/GetterSetter ),它似乎没有提供一种为setter(或getter)指定自定义检查的方法.我担心您将不得不手动编写每个设置器.

Based on the current version's documentation (https://projectlombok.org/features/GetterSetter), it doesn't seem to include a way to specify custom checks for the setter (or getter). I fear you will have to manually code each and every setter.

实验性@Accessor 功能也是如此.

这篇关于龙目岛-如何创建自定义设置器并应用于Java中的其他成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆