isRidectBuy = productData ==null?false:true
时间: 2024-03-22 14:37:20 浏览: 43
这是一个三目运算符,用于判断 productData 是否为 null,如果是则将 isRidectBuy 设为 false,否则设为 true。可以理解为:
```
if (productData == null) {
isRidectBuy = false;
} else {
isRidectBuy = true;
}
```
该语句的作用是根据 productData 是否为空来判断用户是从商品详情页面直接购买还是从购物车结算。如果为空,则用户是从购物车结算,否则用户是从商品详情页面直接购买。
相关问题
private void init() { productData = (ProductBean) getIntent().getSerializableExtra("productData"); quantity = getIntent().getIntExtra("quantity",1); isRidectBuy = productData ==null?false:true; }
这是一个私有方法 init(),用于初始化数据。在方法内部,首先通过 getIntent() 方法获取从上一个 Activity 传递过来的数据,包括 ProductBean 类型的 productData 和 int 类型的 quantity,分别通过 getSerializableExtra() 和 getIntExtra() 方法获取。接着判断 productData 是否为空,如果为空则将 isRidectBuy 设为 false,否则设为 true。这些数据可能是用于购物车或确认订单等功能的数据。
switch (goodsItem.getProductType()) { case 1://爆品 GoodsExplosive explosive = null; List<GoodsExplosive> explosives = new GoodsExplosive().selectList(new LambdaUpdateWrapper<GoodsExplosive>() .eq(GoodsExplosive::getGoodsItemId, goodsItem.getId()) .orderByDesc(GoodsExplosive::getId)); if (explosives.size() > 1) { explosive = explosives.stream().sorted(Comparator.comparing(GoodsExplosive::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null); } else { explosive = explosives.size() == 1 ? explosives.get(0) : explosive; } map.put("productData", explosive); break; case 2:// GoodsExcellent excellent = null; List<GoodsExcellent> excellents = new GoodsExcellent().selectList(new LambdaUpdateWrapper<GoodsExcellent>() .eq(GoodsExcellent::getGoodsItemId, goodsItem.getId()) .orderByDesc(GoodsExcellent::getId)); if (excellents.size() > 1) { excellent = excellents.stream().sorted(Comparator.comparing(GoodsExcellent::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null); } else { excellent = excellents.size() == 1 ? excellents.get(0) : excellent; } map.put("productData", excellent); break; case 3://现货 GoodsEnjoy enjoy = null; List<GoodsEnjoy> enjoys = new GoodsEnjoy().selectList(new LambdaUpdateWrapper<GoodsEnjoy>() .eq(GoodsEnjoy::getGoodsItemId, goodsItem.getId() .orderByDesc(GoodsEnjoy::getId)); if (enjoys.size() > 1) { enjoy = enjoys.stream().sorted(Comparator.comparing(GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null); } else { enjoy = enjoys.size() == 1 ? enjoys.get(0) : enjoy; } map.put("productData", enjoy); break; default: throw new IllegalStateException("Unexpected value: " + goodsItem.getProductType()); }怎么优化
这段代码中有重复的逻辑,可以抽象成一个方法,减少重复代码。同时,可以使用 Java 8 的 Optional 等新特性简化代码。具体来说,可以将以下部分重构:
```
GoodsExplosive explosive = null;
List<GoodsExplosive> explosives = new GoodsExplosive().selectList(new LambdaUpdateWrapper<GoodsExplosive>() .eq(GoodsExplosive::getGoodsItemId, goodsItem.getId()) .orderByDesc(GoodsExplosive::getId));
if (explosives.size() > 1) {
explosive = explosives.stream().sorted(Comparator.comparing(GoodsExplosive::getGroundingState, (s1, s2) -> {
if (s1.equals(s2)) {
return 0;
} else if (s1 == 1 || s2 == 5) {
return -1;
} else if (s1 == 5 || s2 == 1) {
return 1;
} else if (s1 == 2) {
return -1;
} else if (s2 == 2) {
return 1;
} else if (s1 == 0) {
return -1;
} else {
return 1;
}
})).findFirst().orElse(null);
} else {
explosive = explosives.size() == 1 ? explosives.get(0) : explosive;
}
```
可以抽象成一个方法:
```
private Optional<GoodsExplosive> getGoodsExplosive(GoodsItem goodsItem) {
List<GoodsExplosive> explosives = new GoodsExplosive().selectList(new LambdaUpdateWrapper<GoodsExplosive>()
.eq(GoodsExplosive::getGoodsItemId, goodsItem.getId())
.orderByDesc(GoodsExplosive::getId));
return explosives.stream().sorted(Comparator.comparing(GoodsExplosive::getGroundingState, (s1, s2) -> {
if (s1.equals(s2)) {
return 0;
} else if (s1 == 1 || s2 == 5) {
return -1;
} else if (s1 == 5 || s2 == 1) {
return 1;
} else if (s1 == 2) {
return -1;
} else if (s2 == 2) {
return 1;
} else if (s1 == 0) {
return -1;
} else {
return 1;
}
})).findFirst();
}
```
然后可以简化 switch 语句:
```
switch (goodsItem.getProductType()) {
case 1: //爆品
map.put("productData", getGoodsExplosive(goodsItem).orElse(null));
break;
case 2: //精品
map.put("productData", new GoodsExcellent().selectList(new LambdaUpdateWrapper<GoodsExcellent>()
.eq(GoodsExcellent::getGoodsItemId, goodsItem.getId())
.orderByDesc(GoodsExcellent::getId))
.stream().findFirst().orElse(null));
break;
case 3: //现货
map.put("productData", new GoodsEnjoy().selectList(new LambdaUpdateWrapper<GoodsEnjoy>()
.eq(GoodsEnjoy::getGoodsItemId, goodsItem.getId())
.orderByDesc(GoodsEnjoy::getId))
.stream().findFirst().orElse(null));
break;
default:
throw new IllegalStateException("Unexpected value: " + goodsItem.getProductType());
}
```
阅读全文