Skip to content

Commit 14be125

Browse files
Duncan McGregordmcg
authored andcommitted
single-expressions.10 : When instead of if
1 parent 66db2a2 commit 14be125

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/main/java/travelator/EmailAddress.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ data class EmailAddress(
99

1010
companion object {
1111
@JvmStatic
12-
fun parse(value: String): EmailAddress {
13-
return emailAddress(value, value.lastIndexOf('@'))
14-
}
12+
fun parse(value: String) =
13+
emailAddress(value, value.lastIndexOf('@'))
1514

1615
private fun emailAddress(value: String, atIndex: Int): EmailAddress =
17-
if ((atIndex < 1 || atIndex == value.length - 1)) {
18-
throw IllegalArgumentException(
19-
"EmailAddress must be two parts separated by @"
20-
)
21-
} else {
22-
EmailAddress(
16+
when {
17+
atIndex < 1 || atIndex == value.length - 1 ->
18+
throw IllegalArgumentException(
19+
"EmailAddress must be two parts separated by @"
20+
)
21+
else -> EmailAddress(
2322
value.substring(0, atIndex),
2423
value.substring(atIndex + 1)
2524
)

0 commit comments

Comments
 (0)