Bimba.git

ref: 684a5b38ec0977ecaf2bd1d06f701061d14e7e02

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

import android.os.Parcel
import android.os.Parcelable
import ml.adamsprogs.bimba.models.gtfs.AgencyAndId
import ml.adamsprogs.bimba.safeSplit

data class StopSegment(val stop: AgencyAndId, var plates: Set<Plate.ID>?) : Parcelable {
    constructor(parcel: Parcel) : this(
            parcel.readSerializable() as AgencyAndId,
            parcel.readString().safeSplit(";").map { Plate.ID.fromString(it) }.toSet()
    )

    companion object CREATOR : Parcelable.Creator<StopSegment> {
        override fun createFromParcel(parcel: Parcel): StopSegment {
            return StopSegment(parcel)
        }

        override fun newArray(size: Int): Array<StopSegment?> {
            return arrayOfNulls(size)
        }
    }

    fun fillPlates() {
        plates = Timetable.getTimetable().getPlatesForStop(stop)
    }

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        dest?.writeSerializable(stop)
        if (plates != null)
            dest?.writeString(plates!!.joinToString(";") { it.toString() })
        else
            dest?.writeString("")
    }

    override fun describeContents(): Int {
        return Parcelable.CONTENTS_FILE_DESCRIPTOR
    }

    override fun equals(other: Any?): Boolean {
        if (other !is StopSegment)
            return false
        if (this.stop != other.stop)
            return false
        if (this.plates != null && other.plates == null)
            return false
        if (this.plates == null && other.plates != null)
            return false
        if (this.plates == null && other.plates == null)
            return true
        if (this.plates!!.size != other.plates!!.size)
            return false
        if (this.plates!!.containsAll(other.plates!!))
            return true
        return false
    }

    override fun hashCode(): Int {
        return super.hashCode()
    }

    fun contains(plateId: Plate.ID): Boolean {
        if (plates == null)
            return false
        return plates!!.contains(plateId)
    }

    fun remove(plateId: Plate.ID) {
        (plates as HashSet).remove(plateId)
    }

    val size: Int
        get() = plates?.size ?: 0
}