Bimba.git

ref: 5069bf915e804ee4ed5d7a4f755b3494be0055e5

app/src/main/java/xyz/apiote/bimba/czwek/api/responses/Departures.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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.api.responses

import xyz.apiote.bimba.czwek.api.AlertV1
import xyz.apiote.bimba.czwek.api.DepartureV1
import xyz.apiote.bimba.czwek.api.DepartureV2
import xyz.apiote.bimba.czwek.api.DepartureV3
import xyz.apiote.bimba.czwek.api.DepartureV4
import xyz.apiote.bimba.czwek.api.StopV1
import xyz.apiote.bimba.czwek.api.StopV2
import xyz.apiote.fruchtfleisch.Reader
import java.io.InputStream

interface DeparturesResponse {
	companion object {
		fun unmarshal(stream: InputStream): DeparturesResponse {
			val reader = Reader(stream)
			return when (val v = reader.readUInt().toULong()) {
				// 0UL -> DeparturesResponseDev.unmarshal(stream)
				1UL -> DeparturesResponseV1.unmarshal(stream)
				2UL -> DeparturesResponseV2.unmarshal(stream)
				3UL -> DeparturesResponseV3.unmarshal(stream)
				4UL -> DeparturesResponseV4.unmarshal(stream)
				else -> throw UnknownResponseVersion("Departures", v)
			}
		}
	}
}

data class DeparturesResponseV4(
	val alerts: List<AlertV1>,
	val departures: List<DepartureV4>,
	val stop: StopV2
) : DeparturesResponse {
	companion object {
		fun unmarshal(stream: InputStream): DeparturesResponseV4 {
			val alerts = mutableListOf<AlertV1>()
			val departures = mutableListOf<DepartureV4>()

			val reader = Reader(stream)
			val alertsNum = reader.readUInt().toULong()
			for (i in 0UL until alertsNum) {
				val alert = AlertV1.unmarshal(stream)
				alerts.add(alert)
			}
			val departuresNum = reader.readUInt().toULong()
			for (i in 0UL until departuresNum) {
				val departure = DepartureV4.unmarshal(stream)
				departures.add(departure)
			}

			return DeparturesResponseV4(alerts, departures, StopV2.unmarshal(stream))
		}
	}
}

data class DeparturesResponseV3(
	val alerts: List<AlertV1>,
	val departures: List<DepartureV3>,
	val stop: StopV2
) : DeparturesResponse {
	companion object {
		fun unmarshal(stream: InputStream): DeparturesResponseV3 {
			val alerts = mutableListOf<AlertV1>()
			val departures = mutableListOf<DepartureV3>()

			val reader = Reader(stream)
			val alertsNum = reader.readUInt().toULong()
			for (i in 0UL until alertsNum) {
				val alert = AlertV1.unmarshal(stream)
				alerts.add(alert)
			}
			val departuresNum = reader.readUInt().toULong()
			for (i in 0UL until departuresNum) {
				val departure = DepartureV3.unmarshal(stream)
				departures.add(departure)
			}

			return DeparturesResponseV3(alerts, departures, StopV2.unmarshal(stream))
		}
	}
}

data class DeparturesResponseV2(
	val alerts: List<AlertV1>,
	val departures: List<DepartureV2>,
	val stop: StopV2
) : DeparturesResponse {
	companion object {
		fun unmarshal(stream: InputStream): DeparturesResponseV2 {
			val alerts = mutableListOf<AlertV1>()
			val departures = mutableListOf<DepartureV2>()

			val reader = Reader(stream)
			val alertsNum = reader.readUInt().toULong()
			for (i in 0UL until alertsNum) {
				val alert = AlertV1.unmarshal(stream)
				alerts.add(alert)
			}
			val departuresNum = reader.readUInt().toULong()
			for (i in 0UL until departuresNum) {
				val departure = DepartureV2.unmarshal(stream)
				departures.add(departure)
			}

			return DeparturesResponseV2(alerts, departures, StopV2.unmarshal(stream))
		}
	}
}

data class DeparturesResponseV1(
	val alerts: List<AlertV1>,
	val departures: List<DepartureV1>,
	val stop: StopV1
) : DeparturesResponse {
	companion object {
		fun unmarshal(stream: InputStream): DeparturesResponseV1 {
			val alerts = mutableListOf<AlertV1>()
			val departures = mutableListOf<DepartureV1>()

			val reader = Reader(stream)
			val alertsNum = reader.readUInt().toULong()
			for (i in 0UL until alertsNum) {
				val alert = AlertV1.unmarshal(stream)
				alerts.add(alert)
			}
			val departuresNum = reader.readUInt().toULong()
			for (i in 0UL until departuresNum) {
				val departure = DepartureV1.unmarshal(stream)
				departures.add(departure)
			}

			return DeparturesResponseV1(alerts, departures, StopV1.unmarshal(stream))
		}
	}
}