Bimba.git

ref: 8e82fc5bc0f528a732118c80565687f02b2d7c8e

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
package ml.adamsprogs.bimba.models

import android.util.Log

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

    companion object {
        fun fromString(string: String): Plate {
            val s = string.split("=")
            val departures = HashMap<String, HashSet<Departure>>()
            for (d in s[2].replace("{", "").replace("}", "").split(";")) {
                if (d == "")
                    continue
                val dep = Departure.fromString(d)
                if (departures[dep.mode] == null)
                    departures[dep.mode] = HashSet()
                departures[dep.mode]!!.add(dep)
            }
            return Plate(s[0], s[1], departures)
        }

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