Bimba.git

ref: 65df18cd9983af2f0dc626bfe4dedc343eb470dc

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

import java.io.Serializable

data class Plate(val id: ID, val departures: HashMap<Int, 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 departures = HashMap<Int, HashSet<Departure>>()
            departuresString.replace("{", "").replace("}", "").split(";")
                    .filter { it != "" }
                    .forEach {
                        try {
                            val (serviceStr, depStr) = it.split(":")
                            val dep = Departure.fromString(depStr)
                            if (departures[serviceStr] == null)
                                departures[serviceStr] = HashSet()
                            departures[serviceStr]!!.add(dep)
                        } catch (e: IllegalArgumentException) {
                        }
                    }
            return Plate(ID(lineStr, stopStr, headsign), departures)
        }

        fun join(set: Set<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
        }*/
    }

    data class ID(val line: String, val stop: String, val headsign: String) : Serializable {
        companion object {
            val dummy = Plate.ID("", "", "")

            fun fromString(string: String): ID {
                val (line, stop, headsign) = string.split("|")
                return ID(line,
                        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)
    }
}