XwAttrVo xwAttr = new XwAttrVo(); xwAttr.setAttrId(new BigDecimal(attrId)); xwAttr.setBusiTypeId(new BigDecimal(req.getBusiTypeId())); xwAttr.setParAttrId(new BigDecimal(req.getParAttrId())); xwAttr.setAttrNbr(req.getAttrNbr()); xwAttr.setAttrName(req.getAttrName()); xwAttr.setAttrDesc(req.getAttrDesc()); xwAttr.setDefaultValue(req.getDefaultValue()); xwAttr.setValueFrom(req.getValueFrom()); xwAttr.setValueTo(req.getValueTo()); xwAttr.setIsUnique(new BigDecimal(req.getIsUnique())); xwAttr.setIsNullable(new BigDecimal(req.getIsNullable())); xwAttr.setAttrValueDataType(req.getAttrValueDataType()); xwAttr.setIsDanyAttr(new BigDecimal(req.getIsDanyAttr())); xwAttr.setAttrValueType(req.getAttrValueType()); xwAttr.setAttrFormat(req.getAttrFormat()); xwAttr.setAttrLength(new BigDecimal(req.getAttrLength())); xwAttr.setStatusCd("1000"); xwAttr.setCreateStaff(new BigDecimal(req.getCreateStaff())); xwAttr.setUpdateStaff(new BigDecimal(req.getUpdateStaff())); xwAttr.setCreateDate(new Date()); xwAttr.setStatusDate(new Date()); xwAttr.setUpdateDate(new Date()); xwAttr.setRemark(req.getRemark()); xwAttr.setUnitCd(new BigDecimal(req.getUnitCd())); xwAttr.setExtAttrId(req.getExtAttrId()); xwAttr.setGrpAttrNbr(req.getGrpAttrNbr()); xwAttr.setShowFlag(new BigDecimal(req.getShowFlag())); xwAttr.setModFlag(new BigDecimal(req.getModFlag())); xwAttr.setFormatMsg(req.getFormatMsg()); xwAttr.setHasLinkCtrl(req.getHasLinkCtrl()); xwAttr.setPrivCode(req.getPrivCode()); xwAttr.setBusiObjNbr(req.getBusiObjNbr()); 使用 Java 8 的 Optional 类和流式操作来简化这段代码
时间: 2024-04-13 20:30:27 浏览: 117
使用 Java 8 的 Optional 类和流式操作可以简化这段代码。可以通过创建一个方法来处理属性的判空和转换,并使用流式操作将所有属性进行处理。以下是简化后的代码示例:
```java
import java.math.BigDecimal;
import java.util.Date;
import java.util.Optional;
public class Main {
public static void main(String[] args) {
XwAttrVo xwAttr = new XwAttrVo();
xwAttr.setAttrId(new BigDecimal(attrId));
xwAttr.setBusiTypeId(Optional.ofNullable(req.getBusiTypeId()).map(BigDecimal::new).orElse(null));
xwAttr.setParAttrId(Optional.ofNullable(req.getParAttrId()).map(BigDecimal::new).orElse(null));
xwAttr.setAttrNbr(req.getAttrNbr());
xwAttr.setAttrName(req.getAttrName());
xwAttr.setAttrDesc(req.getAttrDesc());
xwAttr.setDefaultValue(req.getDefaultValue());
xwAttr.setValueFrom(req.getValueFrom());
xwAttr.setValueTo(req.getValueTo());
xwAttr.setIsUnique(Optional.ofNullable(req.getIsUnique()).map(BigDecimal::new).orElse(null));
xwAttr.setIsNullable(Optional.ofNullable(req.getIsNullable()).map(BigDecimal::new).orElse(null));
xwAttr.setAttrValueDataType(req.getAttrValueDataType());
xwAttr.setIsDanyAttr(Optional.ofNullable(req.getIsDanyAttr()).map(BigDecimal::new).orElse(null));
xwAttr.setAttrValueType(req.getAttrValueType());
xwAttr.setAttrFormat(req.getAttrFormat());
xwAttr.setAttrLength(Optional.ofNullable(req.getAttrLength()).map(BigDecimal::new).orElse(null));
xwAttr.setStatusCd("1000");
xwAttr.setCreateStaff(new BigDecimal(req.getCreateStaff()));
xwAttr.setUpdateStaff(new BigDecimal(req.getUpdateStaff()));
xwAttr.setCreateDate(new Date());
xwAttr.setStatusDate(new Date());
xwAttr.setUpdateDate(new Date());
xwAttr.setRemark(req.getRemark());
xwAttr.setUnitCd(Optional.ofNullable(req.getUnitCd()).map(BigDecimal::new).orElse(null));
xwAttr.setExtAttrId(req.getExtAttrId());
xwAttr.setGrpAttrNbr(req.getGrpAttrNbr());
xwAttr.setShowFlag(Optional.ofNullable(req.getShowFlag()).map(BigDecimal::new).orElse(null));
xwAttr.setModFlag(Optional.ofNullable(req.getModFlag()).map(BigDecimal::new).orElse(null));
xwAttr.setFormatMsg(req.getFormatMsg());
xwAttr.setHasLinkCtrl(req.getHasLinkCtrl());
xwAttr.setPrivCode(req.getPrivCode());
xwAttr.setBusiObjNbr(req.getBusiObjNbr());
}
}
```
通过使用 `Optional` 类和流式操作,我们可以将属性的判空和转换逻辑封装起来,并且使用流式操作简化代码。将所有属性通过流式操作一一处理,可以避免重复的判空和转换代码,使代码更加简洁和易读。
阅读全文