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

前端 未结 2 821
暖寄归人
暖寄归人 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<Integer> setter,
            BiConsumer<EventDocument, Integer> 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);
    }
    
    0 讨论(0)
  • 2021-01-28 13:31

    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.

    The same applies for the experimental @Accessor feature.

    0 讨论(0)
提交回复
热议问题