-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatternMatch.java
60 lines (49 loc) · 1.92 KB
/
PatternMatch.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
package michid.fun;
import static java.lang.String.format;
import java.util.function.Function;
import michid.fun.PatternMatch.Vehicle.Bicycle;
import michid.fun.PatternMatch.Vehicle.Rocket;
import michid.fun.PatternMatch.Vehicle.Ship;
public class PatternMatch {
sealed interface Vehicle {
record Ship(String name, int weight) implements Vehicle {
@Override
public <T> T match(
Function<Ship, T> ship,
Function<Rocket, T> rocket,
Function<Bicycle, T> bicycle) {
return ship.apply(this);
}
}
record Rocket(String name, int height) implements Vehicle {
@Override
public <T> T match(
Function<Ship, T> ship,
Function<Rocket, T> rocket,
Function<Bicycle, T> bicycle) {
return rocket.apply(this);
}
}
record Bicycle(String name, double speed) implements Vehicle {
@Override
public <T> T match(
Function<Ship, T> ship,
Function<Rocket, T> rocket,
Function<Bicycle, T> bicycle) {
return bicycle.apply(this);
}
}
<T> T match(
Function<Ship, T> ship,
Function<Rocket, T> rocket,
Function<Bicycle, T> bicycle);
}
public static void main(String[] args) {
Vehicle vehicle = new Ship("Moby Dick", 42);
String result = vehicle.match (
(Ship ship) -> format("The ship named %s weighs %d tons", ship.name, ship.weight),
(Rocket rocket) -> format("The rocket of height %d is called %s", rocket.height, rocket.name),
(Bicycle bicycle) -> format("My bicycle is called %s and is speeding at %f kmh", bicycle.name, bicycle.speed));
System.out.println(result);
}
}