forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookAvoid.java
More file actions
32 lines (24 loc) · 756 Bytes
/
Copy pathBookAvoid.java
File metadata and controls
32 lines (24 loc) · 756 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
package modern.challenge;
import java.util.Objects;
import java.util.Optional;
public class BookAvoid {
private final String title; // cannot be null
private final Optional<String> isbn; // optional field, cannot be null
public BookAvoid(String title, Optional<String> isbn) {
this.title = Objects.requireNonNull(title, () -> "Title cannot be null");
/*
if (isbn == null) {
this.isbn = Optional.empty();
} else {
this.isbn = isbn;
}*/
// or
this.isbn = Objects.requireNonNullElse(isbn, Optional.empty());
}
public String getTitle() {
return title;
}
public Optional<String> getIsbn() {
return isbn;
}
}