Bimba.git

ref: 5f43908b28d99c5ce551f81eac56377da6b39e51

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

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>>()
            s[2].replace("{", "").replace("}", "").split(";")
                    .filter { it != "" }
                    .forEach {
                        try {
                            val dep = Departure.fromString(it)
                            if (departures[dep.mode] == null)
                                departures[dep.mode] = HashSet()
                            departures[dep.mode]!!.add(dep)
                        } catch (e: IllegalArgumentException) {
                        }
                    }
            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)
                }
            }
            for ((mode, _) in departures) {
                departures[mode]?.sortBy { it.time }
            }
            return departures
        }
    }
}