Bimba.git

ref: 076a9923c8d5f588489aae6a667e82a5763a3475

app/src/main/java/ml/adamsprogs/bimba/models/Plate.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
package ml.adamsprogs.bimba.models

import org.onebusaway.gtfs.model.AgencyAndId

data class Plate(val line: AgencyAndId, val stop: AgencyAndId, val headsign: String, val departures: HashMap<AgencyAndId, HashSet<Departure>>?) {
    override fun toString(): String {
        var result = "$line=$stop=$headsign={"
        if (departures != null) {
            for ((service, column) in departures) {
                result += service.toString() + ":"
                for (departure in column) {
                    result += departure.toString() + ";"
                }
            }
        }
        result += "}"
        return result
    }

    companion object {
        fun fromString(string: String): Plate {
            val (lineStr, stopStr, headsign, departuresString) = string.split("=")
            val line = AgencyAndId.convertFromString(lineStr)
            val stop = AgencyAndId.convertFromString(stopStr)
            val departures = HashMap<AgencyAndId, HashSet<Departure>>()
            departuresString.replace("{", "").replace("}", "").split(";")
                    .filter { it != "" }
                    .forEach {
                        try {
                            val (serviceStr, depStr) = it.split(":")
                            val dep = Departure.fromString(depStr)
                            val service = AgencyAndId.convertFromString(serviceStr)
                            if (departures[service] == null)
                                departures[service] = HashSet()
                            departures[service]!!.add(dep)
                        } catch (e: IllegalArgumentException) {
                        }
                    }
            return Plate(line, stop, headsign, departures)
        }

        fun join(set: Set<Plate>): HashMap<AgencyAndId, ArrayList<Departure>> {
            val departures = HashMap<AgencyAndId, ArrayList<Departure>>()
            for (plate in set) {
                for ((mode, d) in plate.departures!!) {
                    if (departures[mode] == null)
                        departures[mode] = ArrayList()
                    departures[mode]!!.addAll(d)
                }
            }
            for ((mode, _) in departures) {
                departures[mode]?.sortBy { it.time }
            }
            return departures
        }
    }
}