8000 open-to-sealed.2 : make ItineraryItem a sealed class · java-to-kotlin/code@c52bee3 · GitHub
[go: up one dir, main page]

Skip to content

Commit c52bee3

Browse files
nprycedmcg
authored andcommitted
open-to-sealed.2 : make ItineraryItem a sealed class
1 parent 6055e1b commit c52bee3

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

src/main/java/travelator/itinerary/ItineraryItem.kt

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import java.time.Duration
88
import java.time.Period
99
import java.time.ZonedDateTime
1010

11-
interface ItineraryItem {
12-
val id: Id<ItineraryItem>
13-
val description: String
14-
val costs: List<Money>
15-
val mapOverlay: MapOverlay
11+
sealed class ItineraryItem { // <1>
12+
abstract val id: Id<ItineraryItem> // <2>
13+
abstract val description: String
14+
abstract val costs: List<Money>
15+
abstract val mapOverlay: MapOverlay
1616
}
1717

1818
data class Accommodation(
@@ -21,8 +21,11 @@ data class Accommodation(
2121
val checkInFrom: ZonedDateTime,
2222
val checkOutBefore: ZonedDateTime,
2323
val pricePerNight: Money
24-
) : ItineraryItem {
25-
val nights = Period.between(checkInFrom.toLocalDate(), checkOutBefore.toLocalDate()).days
24+
) : ItineraryItem() { // <3>
25+
val nights = Period.between(
26+
checkInFrom.toLocalDate(),
27+
checkOutBefore.toLocalDate()
28+
).days
2629
val totalPrice: Money = pricePerNight * nights
2730

2831
override val description
@@ -39,24 +42,28 @@ data class Accommodation(
3942

4043
}
4144

45+
4246
data class Attraction(
4347
override val id: Id<Attraction>,
4448
val location: Location,
4549
val notes: String
46-
) : ItineraryItem {
47-
override val description get() =
48-
location.userReadableName
50+
) : ItineraryItem() {
51+
override val description
52+
get() =
53+
location.userReadableName
4954

50-
override val costs get() =
51-
emptyList<Money>()
55+
override val costs
56+
get() =
57+
emptyList<Money>()
5258

53-
override val mapOverlay get() =
54-
PointOverlay(
55-
position = location.position,
56-
text = description,
57-
icon = StandardIcons.ATTRACTION,
58-
id = id
59-
)
59+
override val mapOverlay
60+
get() =
61+
PointOverlay(
62+
position = location.position,
63+
text = description,
64+
icon = StandardIcons.ATTRACTION,
65+
id = id
66+
)
6067

6168
}
6269

@@ -69,7 +76,7 @@ data class Journey(
6976
val arrivalTime: ZonedDateTime,
7077
val price: Money,
7178
val path: List<Position> = listOf(departsFrom.position, arrivesAt.position),
72-
) : ItineraryItem {
79+
) : ItineraryItem() {
7380
override val description
7481
get() = "${departsFrom.userReadableName} " +
7582
"to ${arrivesAt.userReadableName} " +
@@ -94,16 +101,17 @@ data class RestaurantBooking(
94101
override val id: Id<RestaurantBooking>,
95102
val location: Location,
96103
val time: ZonedDateTime
97-
) : ItineraryItem {
104+
) : ItineraryItem() {
98105
override val description get() = location.userReadableName
99106

100107
override val costs get() = emptyList<Money>()
101108

102-
override val mapOverlay get() =
103-
PointOverlay(
104-
id = id,
105-
position = location.position,
106-
text = location.userReadableName,
107-
icon = StandardIcons.RESTAURANT
108-
)
109+
override val mapOverlay
110+
get() =
111+
PointOverlay(
112+
id = id,
113+
position = location.position,
114+
text = location.userReadableName,
115+
icon = StandardIcons.RESTAURANT
116+
)
109117
}

0 commit comments

Comments
 (0)
0