dff71d76c2bbdefe0fce96d150499ebfae66a3a0
[rhynodge.git] / src / main / kotlin / net / pterodactylus / rhynodge / webpages / weather / WeatherState.kt
1 package net.pterodactylus.rhynodge.webpages.weather
2
3 import com.fasterxml.jackson.annotation.JsonGetter
4 import com.fasterxml.jackson.annotation.JsonProperty
5 import net.pterodactylus.rhynodge.states.AbstractState
6 import java.time.Instant
7 import java.time.ZoneId
8 import java.time.ZonedDateTime
9
10 /**
11  * Contains a weather state.
12  *
13  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
14  */
15 class WeatherState(val service: String, val dateTime: ZonedDateTime) : AbstractState(true), Iterable<HourState> {
16
17     constructor(@JsonProperty("service") service: String, @JsonProperty("dateTime") time: Long) :
18     this(service, Instant.ofEpochMilli(time).atZone(ZoneId.of("Europe/Berlin")))
19
20     @JsonProperty("hours")
21     val hours: List<HourState> = mutableListOf()
22
23     val timeMillis: Long
24         @JsonGetter("dateTime")
25         get() = dateTime.toInstant().toEpochMilli()
26
27     operator fun plusAssign(hourState: HourState) {
28         (hours as MutableList<HourState>).add(hourState)
29     }
30
31     override fun iterator(): Iterator<HourState> {
32         return hours.iterator()
33     }
34
35     override fun equals(other: Any?): Boolean {
36         other as? WeatherState ?: return false
37         return (dateTime == other.dateTime) and (hours == other.hours)
38     }
39
40 }