Transform 'if' to 'when' with subject if possible

This commit is contained in:
Alexey Sedunov
2013-05-15 15:43:11 +04:00
parent b2b7bce99a
commit 158c2753b8
10 changed files with 61 additions and 43 deletions
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n == 0 -> "zero"
n == 1 -> "one"
n == 2 -> "two"
return <caret>when (n) {
0 -> "zero"
1 -> "one"
2 -> "two"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(obj: Any): String {
return <caret>when {
obj is String -> "string"
obj is Int -> "int"
obj is Class<*> -> "class"
return <caret>when (obj) {
is String -> "string"
is Int -> "int"
is Class<*> -> "class"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(obj: Any): String {
return <caret>when {
obj !is Iterable<*> -> "not iterable"
obj !is Collection<*> -> "not collection"
obj !is MutableCollection<*> -> "not mutable collection"
return <caret>when (obj) {
!is Iterable<*> -> "not iterable"
!is Collection<*> -> "not collection"
!is MutableCollection<*> -> "not mutable collection"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n !in 0..1000 -> "unknown"
n !in 0..100 -> "big"
n !in 0..10 -> "average"
return <caret>when (n) {
!in 0..1000 -> "unknown"
!in 0..100 -> "big"
!in 0..10 -> "average"
else -> "small"
}
}
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n in 0..10 -> "small"
n in 10..100 -> "average"
n in 100..1000 -> "big"
return <caret>when (n) {
in 0..10 -> "small"
in 10..100 -> "average"
in 100..1000 -> "big"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n in 0..5, n in 5..10 -> "small"
n in 10..50, n in 50..100 -> "average"
n in 100..500, n in 500..1000 -> "big"
return <caret>when (n) {
in 0..5, in 5..10 -> "small"
in 10..50, in 50..100 -> "average"
in 100..500, in 500..1000 -> "big"
else -> "unknown"
}
}
@@ -1,8 +1,8 @@
fun test(n: Int): String {
return <caret>when {
n in 0..5, n in 5..10 -> "small"
n in 10..50, n in 50..100 -> "average"
n in 100..500, n in 500..1000 -> "big"
return <caret>when (n) {
in 0..5, in 5..10 -> "small"
in 10..50, in 50..100 -> "average"
in 100..500, in 500..1000 -> "big"
else -> "unknown"
}
}
@@ -1,12 +1,12 @@
fun test(n: Int): String {
return <caret>when {
n !is Int -> "???"
n in 0..10 -> "small"
n in 10..100 -> "average"
n in 100..1000 -> "big"
n == 1000000 -> "million"
n !in -100..-10 -> "good"
n is Int -> "unknown"
return <caret>when (n) {
!is Int -> "???"
in 0..10 -> "small"
in 10..100 -> "average"
in 100..1000 -> "big"
1000000 -> "million"
!in -100..-10 -> "good"
is Int -> "unknown"
else -> "unknown"
}
}