Setter中进行null和空值检查的高效方法是什么?

huangapple go评论71阅读模式
英文:

What is the efficient way of null and empty check in setter

问题

我需要为我的DTO类的27个字段中的每一个添加null和空值检查,并且我正在寻找最有效的方法来完成它。有人可以提出任何想法吗?

private String settingType;
private String settingCode;
private String executionEndDate;
private String startToday;
if (!ObjectUtils.isEmpty(updateRequest.getSettingType())) {
    builder.setSettingType(updateRequest.getSettingType());
}
if (!ObjectUtils.isEmpty(updateRequest.getSettingCode())) {
    builder.setSettingCode(updateRequest.getSettingCode());
}
if (!ObjectUtils.isEmpty(updateRequest.getExecutionEndDate())) {
    builder.setExecutionEndDate(updateRequest.getExecutionEndDate());
}
if (!ObjectUtils.isEmpty(updateRequest.isStartToday())) {
    builder.setStartToday(updateRequest.getStartToday());
}
英文:

I need to add null and empty checks to each of the 27 fields in my DTO class, and I'm searching for the most effective method to accomplish it. Can anyone suggest any ideas?

private String settingType;
private String settingCode;
private String executionEndDate;
private String startToday;


        if (!ObjectUtils.isEmpty(updateRequest.getSettingType())) {
            builder.setSettingType(updateRequest.getSettingType());
        }
        if (!ObjectUtils.isEmpty(updateRequest.getSettingCode())) {
            builder.setSettingCode(updateRequest.getSettingCode());
        }
        if (!ObjectUtils.isEmpty(updateRequest.getExecutionEndDate())) {
            builder.setExecutionEndDate(updateRequest.getExecutionEndDate());
        }
        if (!ObjectUtils.isEmpty(updateRequest.isStartToday())) {
            builder.setStartToday(updateRequest.getStartToday());
        }

答案1

得分: 4

这个方法可以使用消费者方法来实现:

import java.util.function.Consumer;

private <T> void setFieldIfNotEmpty(Consumer<T> setter, T value) {
    if (ObjectUtils.isNotEmpty(value)) {
        setter.accept(value);
    }
}

然后可以使用上面的方法进行空值和空检查,如下所示:

setFieldIfNotEmpty(builder::setSettingType, updateRequest.getSettingType());

setFieldIfNotEmpty(builder::setSettingCode, updateRequest.getSettingCode());

builder.setStartToday(updateRequest.getStartToday());

setFieldIfNotEmpty(builder::setExecutionEndDate, updateRequest.getExecutionEndDate());

就像这样。

英文:

We can use this method using consumer

import java.util.function.Consumer;

    private &lt;T&gt; void setFieldIfNotEmpty(Consumer&lt;T&gt; setter, T value) {
        if (ObjectUtils.isNotEmpty(value)) {
            setter.accept(value);
        }
    }

Then use the above method for null and empty check as well


		setFieldIfNotEmpty(builder::setSettingType, updateRequest.getSettingType());

        setFieldIfNotEmpty(builder::setSettingCode, updateRequest.getSettingCode());

        builder.setStartToday(updateRequest.getStartToday());

        setFieldIfNotEmpty(builder::setExecutionEndDate, updateRequest.getExecutionEndDate());

Like this

答案2

得分: 2

一种方法是将可能返回null值的方法更改为返回Optional类型,例如:

Optional<String> getSettingType() {
    return Optional.ofNullable(settingType);
}

这有一个优点,即它具有自我文档化的特点(可能不返回值的事实现在是明确的),还意味着您可以用类似以下方式替换您的代码:

updateRequest.getSettingType().ifPresent(builder::setSettingType);
updateRequest.getSettingCode().ifPresent(builder::setSettingCode);
updateRequest.getExecutionEndDate().ifPresent(builder::setExecutionEndDate);
updateRequest.getStartToday().ifPresent(builder::setStartToday);

这种方法确实会创建短暂的Optional对象,因此是否足够"高效"是您需要决定的事情。

英文:

One approach is to change methods that might return null values to instead return Optional types, e.g.:

Optional&lt;String&gt; getSettingType() {
    return Optional.ofNullable(settingType);
}

This has the advantage of being self-documenting (the fact that it might return no value is now explicit), and also means you can replace your code with something like this:

updateRequest.getSettingType().ifPresent(builder::setSettingType);
updateRequest.getSettingCode().ifPresent(builder::setSettingCode);
updateRequest.getExecutionEndDate().ifPresent(builder::setExecutionEndDate);
updateRequest.getStartToday().ifPresent(builder::setStartToday);

THis approach does mean creating short-lived Optional objects, so whether it's sufficiently "efficient" is something you would need to decide.

答案3

得分: 0

1.) 最好在构造函数中初始化一些值,以避免任何字段获得空值。

2.) Java 8 的 Optional 对于检查空值将非常有益。

英文:

1.) It would be better to initialise some value in the constructor, So that it wouldn't got the null value for any fields.

2.) Java 8 optional would be more than beneficial for null values check.

huangapple
  • 本文由 发表于 2023年5月17日 15:55:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定