Skip to content

Instantly share code, notes, and snippets.

@mlesikov
Created July 26, 2019 08:27
Show Gist options
  • Save mlesikov/bf093f8491a88455fde4f68e5854e845 to your computer and use it in GitHub Desktop.
Save mlesikov/bf093f8491a88455fde4f68e5854e845 to your computer and use it in GitHub Desktop.
Kotlin find max temp city - single row definition
data class City(val temp:Double)
class Utility {
fun maxTemp(cities: List<City>?): City = cities?.maxBy { it.temp }?: throw IllegalArgumentException("param cities should not be null or empty!")
}
//extension funciton - maxBy is integrated in Kotlin by default
fun List<City>.maxTemp(): City = this.maxBy { it.temp }?: throw IllegalArgumentException("param cities should not be null or empty!")
@Test
fun testFindMaxTempCity() {
val cities = listOf(City(1.0),City(2.0),City(3.0))
val maxTempCity = Utility().maxTemp(cities)
assertThat(maxTempCity, Matchers.`is`(City(3.0)))
}
@Test(expected = IllegalArgumentException::class)
fun findMaxTempCityInEmptyList() {
val cities = listOf<City>()
Utility().maxTemp(cities)
}
@Test(expected = IllegalArgumentException::class)
fun findMaxTempCityNullList() {
Utility().maxTemp(null)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment