-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.java
More file actions
58 lines (40 loc) · 1.25 KB
/
App.java
File metadata and controls
58 lines (40 loc) · 1.25 KB
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
package kr.springboot.examples.ex25;
import kr.springboot.examples.commons.Product;
import java.util.Optional;
/**
* Avoid Using Identity-Sensitive Operations on Optionals
* 옵셔널에 대한 신원 감지 연산 사용을 피하라
*/
public class App {
// Avoid
public void avoidExample() {
Product product = new Product();
Optional<Product> op1 = Optional.of(product);
Optional<Product> op2 = Optional.of(product);
// op1 == op2 => false, expected true
if (op1 == op2) {
System.out.println(false);
} else {
System.out.println(true);
}
}
// Prefer
public void preferExample() {
Product product = new Product();
Optional<Product> op1 = Optional.of(product);
Optional<Product> op2 = Optional.of(product);
// op1.equals(op2) => true,expected true
if (op1.equals(op2)) {
System.out.println(true);
} else {
System.out.println(false);
}
}
// 절때 하지 말아야 할 것
public void neverDoThat() {
Optional<Product> product = Optional.of(new Product());
synchronized(product) {
System.out.println(product.get().getName());
}
}
}