-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.java
More file actions
28 lines (22 loc) · 783 Bytes
/
App.java
File metadata and controls
28 lines (22 loc) · 783 Bytes
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
package kr.springboot.examples.ex4;
import java.util.Optional;
/**
* When No Value Is Present, Set/Return a Non-Existent Default Object Via the Optional.orElseGet() Method
* 값이 없을 때 Optional.orElseGet() 메서드를 통해 존재하지 않는 기본 개체를 설정또는 반환해라
*/
public class App {
public String computeStatus() {
return "UNKNOWN";
}
// Avoid
public String findUserStatusAvoidExample() {
Optional<String> status = Optional.empty();
if (status.isPresent()) return status.get();
else return computeStatus();
}
// Prefer
public String findUserStatusPreferExample() {
Optional<String> status = Optional.empty();
return status.orElseGet(this::computeStatus);
}
}