-
Notifications
You must be signed in to change notification settings - Fork 1
/
Optionals.java
180 lines (129 loc) · 4.99 KB
/
Optionals.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
174
175
176
177
178
package com.hiwijaya.javabasic;
import java.util.Optional;
public class Optionals {
public void basicOptional(){
Optional<Product> emptyOptional = Optional.empty();
Product product = new Product(1, "Spoon");
Optional<Product> optional = Optional.of(product); // of() will directly throw Exception. use ofNullable() instead
if(optional.isPresent()){
System.out.println("product isn't null: " + optional.get());
}
optional.ifPresent((p) -> System.out.println(p)); // the operation will only be executed if value is not null
}
public void nullCheck(){
Product product = new Product(1,null);
// String name = product.getName();
// if(name == null){
// name = "";
// }
String name = Optional.ofNullable(product.getName())
.orElse("-");
System.out.println(name);
}
public void ifLogic(){
ProductService service = new ProductService();
// Product product = service.findById(1);
// if(product == null){
// service.createNewProduct(1);
// }
Product product = Optional.ofNullable(service.findById(1))
.orElseGet(() -> service.createNewProduct(1));
System.out.println(product);
}
public void operationToNullValue(){
Product product = new Product(1, null);
// String name = product.getName();
// if(name != null){
// name = name.toUpperCase();
// }
// else{
// name = "-";
// }
String name = Optional.ofNullable(product.getName())
.map(n -> n.toUpperCase()) // the operation will only be executed if value is not null
.orElse("-");
System.out.println(name);
}
public void nestedIfChecking(){
Product product = new Product(1, "Spoon");
// String country = "Indonesia";
// if(product.getManufacture() != null){
// if(product.getManufacture().getCountry() != null){
// country = product.getManufacture().getCountry();
// }
// }
String country = Optional.ofNullable(product.getManufacture())
.map(m -> m.getCountry())
.orElse("Indonesia");
System.out.println(country);
}
public void checkAndThrowException(){
Product product = new Product(1, null);
// String name = product.getName();
// if(name == null){
// throw new IllegalArgumentException("Name is null");
// }
String name = Optional.ofNullable(product.getName())
.orElseThrow(() -> new IllegalArgumentException("Name is null"));
}
public void ifCheckDoSomething(){
Product product = new Product(1, "Spoon");
product.setManufacture(new Manufacture("indonesia"));
// if(product.getManufacture() != null){
// if(product.getManufacture().getCountry() != null){
// System.out.println(product.getManufacture().getCountry().toUpperCase());
// }
// }
Optional.ofNullable(product.getManufacture())
.map(m -> m.getCountry())
.ifPresent(c -> System.out.println(c.toUpperCase()));
}
public void ifFilter(){
Product product = new Product(1, "Spoon");
product.setManufacture(new Manufacture("Indonesia"));
// Long cost = 0L;
// if(product.getManufacture().getCountry().equals("Indonesia")){
// cost = 10000L;
// }
// else {
// cost = 200000L;
// }
Long cost = Optional.ofNullable(product)
.filter(p -> p.getManufacture().getCountry().equals("Indonesia"))
.map(p -> 10000L)
.orElse(20000L);
System.out.println(cost);
}
public void nestedIfFilter() {
Product product = new Product(1, "Spoon");
product.setManufacture(new Manufacture("Indonesia"));
// Long cost = 0L;
// if(product.getManufacture().getCountry().equals("Indonesia")){
// cost = 10000L;
// }
// else if(product.getManufacture().getCountry().equals("China")){
// cost= 8000L;
// }
// else {
// cost = 200000L;
// }
Long cost = Optional.of(product).filter(p -> p.getManufacture().getCountry().equals("Indonesia"))
.map(p -> 10000L).orElseGet(() -> Optional.of(product)
.filter(p -> p.getManufacture().getCountry().equals("China"))
.map(p -> 8000L)
.orElse(20000L));
System.out.println(cost);
}
public static void main(String[] args) {
Optionals o = new Optionals();
// o.basicOptional();
// o.nullCheck();
// o.ifLogic();
// o.operationToNullValue();
// o.nestedIfChecking();
// o.checkAndThrowException();
// o.ifCheckDoSomething();
// o.ifFilter();
o.nestedIfFilter();
}
}