java 8 空指针_Java8之后的业务取值避免空指针的一种解法

本文介绍了如何使用Java 8的Optional特性以及自定义工具类OptionalUtil来避免在处理业务数据时出现空指针异常。通过示例展示了在对象字段复制和装饰器风格代码中,如何更优雅地处理可能为null的值,使得代码更加简洁和易读。
摘要由CSDN通过智能技术生成

写业务代码的时候,通常会遇到数据库POJO对象转换为前端需要的VO对象,这时经常会遇到烦人的空指针问题,Java 8之前,我们可能这么写:

对于对象field复制风格:

a.setCreateTime(b.getCreateTime().getTime());

a.setAmount(b.getPayed()+b.getVoucher());

对于装饰器风格:

public Long getCreateTime() {

return b.getCreateTime().getTime();

}

public Long getAmount() {

return b.getPayed()+b.getVoucher()

}

很明显,这些代码会有空指针异常的风险.一般我们会这么修改:

对于对象field复制风格:

if (b.getCreateTime()!= null) {

a.setCreateTime(b.getCreateTime().getTime());

}

if (b.getPayed() != null) {

a.setAmount(b.getPayed();

} else {

a.setAmount(0L);

}

if (b.getVoucher() != null) {

a.setAmount(a.getAmount()+b.getVoucher());

}

对于装饰器风格:

public Long getCreateTime() {

if (b.getCreateTime()!= null) {

return b.getCreateTime().getTime();

} else {

return null;

}

}

public Long getAmount() {

Long result = 0L;

if (b.getPayed() != null) {

result += b.getPayed();

}

if (b.getVoucher() != null) {

result += b.getVoucher();

}

return result;

}

Java8引入的Optional写法:

对于对象field复制风格:

Optional.ofNullable(b.getCreateTime()).ifPresent(timestamp -> a.setCreateTime(timestamp.getTime()));

a.setAmount(Optional.ofNullable(b.getPayed()).orElse(0L) + Optional.ofNullable(b.getVoucher()).orElse(0L));

对于装饰器风格:

public Long getCreateTime() {

Optional createTime = Optional.ofNullable(b.getCreateTime());

if (createTime.isPresent()) {

return createTime.get();

} else {

return null;

}

}

public Long getAmount() {

return Optional.ofNullable(b.getPayed()).orElse(0L) + Optional.ofNullable(b.getVoucher()).orElse(0L)

}

这样依然很复杂,尤其是嵌套多层之后。 更进一步,有没有通用简便的方法呢?联想到Java8的Functional Interface以及我们需要处理的异常只有空指针异常,可以写工具类:

import java.util.function.Supplier;

public class OptionalUtil {

/**

* 忽略NullPointerException的获取

* @param supplier

* @param

* @return 如果有空指针,返回null

*/

public static T orNull(Supplier supplier) {

try {

return supplier.get();

} catch (NullPointerException e) {

return null;

}

}

/**

* 忽略NullPointerException的获取

*

* @param supplier

* @param or

* @param

* @return 如果有空指针,返回or

*/

public static T or(Supplier supplier, T or) {

try {

T t = supplier.get();

if (t != null) {

return t;

}

return or;

} catch (NullPointerException e) {

return or;

}

}

}

这样,我们的代码就变得简洁多了:

对于对象field复制风格:

a.setCreateTime(OptionalUtil.orNull(() -> b.getCreateTime().getTime()));

a.setAmount(OptionalUtil.or(() -> b.getPayed(), 0L) + OptionalUtil.or(() -> b.getAmount(), 0L));

对于装饰器风格:

public Long getCreateTime() {

return OptionalUtil.orNull(() -> b.getCreateTime().getTime());

}

public Long getAmount() {

return OptionalUtil.or(() -> b.getPayed(), 0L) + OptionalUtil.or(() -> b.getAmount(), 0L);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值