-
Notifications
You must be signed in to change notification settings - Fork 1
/
FlightsFinder.kt
78 lines (61 loc) · 3.19 KB
/
FlightsFinder.kt
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
package org.kollektions.examples.basics
import org.kollektions.consumers.Consumer
import org.kollektions.consumers.bottomNBy
import org.kollektions.consumers.consume
import org.kollektions.transformations.filterOn
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.*
import kotlin.test.Test
import kotlin.test.assertEquals
class FlightsFinder {
data class Flight(val arrival: LocalDateTime, val price: BigDecimal)
private val friday = LocalDate.of(2019, 10, 11)
private val saturday = friday.plusDays(1)
private val sunday = friday.plusDays(2)
val cheapestOnSaturday = Flight(saturday.atTime(11, 45), BigDecimal.valueOf(100L))
val earliestAfterSaturday = Flight(sunday.atTime(8, 45), BigDecimal.valueOf(410L))
val flights = listOf(
Flight(friday.atTime(14, 45), BigDecimal.valueOf(175L)),
Flight(friday.atTime(19, 0), BigDecimal.valueOf(300L)),
cheapestOnSaturday,
Flight(saturday.atTime(14, 14), BigDecimal.valueOf(175L)),
earliestAfterSaturday,
Flight(sunday.atTime(14, 45), BigDecimal.valueOf(175L))
)
@Test
fun `find flights for plans A and B`() {
val cheapestOnSaturdayPlanA = filterOn<Flight> { it.arrival.toLocalDate() == saturday }
.bottomNBy(1) { it: Flight -> it.price }
val earliestAfterSaturdayPlanB = filterOn<Flight> { it.arrival.toLocalDate() > saturday }
.bottomNBy(1) { it: Flight -> it.arrival }
val actual = flights.consume(cheapestOnSaturdayPlanA, earliestAfterSaturdayPlanB)
println(cheapestOnSaturdayPlanA.results())
println(earliestAfterSaturdayPlanB.results())
assertEquals(listOf(listOf(listOf(cheapestOnSaturday)), listOf(listOf(earliestAfterSaturday))), actual)
}
@Test
fun `wrap search results in a data class`() {
val cheapestOnSaturdayPlanA = filterOn<Flight> { it.arrival.toLocalDate() == saturday }
.bottomNBy(1) { it: Flight -> it.price }
val earliestAfterSaturdayPlanB = filterOn<Flight> { it.arrival.toLocalDate() > saturday }
.bottomNBy(1) { it: Flight -> it.arrival }
val actual = flights.consume(
{consumers: List<Consumer<Flight>> -> getFlightSearchResults(consumers)},
cheapestOnSaturdayPlanA,
earliestAfterSaturdayPlanB)
println(actual)
val expected = FlightSearchResults(Optional.of(cheapestOnSaturday), Optional.of(earliestAfterSaturday))
assertEquals(expected, actual)
}
private data class FlightSearchResults(val cheapestOnSaturday: Optional<Flight>,
val earliestAfterSaturday: Optional<Flight>)
private fun getFlightSearchResults(consumers: List<Consumer<Flight>>): FlightSearchResults {
val cheapestOnSaturday = listToOptional(consumers[0].results() as List<List<Flight>>)
val earliestAfterSaturday = listToOptional(consumers[1].results() as List<List<Flight>>)
return FlightSearchResults(cheapestOnSaturday, earliestAfterSaturday)
}
private fun<T> listToOptional(list: List<List<T>>) =
if (list.isEmpty() || list[0].isEmpty()) Optional.empty() else Optional.of(list[0][0])
}