forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutOptional.java
More file actions
38 lines (30 loc) · 971 Bytes
/
Copy pathAboutOptional.java
File metadata and controls
38 lines (30 loc) · 971 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
29
30
31
32
33
34
35
36
37
38
package java8;
import com.sandwich.koan.Koan;
import java.util.Optional;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutOptional {
boolean optionalIsPresentField = false;
@Koan
public void isPresent() {
boolean optionalIsPresent = false;
Optional<String> value = notPresent();
if (value.isPresent()) {
optionalIsPresent = true;
}
assertEquals(optionalIsPresent, __);
}
@Koan
public void ifPresentLambda() {
Optional<String> value = notPresent();
value.ifPresent( x -> optionalIsPresentField = true);
assertEquals(optionalIsPresentField, __);
}
//use optional on api to signal that value is optional
public Optional<String> notPresent() {
return Optional.empty();
}
private Optional<String> present() {
return Optional.of("is present");
}
}