8000 update · githubcs/Java-Coding-Problems@3924360 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3924360

Browse files
committed
update
1 parent f3c0a2e commit 3924360

File tree

1 file changed

+11
-2
lines changed
  • Chapter_13/P269_OptionalInConstructorsArgs/src/modern/challenge

1 file changed

+11
-2
lines changed

Chapter_13/P269_OptionalInConstructorsArgs/src/modern/challenge/BookAvoid.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66
public class BookAvoid {
77

88
private final String title; // cannot be null
9-
private final Optional<String> isbn; // optional field, thus may be empty
9+
private final Optional<String> isbn; // optional field, cannot be null
1010

1111
public BookAvoid(String title, Optional<String> isbn) {
1212
this.title = Objects.requireNonNull(title, () -> "Title cannot be null");
13-
this.isbn = isbn;
13+
14+
/*
15+
if (isbn == null) {
16+
this.isbn = Optional.empty();
17+
} else {
18+
this.isbn = isbn;
19+
}*/
20+
21+
// or
22+
this.isbn = Objects.requireNonNullElse(isbn, Optional.empty());
1423
}
1524

1625
public String getTitle() {

0 commit comments

Comments
 (0)
0