Bimba.git

ref: 8ea9ead9df9f1bcd5760201d59f015531f3b3683

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

import ml.adamsprogs.bimba.models.gtfs.AgencyAndId
import java.io.Serializable

data class Plate(val id: ID, val departures: HashMap<AgencyAndId, HashSet<Departure>>?) {
    override fun toString(): String {
        var result = "${id.line}=${id.stop}=${id.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(ID(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
        }
    }

    data class ID(val line: AgencyAndId, val stop: AgencyAndId, val headsign: String) : Serializable {
        companion object {
            fun fromString(string: String): ID {
                val (line, stop, headsign) = string.split("|")
                return ID(AgencyAndId.convertFromString(line),
                        AgencyAndId.convertFromString(stop), headsign)
            }
        }

        override fun equals(other: Any?): Boolean {
            if (other !is ID)
                return false
            return line == other.line && stop == other.stop && headsign.toLowerCase() == other.headsign.toLowerCase()
        }

        override fun toString(): String {
            return "$line|$stop|$headsign"
        }

        override fun hashCode(): Int {
            var result = line.hashCode()
            result = 31 * result + stop.hashCode()
            result = 31 * result + headsign.hashCode()
            return result
        }

        constructor(other: Plate.ID) : this(other.line, other.stop, other.headsign)
    }
}