8000 encapsulated-collections.10 : final tidy · test-driven-development/code@618cb39 · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit 618cb39

Browse files
Duncan McGregordmcg
authored andcommitted
encapsulated-collections.10 : final tidy
1 parent 27de43b commit 618cb39

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

src/main/java/travelator/UI.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@ public void render(Iterable<Journey> route) {
1616

1717

1818
public void render(List<Journey> route) {
19-
for (int i = 0; i < RouteKt.getSize(route); i++) {
20-
var journey = RouteKt.get(route, i);
19+
for (int i = 0; i < route.size(); i++) {
20+
var journey = route.get(i);
2121
render(journey);
2222
}
2323
}
2424

2525
public void renderWithHeader(List<Journey> route) {
2626
renderHeader(
27-
RouteKt.getDepartsFrom(route), // <1>
27+
RouteKt.getDepartsFrom(route),
2828
RouteKt.getArrivesAt(route),
2929
RouteKt.getDuration(route)
3030
);
31-
for (int i = 0; i < RouteKt.getSize(route); i++) {
32-
var journey = RouteKt.get(route, i);
31+
for (var journey : route) {
3332
render(journey);
3433
}
3534
}

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,18 @@ import java.time.Duration
55

66
typealias Route = List<Journey>
77

8-
val Route.size: Int
9-
get() = this.size
10-
11-
operator fun Route.get(index: Int) = this[index]
12-
138
val Route.departsFrom: Location
14-
get() = get(0).departsFrom
9+
get() = first().departsFrom
1510

1611
val Route.arrivesAt: Location
17-
get() = get(size - 1).arrivesAt
12+
get() = last().arrivesAt
1813

1914
val Route.duration: Duration
2015
get() = Duration.between(
21-
get(0).departureTime,
22-
get(size - 1).arrivalTime
16+
first().departureTime,
17+
last().arrivalTime
2318
)
2419

25-
fun Route.withJourneyAt(index: Int, replacedBy: Journey): Route =
26-
this.withItemAt(index, replacedBy)
27-
2820
fun <T> Iterable<T>.withItemAt(index: Int, replacedBy: T): List<T> =
2921
this.toMutableList().apply {
3022
this[index] = replacedBy

src/test/java/travelator/itinerary/RouteTests.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ class RouteTests {
2727
val journey1 = Journey(waterloo, alton, someTime(), someTime(), RAIL)
2828
val journey2 = Journey(alton, alresford, someTime(), someTime(), CAMEL)
2929
val journey3 = Journey(alresford, winchester, someTime(), someTime(), BUS)
30-
val route = listOf(journey1, journey2, journey3) // <1>
31-
3230
val replacement = Journey(alton, alresford, someTime(), someTime(), RAIL)
3331

32+
val route = listOf(journey1, journey2, journey3)
3433
assertEquals(
3534
listOf(journey1, replacement, journey3),
36-
route.withJourneyAt(1, replacement) // <2>
35+
route.withItemAt(1, replacement)
3736
)
3837
}
3938
}

0 commit comments

Comments
 (0)
0