From: David ‘Bombe’ Roden Date: Thu, 26 May 2016 06:03:06 +0000 (+0200) Subject: Extract inner classes, some cleanups X-Git-Tag: v2~143 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3ad80850a756b85b9cab839d006ab104e01b61d5;p=rhynodge.git Extract inner classes, some cleanups --- diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/HourState.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/HourState.kt new file mode 100644 index 0000000..8ef39cb --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/HourState.kt @@ -0,0 +1,66 @@ +package net.pterodactylus.rhynodge.webpages.weather.wettercom + +/** + * Container for weather conditions of a single hour. + * + * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net) + */ +data class HourState(val hourIndex: Int, val temperature: Double, val rainProbability: Double, val rainAmount: Double, val windDirection: WindDirection, val windSpeed: Double, val description: String, val image: String) { + + class Builder(private val hourIndex: Int) { + + fun temperature(temperature: Double) = _1(temperature) + + inner class _1(private val temperature: Double) { + + fun rainProbability(rainProbability: Double) = _2(rainProbability) + + inner class _2(private val rainProbability: Double) { + + fun rainAmount(rainAmount: Double) = _3(rainAmount) + + inner class _3(private val rainAmount: Double) { + + fun windFrom(windDirection: WindDirection) = _4(windDirection); + + inner class _4(private val windDirection: WindDirection) { + + fun at(windSpeed: Double) = _5(windSpeed) + + inner class _5(private val windSpeed: Double) { + + fun describedAs(description: String) = _6(description) + + inner class _6(private val description: String) { + + fun withImage(imageUrl: String) = _7(imageUrl) + + inner class _7(private val imageUrl: String) { + + fun build(): HourState { + return HourState(hourIndex, temperature, rainProbability, rainAmount, windDirection, windSpeed, description, imageUrl) + } + + } + + } + + } + + } + + } + + } + + } + + } + + companion object { + + fun atHour(hourIndex: Int) = Builder(hourIndex) + + } + +} diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilter.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilter.kt index 1c8d59b..f9411a5 100644 --- a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilter.kt +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilter.kt @@ -44,12 +44,12 @@ class WetterComFilter : Filter { return LocalDateTime.from(dateTimeFormatter.parse("%s %s".format(dateElement, timeElement))) } - private fun parseHourStates(document: Document): List { + private fun parseHourStates(document: Document): List { return document.select(".weather-strip--detail").mapIndexed { index, element -> parseHourState(index, element) } } - private fun parseHourState(index: Int, hourElement: Element): WetterComState.HourState { - return WetterComState.HourState.atHour(index) + private fun parseHourState(index: Int, hourElement: Element): HourState { + return HourState.atHour(index) .temperature(parseTemperature(hourElement)) .rainProbability(parseRainProbability(hourElement)) .rainAmount(parseRainAmount(hourElement)) diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt index 5bd6291..06411cf 100644 --- a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComState.kt @@ -16,92 +16,4 @@ class WetterComState(val dateTime: LocalDateTime) : AbstractState(true) { (hours as MutableList).add(hourState) } - enum class WindDirection { - - NONE, - NORTH, - NORTHEAST, - EAST, - SOUTHEAST, - SOUTH, - SOUTHWEST, - WEST, - NORTHWEST - - } - - data class HourState(val hourIndex: Int, val temperature: Double, val rainProbability: Double, val rainAmount: Double, val windDirection: WindDirection, val windSpeed: Double, val description: String, val image: String) { - - class Builder(private val hourIndex: Int) { - - fun temperature(temperature: Double) = _1(temperature) - - inner class _1(private val temperature: Double) { - - fun rainProbability(rainProbability: Double) = _2(rainProbability) - - inner class _2(private val rainProbability: Double) { - - fun rainAmount(rainAmount: Double) = _3(rainAmount) - - inner class _3(private val rainAmount: Double) { - - fun windFrom(windDirection: WindDirection) = _4(windDirection); - - inner class _4(private val windDirection: WindDirection) { - - fun at(windSpeed: Double) = _5(windSpeed) - - inner class _5(private val windSpeed: Double) { - - fun describedAs(description: String) = _6(description) - - inner class _6(private val description: String) { - - fun withImage(imageUrl: String) = _7(imageUrl) - - inner class _7(private val imageUrl: String) { - - fun build(): HourState { - return HourState(hourIndex, temperature, rainProbability, rainAmount, windDirection, windSpeed, description, imageUrl) - } - - } - - } - - } - - } - - } - - } - - } - - } - - companion object { - - fun atHour(hourIndex: Int) = Builder(hourIndex) - - } - - } - -} - -fun String.toWindDirection(): WetterComState.WindDirection { - return when (this) { - "N" -> WetterComState.WindDirection.NORTH - "NE" -> WetterComState.WindDirection.NORTHEAST - "E" -> WetterComState.WindDirection.EAST - "SE" -> WetterComState.WindDirection.SOUTHEAST - "S" -> WetterComState.WindDirection.SOUTH - "SW" -> WetterComState.WindDirection.SOUTHWEST - "W" -> WetterComState.WindDirection.WEST - "NW" -> WetterComState.WindDirection.NORTHWEST - else -> WetterComState.WindDirection.NONE - } } diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WindDirection.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WindDirection.kt new file mode 100644 index 0000000..6335fe2 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WindDirection.kt @@ -0,0 +1,34 @@ +package net.pterodactylus.rhynodge.webpages.weather.wettercom + +/** + * The direction the wind comes from. + * + * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net) + */ +enum class WindDirection { + + NONE, + NORTH, + NORTHEAST, + EAST, + SOUTHEAST, + SOUTH, + SOUTHWEST, + WEST, + NORTHWEST + +} + +fun String.toWindDirection(): WindDirection { + return when (this) { + "N" -> WindDirection.NORTH + "NE" -> WindDirection.NORTHEAST + "E" -> WindDirection.EAST + "SE" -> WindDirection.SOUTHEAST + "S" -> WindDirection.SOUTH + "SW" -> WindDirection.SOUTHWEST + "W" -> WindDirection.WEST + "NW" -> WindDirection.NORTHWEST + else -> WindDirection.NONE + } +} diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilterTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilterTest.kt index 85e76c4..bd951f2 100644 --- a/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComFilterTest.kt @@ -2,7 +2,6 @@ package net.pterodactylus.rhynodge.webpages.weather.wettercom import net.pterodactylus.rhynodge.State import net.pterodactylus.rhynodge.filters.ResourceLoader.loadDocument -import net.pterodactylus.rhynodge.webpages.weather.wettercom.WetterComState.WindDirection import net.pterodactylus.rhynodge.states.FailedState import net.pterodactylus.rhynodge.states.HtmlState import org.hamcrest.MatcherAssert.assertThat @@ -16,7 +15,6 @@ import org.mockito.Mockito.mock import java.time.LocalDateTime import java.time.Month - /** * Unit test for [WetterComFilter]. * @@ -53,30 +51,30 @@ class WetterComFilterTest { val newState = filter.filter(htmlState) as WetterComState assertThat(newState.dateTime, `is`(LocalDateTime.of(2016, Month.MAY, 23, 5, 0))) assertThat(newState.hours, contains( - WetterComState.HourState(0, 15.0, 0.65, 0.8, WindDirection.NORTH, 5.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), - WetterComState.HourState(1, 15.0, 0.7, 0.9, WindDirection.NONE, 5.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), - WetterComState.HourState(2, 17.0, 0.75, 1.0, WindDirection.NORTHWEST, 5.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), - WetterComState.HourState(3, 17.0, 0.85, 0.3, WindDirection.NORTHWEST, 5.0, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"), - WetterComState.HourState(4, 19.0, 0.9, 0.3, WindDirection.SOUTHWEST, 5.0, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"), - WetterComState.HourState(5, 20.0, 0.85, 0.3, WindDirection.SOUTHWEST, 7.0, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"), - WetterComState.HourState(6, 20.0, 0.75, 0.3, WindDirection.SOUTHWEST, 11.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), - WetterComState.HourState(7, 20.0, 0.70, 0.3, WindDirection.WEST, 11.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), - WetterComState.HourState(8, 20.0, 0.70, 0.2, WindDirection.WEST, 9.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), - WetterComState.HourState(9, 20.0, 0.70, 0.4, WindDirection.WEST, 5.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), - WetterComState.HourState(10, 20.0, 0.70, 0.4, WindDirection.WEST, 7.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), - WetterComState.HourState(11, 19.0, 0.70, 0.4, WindDirection.WEST, 11.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), - WetterComState.HourState(12, 18.0, 0.70, 0.4, WindDirection.NORTHWEST, 12.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), - WetterComState.HourState(13, 17.0, 0.70, 0.4, WindDirection.NORTHWEST, 11.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), - WetterComState.HourState(14, 16.0, 0.75, 0.4, WindDirection.NORTHWEST, 12.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), - WetterComState.HourState(15, 15.0, 0.85, 0.2, WindDirection.WEST, 12.0, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"), - WetterComState.HourState(16, 14.0, 0.9, 0.2, WindDirection.WEST, 14.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), - WetterComState.HourState(17, 14.0, 0.9, 0.2, WindDirection.NORTHWEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), - WetterComState.HourState(18, 14.0, 0.9, 0.2, WindDirection.WEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), - WetterComState.HourState(19, 13.0, 0.85, 0.1, WindDirection.NORTHWEST, 11.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), - WetterComState.HourState(20, 13.0, 0.8, 0.01, WindDirection.WEST, 11.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), - WetterComState.HourState(21, 12.0, 0.75, 0.2, WindDirection.WEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), - WetterComState.HourState(22, 12.0, 0.75, 0.2, WindDirection.NORTHWEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), - WetterComState.HourState(23, 11.0, 0.75, 0.2, WindDirection.NORTHWEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518") + HourState(0, 15.0, 0.65, 0.8, WindDirection.NORTH, 5.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), + HourState(1, 15.0, 0.7, 0.9, WindDirection.NONE, 5.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), + HourState(2, 17.0, 0.75, 1.0, WindDirection.NORTHWEST, 5.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), + HourState(3, 17.0, 0.85, 0.3, WindDirection.NORTHWEST, 5.0, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"), + HourState(4, 19.0, 0.9, 0.3, WindDirection.SOUTHWEST, 5.0, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"), + HourState(5, 20.0, 0.85, 0.3, WindDirection.SOUTHWEST, 7.0, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"), + HourState(6, 20.0, 0.75, 0.3, WindDirection.SOUTHWEST, 11.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), + HourState(7, 20.0, 0.70, 0.3, WindDirection.WEST, 11.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), + HourState(8, 20.0, 0.70, 0.2, WindDirection.WEST, 9.0, "leichter Regen-schauer", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_80_S.png?201605201518"), + HourState(9, 20.0, 0.70, 0.4, WindDirection.WEST, 5.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), + HourState(10, 20.0, 0.70, 0.4, WindDirection.WEST, 7.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), + HourState(11, 19.0, 0.70, 0.4, WindDirection.WEST, 11.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), + HourState(12, 18.0, 0.70, 0.4, WindDirection.NORTHWEST, 12.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), + HourState(13, 17.0, 0.70, 0.4, WindDirection.NORTHWEST, 11.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), + HourState(14, 16.0, 0.75, 0.4, WindDirection.NORTHWEST, 12.0, "Regenschauer", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/d_81_S.png?201605201518"), + HourState(15, 15.0, 0.85, 0.2, WindDirection.WEST, 12.0, "leichter Regen", "http://ls1.wettercomassets.com/wcomv5/images/icons/small/d_61_S.png?201605201518"), + HourState(16, 14.0, 0.9, 0.2, WindDirection.WEST, 14.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), + HourState(17, 14.0, 0.9, 0.2, WindDirection.NORTHWEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), + HourState(18, 14.0, 0.9, 0.2, WindDirection.WEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), + HourState(19, 13.0, 0.85, 0.1, WindDirection.NORTHWEST, 11.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), + HourState(20, 13.0, 0.8, 0.01, WindDirection.WEST, 11.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), + HourState(21, 12.0, 0.75, 0.2, WindDirection.WEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), + HourState(22, 12.0, 0.75, 0.2, WindDirection.NORTHWEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518"), + HourState(23, 11.0, 0.75, 0.2, WindDirection.NORTHWEST, 12.0, "leichter Regen", "http://ls2.wettercomassets.com/wcomv5/images/icons/small/n_61_S.png?201605201518") )) }