forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
32 lines (21 loc) · 899 Bytes
/
Copy pathBook.java
File metadata and controls
32 lines (21 loc) · 899 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 Book {
// Avoid
public void renderBook(Format format, Optional<Renderer> renderer, Optional<String> size) {
Objects.requireNonNull(format, "Format cannot be null");
Renderer bookRenderer = renderer.orElseThrow(
() -> new IllegalArgumentException("Renderer cannot be empty")
);
String bookSize = size.orElseGet(() -> "125 x 200");
System.out.println("Rendering ...");
}
// Prefer
public void renderBook(Format format, Renderer renderer, String size) {
Objects.requireNonNull(format, "Format cannot be null");
Objects.requireNonNull(renderer, "Renderer cannot be null");
String bookSize = Objects.requireNonNullElseGet(size, () -> "125 x 200");
System.out.println("Rendering ...");
}
}