-
Notifications
You must be signed in to change notification settings - Fork 1
/
OptionalUsage.java
173 lines (150 loc) · 4.7 KB
/
OptionalUsage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.utils.optional;
import com.utils.optional.entity.*;
import java.util.List;
import java.util.Optional;
/**
* @Author Wang Junwei
* @Date 2023/2/8 10:49
* @Description Optional业务实践
*/
public class OptionalUsage {
public static void main(String[] args) {
}
/**
* 1. if-else分支操作
*
* @param productId
* @return
*/
public static Product selectProductOtherwiseDefault(Long productId) {
Optional<Product> optional = Optional.ofNullable(getProduct(productId));
optional.ifPresent(OptionalUsage::setProductInfo);
return optional.orElse(getDefaultProduct());
// return optional.orElseThrow(BusinessException::new);
// return optional.orElseGet(OptionalUsage::getDefaultProduct);
}
public static Product selectProductOtherwiseDefault1(Long productId) {
Product product = getProduct(productId);
if (product != null) {
setProductInfo(product);
} else {
product = getDefaultProduct();
// throw new BusinessException();
}
return product;
}
/**
* 2. 对象属性非空校验
*/
public static List<ProductPicture> getProductPicture(Product product) {
Optional<Product> optional = Optional.ofNullable(product);
return optional
.map(Product::getProductDetail)
.map(ProductDetail::getProductPictures)
.orElse(null);
}
/**
* 非空校验
*
* @param product
* @return
*/
public static List<ProductPicture> getProductPicture1(Product product) {
if (product != null) {
ProductDetail productDetail = product.getProductDetail();
if (productDetail != null) {
return productDetail.getProductPictures();
}
}
return null;
}
/**
* 3. 嵌套非空校验
*/
public static void setProductInfo(Product product) {
Optional<Product> optional = Optional.ofNullable(product);
optional.ifPresent(product1 -> {
// 这里的getBrand方法在业务中一般是sql查询
product1.setBrand(Optional.ofNullable(product1.getBrandId()).map(OptionalUsage::getBrand).orElse(null));
product1.setCategory(Optional.ofNullable(product1.getCategoryId()).map(OptionalUsage::getCategory).orElse(null));
});
}
/**
* 非空校验
*
* @param product
*/
public static void setProductInfo1(Product product) {
if (product != null) {
Long brandId = product.getBrandId();
Long categoryId = product.getCategoryId();
if (brandId != null) {
product.setBrand(OptionalUsage.getBrand(brandId));
}
if (categoryId != null) {
product.setCategory(OptionalUsage.getCategory(categoryId));
}
}
}
/**
* 4. 其它校验与非空校验组合
*
* @param product
*/
public static boolean checkProductIntegrityThenUpdate(Product product) {
Optional<Product> optional = Optional.ofNullable(product)
.filter(product1 -> null != product.getProductDetail() && null != product.getCategory() && anyOtherCheck());
optional.ifPresent(OptionalUsage::updateIntegrityProductInfo);
return optional.isPresent();
}
public static boolean checkProductIntegrityThenUpdate1(Product product) {
boolean check = product != null && null != product.getProductDetail() && null != product.getCategory() && anyOtherCheck();
if (check) {
updateIntegrityProductInfo(product);
}
return check;
}
private static void updateIntegrityProductInfo(Product product) {
product.setIntegrity(true);
}
private static boolean anyOtherCheck() {
return true;
}
private static Product getProduct(Long productId) {
return null;
}
private static Product getDefaultProduct() {
return null;
}
/**
* 获取商品详情
*
* @param productId
* @return
*/
public static ProductDetail getProductDetail(Long productId) {
// mapper
ProductDetail foodDetail = ProductDetail.FOOD_DETAIL;
return foodDetail.getId().equals(productId) ? foodDetail : null;
}
/**
* 品牌
*
* @param brandId
* @return
*/
public static Brand getBrand(Long brandId) {
// mapper
return Brand.map.get(brandId);
}
/**
* 分类
*
* @param categoryId
* @return
*/
public static Category getCategory(Long categoryId) {
// mapper
return Category.map.get(categoryId);
}
}