C005.23届0609怪
C005.23届0609怪
1.CompanySearchService.java 64-64
gigax 479a5bd2922b52f1b5d0cde70dd0eaacdb8063d7
List<Long> searchLadingId(@NotNull Long companyId);
//
List<Long> searchLadingId(long companyId);
时刻铭记NotNull
标记@NotNull
有利于提高代码质量,此时@NotNull Long
等于 long
。 每当看到一个变量(输入,输出,内部变量),脑子里过一遍其边界条件和特殊值,能提高编程意识。
2.DashboardServiceImpl.java 274-277
gigax 479a5bd2922b52f1b5d0cde70dd0eaacdb8063d7
List<Long> ids = new ArrayList<>(data.size());
for (OrderDetail datum : data) {
ids.add(datum.getId());
}
// 修改后
List<Long> ids = data.stream().map(OrderDetail::getId).toList();
一步操作可用lambda
wings鼓励使用普通的for-loop取代lambda的for-each,主要考虑到可读性及少量性能。 一般一步操作,只读的情况,使用lambda的可读性更佳。
3.DashboardServiceImpl.java 303-312
gigax 479a5bd2922b52f1b5d0cde70dd0eaacdb8063d7
// orders is NOT empty
StringBuilder dt = new StringBuilder();
StringBuilder pc = new StringBuilder();
for (OwtLadingOrder order : orders) {
dt.append(order.getDeliveryType());
dt.append(" : ");
pc.append(order.getPredictCtn());
pc.append(" : ");
}
dt.delete(dt.length() - 3, dt.length());
pc.delete(pc.length() - 3, pc.length());
字符链接(分隔)小技巧
如果简单的集合类或数组,可以使用Joiner类型的方法或工具。 如果必须手动拼接,建议joiner在前,然后使用substring的小技巧。
final StringBuilder dt = new StringBuilder();
final StringBuilder pc = new StringBuilder();
final String joiner = " : ";
for (OwtLadingOrder order : orders) {
dt.append(joiner);
dt.append(order.getDeliveryType());
pc.append(joiner);
pc.append(order.getPredictCtn());
}
datum.setDeliveryType(dt.substring(joiner.length()));
datum.setPredictCtn(pc.substring(joiner.length()));
4.EasyExcelServiceImpl.java 65-71
gigax 479a5bd2922b52f1b5d0cde70dd0eaacdb8063d7
catch (ExcelAnalysisException e) {
if (e.getCause() != null && e.getCause() instanceof CodeException) {
throw (CodeException) e.getCause();
}
else {
throw e;
}
}
// 使用Pattern Matching for instanceof简化if分支
if (e.getCause() instanceof CodeException ce) {
throw ce;
}
// 三元操作符再次简化
catch (ExcelAnalysisException e) {
throw e.getCause() instanceof CodeException ce ? ce : e;
}
instanceof
包括了not null
的情况Pattern Matching for instanceof
可以智能转换类型
5.QuoteServiceImpl.java 138-140
gigax 96319f11ac5109ea01f97b57f443e922ac6605d7
@NotNull
private List<QuoteVersionVo> transferVo(List<OwtQuoteVersion> owtQuoteVersions) {
List<QuoteVersionVo> vos = new ArrayList<>(owtQuoteVersions.size());
if (owtQuoteVersions.isEmpty()) return vos;
// ① 若内部对类型无要求,仅是List,可以
if (owtQuoteVersions.isEmpty()) return Collections.emptyList();
List<QuoteVersionVo> vos = new ArrayList<>(owtQuoteVersions.size());
// ② 若要求返回值可修改,则维持现状,以明确类型ArrayList代替List
6.LadingFileServiceImpl.java 63-70
gigax 96319f11ac5109ea01f97b57f443e922ac6605d7
Integer latestVersion = owtLadingFileDao
.ctx()
.select(DSL.max(ft.Version))
.from(ft)
.where(ft.LadingId.eq(ladingId))
.and(ft.FileType.eq(fileType))
.fetchOneInto(Integer.class);
if (EmptyUtil.is(latestVersion)) latestVersion = 0;
// 修改后,使用Jooq的fetchOptionalInto和orElse,替换掉null判断赋值
Integer latestVersion = owtLadingFileDao
.ctx()
.select(DSL.max(ft.Version))
.from(ft)
.where(ft.LadingId.eq(ladingId))
.and(ft.FileType.eq(fileType))
.fetchOptionalInto(Integer.class)
.orElse(0);
7.LadingInterceptServiceImpl.java 411-412
gigax 96319f11ac5109ea01f97b57f443e922ac6605d7
final OwtLadingOrder remainingOrder = new OwtLadingOrder();
BeanUtils.copyProperties(originOrder, remainingOrder);
// ①使用jooq的from强类型复制,替换到动态的copyProperties
remainingOrder.from(originOrder);
// ②直接new
final OwtLadingOrder remainingOrder = new OwtLadingOrder(originOrder);
8.LadingExpressService.java 15-18
gigax d66d6f88aef3d2bf0fdf88bb04b80dd92c34b224
void create(List<OwtLadingExpress> owtLadingExpresses,
List<LadingService.ExpressParcel> expressParcels,
OwtLadingMain owtLadingMain,
List<OwtLadingOrder> owtLadingOrders);
// 使用更多注解,标注方法参数
void create(@NotNull OwtLadingMain owtLadingMain,
@NotNull List<LadingService.ExpressParcel> expressParcels,
@Param.Out List<OwtLadingOrder> owtLadingOrdersOut,
@Param.InOut List<OwtLadingExpress> owtLadingExpressesInOut);
// @Param.Out和@Param.InOut相当于C# in及out修饰符,提醒使用者参数有修改
9.LadingOrderServiceImpl.java 450-464
gigax d66d6f88aef3d2bf0fdf88bb04b80dd92c34b224
@Override
public boolean isAllUpsFedex(List<OwtLadingOrder> owtLadingOrders, List<OwtLadingExpress> owtLadingExpresses, DeliveryType deliveryType) {
if (EmptyUtil.is(owtLadingOrders))
return false;
boolean result = true;
for (OwtLadingOrder order : owtLadingOrders) {
if (!result) break;
result = order.getDeliveryType().equals(deliveryType);
}
if (!EmptyUtil.is(owtLadingExpresses)) {//继续判断需要扫描的快递货物
for (OwtLadingExpress express : owtLadingExpresses) {
if (!result) break;
result = express.getDeliveryType().equals(deliveryType);
}
}
return result;
}
修改后,移除方法内全局flag,使用短路替换判断逻辑。
@Override
public boolean isAllUpsFedex(List<OwtLadingOrder> owtLadingOrders, List<OwtLadingExpress> owtLadingExpresses, @NotNull DeliveryType deliveryType) {
if (EmptyUtil.is(owtLadingOrders)) return false;
for (OwtLadingOrder order : owtLadingOrders) {
if (order.getDeliveryType() != deliveryType) return false;
}
//继续判断需要扫描的快递货物
for (OwtLadingExpress express : Null.notNull(owtLadingExpresses)) {
if (express.getDeliveryType() != deliveryType) return false;
}
return true;
}
10.LadingStaticServiceImpl.java 234-242
gigax d66d6f88aef3d2bf0fdf88bb04b80dd92c34b224
boolean allPlate = true;
Set<Long> orderIdSet = orders.stream().map(OwtLadingOrder::getId).collect(Collectors.toSet());
Set<Long> updateOrderIdSet = updateOrder.stream().map(OwtLadingOrder::getId).collect(Collectors.toSet());
for (Long orderId : orderIdSet) {
if (!updateOrderIdSet.contains(orderId)) {
allPlate = false;
break;
}
}
// 修改后,语义上等同于判断两个集合相等,即其内元素是否相等。
Set<Long> updateOrderIdSet = updateOrder.stream().map(OwtLadingOrder::getId).collect(Collectors.toSet());
Set<Long> orderIdSet = orders.stream().map(OwtLadingOrder::getId).collect(Collectors.toSet());
final boolean allPlate = orderIdSet.equals(updateOrderIdSet);
11.KapaiSearchService.java 22-24
gigax d66d6f88aef3d2bf0fdf88bb04b80dd92c34b224
@Data
class Details {
private String address;
/**
* 关联唛头
*/
private int assMark;
private String airBol;
private String seaBol;
// 元芳你怎么看,第二行这个前缀有点嘎!