写业务代码的时候,一般会遇到数据库POJO对象转换为前端须要的VO对象,这时常常会遇到烦人的空指针问题,Java 8以前,咱们可能这么写:前端
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() }
很明显,这些代码会有空指针异常的风险.通常咱们会这么修改:java
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写法:数据库
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<Timestamp> 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 <T> * @return 若是有空指针,返回null */ public static <T> T orNull(Supplier<T> supplier) { try { return supplier.get(); } catch (NullPointerException e) { return null; } } /** * 忽略NullPointerException的获取 * * @param supplier * @param or * @param <T> * @return 若是有空指针,返回or */ public static <T> T or(Supplier<T> supplier, T or) { try { T t = supplier.get(); if (t != null) { return t; } return or; } catch (NullPointerException e) { return or; } } }
这样,咱们的代码就变得简洁多了:指针
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); }