Lombok - how to create custom setters and apply on different member in java

前端 未结 2 820
暖寄归人
暖寄归人 2021-01-28 12:53

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 s

2条回答
  •  北海茫月
    2021-01-28 13:26

    As @Laf said, Lombok doesn't currently support this feature. However, you still can get rid of some duplicated code by extracting setters logic to the following higher-order function:

    private void doSetTacLacStartUe(
            Integer oldValue,
            Integer newValue,
            Consumer setter,
            BiConsumer eventDocumentUpdater
    ) {
        if (Objects.equals(oldValue, newValue)) return;
        setter.accept(newValue);
        if (DocKind.ORIG == docKind)
            eventDocumentUpdater.accept((EventDocument) prepareDirtyDocument(), newValue);
    }
    

    And using it this way:

    public void setTacLacStartUe1(Integer tacLacStartUe1) {
        doSetTacLacStartUe(getTacLacStartUe1(), tacLacStartUe1, it -> this.tacLacStartUe1 = it, EventDocument::setTacLacStartUe1);
    }
    

提交回复
热议问题