/
ValidationExample.java
38 lines (26 loc) · 1.18 KB
/
ValidationExample.java
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 main.java.artglorin.generic.solution.sealedinterfaces;
import java.net.URI;
import java.util.List;
public class ValidationExample {
private static final ResourceFactory factory = new ResourceFactory();
private static final ReportService reportService = new ReportService();
private static final URI uri = URI.create("file://text.txt");
private static void validate() {
// should be compile time error
String assignToString = factory.createResource(uri);
// should be compile time error
String castToString = factory.createResource(uri);
// should be OK
FileResource assignToCorrectClass = factory.createResource(uri);
// should be OK
var castToCorrectClass = (FileResource) factory.createResource(uri);
// should be compile time warning
Exception assignToIncorrectClass = factory.createResource(uri);
// should be compile time warning
var castToIncorrectClass = (Exception) factory.createResource(uri);
// should be compile time error
reportService.createReport(factory.createResource(uri));
// should be OK
reportService.createReport(List.of(factory.createResource(uri)));
}
}