Skip to content

Commit 80f1e07

Browse files
committed
supplier 공부
1 parent 26a8c8d commit 80f1e07

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## 관련 링크
2+
3+
[관련 링크](https://cornswrold.tistory.com/308)
4+
5+
## 공부한 내용
6+
7+
### supplier에 대해 알게 되었다.
8+
9+
java Optional method에 대해 공부하던 중 .orElseGet()에 대해 알게 되었다.
10+
11+
내부 코드를 보니 인자로 supplier를 받는 것을 볼 수 있었다.
12+
관련 코드는 다음과 같다.
13+
14+
```java
15+
public T orElseGet(Supplier<? extends T> supplier) {
16+
return this.value != null ? this.value : supplier.get();
17+
}
18+
19+
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
20+
if (this.value != null) {
21+
return this.value;
22+
} else {
23+
throw (Throwable)exceptionSupplier.get();
24+
}
25+
}
26+
```
27+
28+
내부 코드에서는 제네릭을 활용해 상속받은 모든 것들을 활용할 수 있게 만들어보인다.
29+
30+
여기서 Supplier는 인자없는 함수코드를 실행해준다. 또한 함수형이기에 lazy연산도 가능하다.

0 commit comments

Comments
 (0)