0d1d7f1dd83320c914f2b0314d1c90c7358fe1d2
[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     @get:JsonGetter("dateTime")
24     val timeMillis = dateTime.toInstant().toEpochMilli()
25
26     operator fun plusAssign(hourState: HourState) {
27         (hours as MutableList<HourState>).add(hourState)
28     }
29
30     override fun iterator(): Iterator<HourState> {
31         return hours.iterator()
32     }
33
34     override fun equals(other: Any?): Boolean {
35         other as? WeatherState ?: return false
36         return (dateTime == other.dateTime) and (hours == other.hours)
37     }
38
39 }