-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jdk18.java
104 lines (90 loc) · 3.55 KB
/
Jdk18.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package samples.jdk18;
import samples.jdk17.LegalPerson;
import samples.jdk17.NaturalPerson;
import samples.jdk17.Person;
import java.time.LocalDate;
public class Jdk18 {
private NaturalPerson naturalPerson;
private LegalPerson legalPerson;
public static void main(String[] args) {
new Jdk18();
}
private Jdk18() {
createPeople();
patternMatchingForSwitches();
/*
//The called method uses if instead of the Switch Pattern Matching feature.
System.out.println("Gets the date field from natural person: " + getPersonDateUsingIf(naturalPerson));
System.out.println("Gets the date field from legal person: " + getPersonDateUsingIf(legalPerson));
*/
}
/**
* Shows how the Pattern Matching for Switches feature is used by
* the {@link #getPersonDateUsingSwitchPatternMatching(Person)} method.
*/
private void patternMatchingForSwitches() {
var msg1 = "Natural person date: " + getPersonDateUsingSwitchPatternMatching(naturalPerson);
System.out.println(msg1);
var msg2 = "Legal person date: " + getPersonDateUsingSwitchPatternMatching(legalPerson);
System.out.println(msg2);
}
/**
* Gets the birthdate if the person is a {@link NaturalPerson}
* or the foundation date if the person is a {@link LegalPerson}.
* It uses the new Pattern Matching for Switches feature
* to avoid casting to access the specific methods
* in each different object.
*
* @param person
* @return
* @see <a href="https://openjdk.org/jeps/420">JEP 420</a>
*/
private LocalDate getPersonDateUsingSwitchPatternMatching(final Person person) {
return switch(person) {
case NaturalPerson human -> human.getBirthDate();
case LegalPerson company -> company.getFoundationDate();
};
}
/**
* This is the old way to call a specific method in each
* kind of person given as parameter.
* @param person
* @return
*/
private LocalDate getPersonDateUsingIf(final Person person) {
if (person instanceof NaturalPerson)
return ((NaturalPerson)person).getBirthDate();
else // if(person instanceof LegalPerson)
{
/* Since Person is a selead class,
* we know for sure that if the person is not a natural person,
* it is a legal person. There are only these two options. */
return ((LegalPerson)person).getFoundationDate();
}
/*
There is no need for such a kind of operation,
even if we just have "else if" statement after the first "if",
because Person is a sealed class.
Try to uncomment the if after the else above, so you can see the
code still compiles, even without this line below.
*/
//throw new UnsupportedOperationException("The kind of person given is not supported.");
}
private void createPeople() {
this.naturalPerson = new NaturalPerson();
naturalPerson
.setGender('M')
.setBirthDate(LocalDate.now())
.setName("Manoel Campos")
.setAddress("Street 1");
System.out.println(naturalPerson);
this.legalPerson = new LegalPerson();
legalPerson
.setWebsite("https://mycompany.com")
.setFoundationDate(LocalDate.of(1950, 3, 19))
.setName("My Company")
.setAddress("5th Avenue");
System.out.println(legalPerson);
System.out.println();
}
}