-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
36 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package visitor; | ||
|
||
import java.util.List; | ||
|
||
public interface visitor3 { | ||
sealed interface Vehicle { } | ||
record Car() implements Vehicle { } | ||
record CarHauler(List<Car> cars) implements Vehicle {} | ||
|
||
static int count(Vehicle vehicle) { | ||
return switch (vehicle) { | ||
case Car() -> 1; | ||
case CarHauler(List<Car> cars) -> 1 + cars.stream().mapToInt(car -> count(car)).sum(); | ||
}; | ||
} | ||
|
||
static void main(String[] args) { | ||
var vehicle = new CarHauler(List.of(new Car(), new Car())); | ||
var count = count(vehicle); | ||
System.out.println(count); | ||
} | ||
} |