Created
July 26, 2019 08:27
-
-
Save mlesikov/bf093f8491a88455fde4f68e5854e845 to your computer and use it in GitHub Desktop.
Kotlin find max temp city - single row definition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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